[california/wip/calendar-spans: 10/13] Further cleanup of UnitSpans, added YearSpan



commit e50935e6fc0f7bfad0a32e506fac54f228205ebd
Author: Jim Nelson <jim yorba org>
Date:   Thu May 8 20:45:25 2014 -0700

    Further cleanup of UnitSpans, added YearSpan

 src/Makefile.am                       |    1 +
 src/calendar/calendar-date-span.vala  |   21 +-------
 src/calendar/calendar-month-span.vala |   23 +-------
 src/calendar/calendar-unit-span.vala  |   28 ++++++++++-
 src/calendar/calendar-week-span.vala  |   23 +-------
 src/calendar/calendar-year-span.vala  |   93 +++++++++++++++++++++++++++++++++
 6 files changed, 126 insertions(+), 63 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 1cc3f22..91b35d3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -71,6 +71,7 @@ california_VALASOURCES = \
        calendar/calendar-week.vala \
        calendar/calendar-week-span.vala \
        calendar/calendar-year.vala \
+       calendar/calendar-year-span.vala \
        \
        collection/collection.vala \
        collection/collection-iterable.vala \
diff --git a/src/calendar/calendar-date-span.vala b/src/calendar/calendar-date-span.vala
index df1d8b0..b9c3e9c 100644
--- a/src/calendar/calendar-date-span.vala
+++ b/src/calendar/calendar-date-span.vala
@@ -17,8 +17,7 @@ namespace California.Calendar {
  * look at things is that Span is a lightweight DateSpan.)
  */
 
-public class DateSpan : UnitSpan<Date>, Collection.SimpleIterable<Date>, Gee.Comparable<DateSpan>,
-    Gee.Hashable<DateSpan> {
+public class DateSpan : UnitSpan<Date>, Collection.SimpleIterable<Date> {
     /**
      * Create a { link DateSpan} with the specified start and end dates.
      */
@@ -80,24 +79,6 @@ public class DateSpan : UnitSpan<Date>, Collection.SimpleIterable<Date>, Gee.Com
         return new DateSpan(new_end_date.adjust(diff), new_end_date);
     }
     
-    /**
-     * Compares two { link DateSpan}s by their { link start_date}.
-     */
-    public int compare_to(DateSpan other) {
-        return start_date.compare_to(other.start_date);
-    }
-    
-    public bool equal_to(DateSpan other) {
-        if (this == other)
-            return true;
-        
-        return start_date.equal_to(other.start_date) && end_date.equal_to(other.end_date);
-    }
-    
-    public uint hash() {
-        return start_date.hash() ^ end_date.hash();
-    }
-    
     public override string to_string() {
         return "%s::%s".printf(start_date.to_string(), end_date.to_string());
     }
diff --git a/src/calendar/calendar-month-span.vala b/src/calendar/calendar-month-span.vala
index 2961878..07bbd63 100644
--- a/src/calendar/calendar-month-span.vala
+++ b/src/calendar/calendar-month-span.vala
@@ -10,8 +10,7 @@ namespace California.Calendar {
  * An immutable representation of a span of { link MonthOfYear}s.
  */
 
-public class MonthSpan : UnitSpan<MonthOfYear>, Collection.SimpleIterable<MonthOfYear>,
-    Gee.Comparable<MonthSpan>, Gee.Hashable<MonthSpan> {
+public class MonthSpan : UnitSpan<MonthOfYear>, Collection.SimpleIterable<MonthOfYear> {
     private class MonthSpanIterator : BaseObject, Collection.SimpleIterator<MonthOfYear> {
         public MonthOfYear first;
         public MonthOfYear last;
@@ -79,30 +78,12 @@ public class MonthSpan : UnitSpan<MonthOfYear>, Collection.SimpleIterable<MonthO
     }
     
     /**
-     * Returns an Iterator for each { link MonthOfYear} (full and partial) in the { link MonthSpan}.
+     * Returns an Iterator for each { link MonthOfYear} in the { link MonthSpan}.
      */
     public override Collection.SimpleIterator<MonthOfYear> iterator() {
         return new MonthSpanIterator(this);
     }
     
-    /**
-     * Compares two { link MonthSpan}s by their { link start_date}.
-     */
-    public int compare_to(MonthSpan other) {
-        return start_date.compare_to(other.start_date);
-    }
-    
-    public bool equal_to(MonthSpan other) {
-        if (this == other)
-            return true;
-        
-        return start_date.equal_to(other.start_date) && end_date.equal_to(other.end_date);
-    }
-    
-    public uint hash() {
-        return start_date.hash() ^ end_date.hash();
-    }
-    
     public override string to_string() {
         return "months of %s".printf(to_date_span().to_string());
     }
diff --git a/src/calendar/calendar-unit-span.vala b/src/calendar/calendar-unit-span.vala
index 293dd4a..9049737 100644
--- a/src/calendar/calendar-unit-span.vala
+++ b/src/calendar/calendar-unit-span.vala
@@ -15,7 +15,8 @@ namespace California.Calendar {
  * @see YearSpan
  */
 
-public abstract class UnitSpan<G> : Span, Collection.SimpleIterable<G> {
+public abstract class UnitSpan<G> : Span, Collection.SimpleIterable<G>, Gee.Comparable<UnitSpan>,
+    Gee.Hashable<UnitSpan> {
     public const string PROP_FIRST = "first";
     public const string PROP_LAST = "last";
     
@@ -51,6 +52,31 @@ public abstract class UnitSpan<G> : Span, Collection.SimpleIterable<G> {
      * Returns a { link Collection.SimpleIterator} of the { link UnitSpan}'s unit of time.
      */
     public abstract Collection.SimpleIterator<G> iterator();
+    
+    /**
+     * Compares two { link UnitSpan}s by their { link start_date}s.
+     */
+    public int compare_to(UnitSpan other) {
+        return start_date.compare_to(other.start_date);
+    }
+    
+    /**
+     * Returns true if both { link UnitSpan}s are equal.
+     *
+     * "Equal" is defined as having the same { link start_date} and { link end_date}.  That means
+     * a { link MonthSpan} and a { link YearSpan} may be equal if the months span exactly the
+     * same years.
+     */
+    public bool equal_to(UnitSpan other) {
+        if (this == other)
+            return true;
+        
+        return start_date.equal_to(other.start_date) && end_date.equal_to(other.end_date);
+    }
+    
+    public uint hash() {
+        return start_date.hash() ^ end_date.hash();
+    }
 }
 
 }
diff --git a/src/calendar/calendar-week-span.vala b/src/calendar/calendar-week-span.vala
index dc77e94..a286481 100644
--- a/src/calendar/calendar-week-span.vala
+++ b/src/calendar/calendar-week-span.vala
@@ -10,8 +10,7 @@ namespace California.Calendar {
  * An immutable representation of a span of { link Week}s.
  */
 
-public class WeekSpan : UnitSpan<Week>, Collection.SimpleIterable<Week>, Gee.Comparable<WeekSpan>,
-    Gee.Hashable<WeekSpan> {
+public class WeekSpan : UnitSpan<Week>, Collection.SimpleIterable<Week> {
     private class WeekSpanIterator : BaseObject, Collection.SimpleIterator<Week> {
         public Week first;
         public Week last;
@@ -93,30 +92,12 @@ public class WeekSpan : UnitSpan<Week>, Collection.SimpleIterable<Week>, Gee.Com
     }
     
     /**
-     * Returns an Iterator for each { link Week} (full and partial) in the { link WeekSpan}.
+     * Returns an Iterator for each { link Week} in the { link WeekSpan}.
      */
     public override Collection.SimpleIterator<Week> iterator() {
         return new WeekSpanIterator(this);
     }
     
-    /**
-     * Compares two { link WeekSpan}s by their { link start_date}.
-     */
-    public int compare_to(WeekSpan other) {
-        return start_date.compare_to(other.start_date);
-    }
-    
-    public bool equal_to(WeekSpan other) {
-        if (this == other)
-            return true;
-        
-        return start_date.equal_to(other.start_date) && end_date.equal_to(other.end_date);
-    }
-    
-    public uint hash() {
-        return start_date.hash() ^ end_date.hash();
-    }
-    
     public override string to_string() {
         return "weeks of %s".printf(to_date_span().to_string());
     }
diff --git a/src/calendar/calendar-year-span.vala b/src/calendar/calendar-year-span.vala
new file mode 100644
index 0000000..cff5dc6
--- /dev/null
+++ b/src/calendar/calendar-year-span.vala
@@ -0,0 +1,93 @@
+/* Copyright 2014 Yorba Foundation
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later).  See the COPYING file in this distribution.
+ */
+
+namespace California.Calendar {
+
+/**
+ * An immutable representation of a span of { link Year}s.
+ */
+
+public class YearSpan : UnitSpan<Year>, Collection.SimpleIterable<Year> {
+    private class YearSpanIterator : BaseObject, Collection.SimpleIterator<Year> {
+        public Year first;
+        public Year last;
+        public Year? current = null;
+        
+        public YearSpanIterator(YearSpan owner) {
+            first = owner.first;
+            last = owner.last;
+        }
+        
+        public new Year get() {
+            return current;
+        }
+        
+        public bool next() {
+            if (current == null)
+                current = first;
+            else if (current.start_date.compare_to(last.start_date) < 0)
+                current = current.adjust(1);
+            else
+                return false;
+            
+            return true;
+        }
+        
+        public override string to_string() {
+            return "YearSpanIterator %s::%s".printf(first.to_string(), last.to_string());
+        }
+    }
+    
+    /**
+     * Create a span of { link Year}s corresponding to the start and end years.
+     */
+    public YearSpan(Year first, Year last) {
+        base (first, last, first.start_date, last.end_date);
+    }
+    
+    /**
+     * Create a span of { link Years}s starting from the specified starting month.
+     */
+    public YearSpan.count(Year first, int count) {
+        Year last = first.adjust(count);
+        
+        base (first, last, first.start_date, last.end_date);
+    }
+    
+    /**
+     * Create a span of { link Year}s from the start and end of a { link Span}.
+     *
+     * The year of the Span's start_date and the month of Span's end_date are used to determine
+     * the YearSpan.
+     */
+    public YearSpan.from_date_span(DateSpan date_span) {
+        Year first = date_span.start_date.year;
+        Year last = date_span.end_date.year;
+        
+        base (first, last, first.start_date, last.end_date);
+    }
+    
+    /**
+     * @inheritDoc
+     */
+    public override bool contains(Year year) {
+        return (first.compare_to(year) <= 0) && (last.compare_to(year) >= 0);
+    }
+    
+    /**
+     * Returns an Iterator for each { link Year} in the { link YearSpan}.
+     */
+    public override Collection.SimpleIterator<Year> iterator() {
+        return new YearSpanIterator(this);
+    }
+    
+    public override string to_string() {
+        return "months of %s".printf(to_date_span().to_string());
+    }
+}
+
+}
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]