[california/wip/calendar-spans: 11/13] Consolidate Iterable into UnitSpan



commit d0a5260b31fabb2a1e18683d60fb98adbcdad554
Author: Jim Nelson <jim yorba org>
Date:   Fri May 9 14:25:54 2014 -0700

    Consolidate Iterable into UnitSpan

 src/calendar/calendar-date-span.vala     |   23 ++++++-----------
 src/calendar/calendar-month-span.vala    |   39 +-----------------------------
 src/calendar/calendar-unit-span.vala     |   38 ++++++++++++++++++++++++++++-
 src/calendar/calendar-week-span.vala     |   39 +-----------------------------
 src/calendar/calendar-year-span.vala     |   39 +-----------------------------
 src/tests/tests-calendar-month-span.vala |   10 +++++++
 6 files changed, 58 insertions(+), 130 deletions(-)
---
diff --git a/src/calendar/calendar-date-span.vala b/src/calendar/calendar-date-span.vala
index b9c3e9c..7b57384 100644
--- a/src/calendar/calendar-date-span.vala
+++ b/src/calendar/calendar-date-span.vala
@@ -17,7 +17,7 @@ namespace California.Calendar {
  * look at things is that Span is a lightweight DateSpan.)
  */
 
-public class DateSpan : UnitSpan<Date>, Collection.SimpleIterable<Date> {
+public class DateSpan : UnitSpan<Date> {
     /**
      * Create a { link DateSpan} with the specified start and end dates.
      */
@@ -36,20 +36,6 @@ public class DateSpan : UnitSpan<Date>, Collection.SimpleIterable<Date> {
     }
     
     /**
-     * @inheritDoc
-     */
-    public override bool contains(Date date) {
-        return has_date(date);
-    }
-    
-    /**
-     * Returns an Iterator for all { link Date}s in the { link DateSpan}.
-     */
-    public override Collection.SimpleIterator<Date> iterator() {
-        return date_iterator();
-    }
-    
-    /**
      * Adjusts the start of the { link DateSpan} preserving the span duration.
      *
      * Since DateSpan always guarantees the { link start_date} will be before the { link end_date},
@@ -79,6 +65,13 @@ public class DateSpan : UnitSpan<Date>, Collection.SimpleIterable<Date> {
         return new DateSpan(new_end_date.adjust(diff), new_end_date);
     }
     
+    /**
+     * @inheritDoc
+     */
+    public override bool contains(Date date) {
+        return has_date(date);
+    }
+    
     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 07bbd63..9e8525c 100644
--- a/src/calendar/calendar-month-span.vala
+++ b/src/calendar/calendar-month-span.vala
@@ -10,37 +10,7 @@ namespace California.Calendar {
  * An immutable representation of a span of { link MonthOfYear}s.
  */
 
-public class MonthSpan : UnitSpan<MonthOfYear>, Collection.SimpleIterable<MonthOfYear> {
-    private class MonthSpanIterator : BaseObject, Collection.SimpleIterator<MonthOfYear> {
-        public MonthOfYear first;
-        public MonthOfYear last;
-        public MonthOfYear? current = null;
-        
-        public MonthSpanIterator(MonthSpan owner) {
-            first = owner.first;
-            last = owner.last;
-        }
-        
-        public new MonthOfYear 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 "MonthSpanIterator %s::%s".printf(first.to_string(), last.to_string());
-        }
-    }
-    
+public class MonthSpan : UnitSpan<MonthOfYear> {
     /**
      * Create a span of { link MonthOfYear}s corresponding to the start and end months.
      */
@@ -77,13 +47,6 @@ public class MonthSpan : UnitSpan<MonthOfYear>, Collection.SimpleIterable<MonthO
         return (first.compare_to(month) <= 0) && (last.compare_to(month) >= 0);
     }
     
-    /**
-     * Returns an Iterator for each { link MonthOfYear} in the { link MonthSpan}.
-     */
-    public override Collection.SimpleIterator<MonthOfYear> iterator() {
-        return new MonthSpanIterator(this);
-    }
-    
     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 9049737..6843cba 100644
--- a/src/calendar/calendar-unit-span.vala
+++ b/src/calendar/calendar-unit-span.vala
@@ -21,6 +21,40 @@ public abstract class UnitSpan<G> : Span, Collection.SimpleIterable<G>, Gee.Comp
     public const string PROP_LAST = "last";
     
     /**
+     * This relies on the fact that DiscreteUnit<G> is, in fact, G, i.e. Week is a
+     * DiscreteUnit<Week>.  This will blow-up if that's every not true.
+     */
+    private class UnitSpanIterator<G> : BaseObject, Collection.SimpleIterator<G> {
+        public DiscreteUnit<G> first;
+        public DiscreteUnit<G> last;
+        public DiscreteUnit<G>? current = null;
+        
+        public UnitSpanIterator(G first, G last) {
+            this.first = (DiscreteUnit<G>) first;
+            this.last = (DiscreteUnit<G>) last;
+        }
+        
+        public new G get() {
+            return (G) current;
+        }
+        
+        public bool next() {
+            if (current == null)
+                current = first;
+            else if (current.start_date.compare_to(last.start_date) < 0)
+                current = (DiscreteUnit<G>) current.adjust(1);
+            else
+                return false;
+            
+            return true;
+        }
+        
+        public override string to_string() {
+            return "UnitSpanIterator %s::%s".printf(first.to_string(), last.to_string());
+        }
+    }
+    
+    /**
      * The earliest delinated unit of time within the { link UnitSpan}.
      */
     public G first { get; private set; }
@@ -51,7 +85,9 @@ public abstract class UnitSpan<G> : Span, Collection.SimpleIterable<G>, Gee.Comp
     /**
      * Returns a { link Collection.SimpleIterator} of the { link UnitSpan}'s unit of time.
      */
-    public abstract Collection.SimpleIterator<G> iterator();
+    public Collection.SimpleIterator<G> iterator() {
+        return new UnitSpanIterator<G>(first, last);
+    }
     
     /**
      * Compares two { link UnitSpan}s by their { link start_date}s.
diff --git a/src/calendar/calendar-week-span.vala b/src/calendar/calendar-week-span.vala
index a286481..cb569d1 100644
--- a/src/calendar/calendar-week-span.vala
+++ b/src/calendar/calendar-week-span.vala
@@ -10,37 +10,7 @@ namespace California.Calendar {
  * An immutable representation of a span of { link Week}s.
  */
 
-public class WeekSpan : UnitSpan<Week>, Collection.SimpleIterable<Week> {
-    private class WeekSpanIterator : BaseObject, Collection.SimpleIterator<Week> {
-        public Week first;
-        public Week last;
-        public Week? current = null;
-        
-        public WeekSpanIterator(WeekSpan owner) {
-            first = owner.first;
-            last = owner.last;
-        }
-        
-        public new Week 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 "WeekSpanIterator %s::%s".printf(first.to_string(), last.to_string());
-        }
-    }
-    
+public class WeekSpan : UnitSpan<Week> {
     /**
      * The defined first day of the week for the weeks in the span.
      */
@@ -91,13 +61,6 @@ public class WeekSpan : UnitSpan<Week>, Collection.SimpleIterable<Week> {
         return (first.compare_to(week) <= 0) && (last.compare_to(week) >= 0);
     }
     
-    /**
-     * Returns an Iterator for each { link Week} in the { link WeekSpan}.
-     */
-    public override Collection.SimpleIterator<Week> iterator() {
-        return new WeekSpanIterator(this);
-    }
-    
     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
index cff5dc6..b69850f 100644
--- a/src/calendar/calendar-year-span.vala
+++ b/src/calendar/calendar-year-span.vala
@@ -10,37 +10,7 @@ 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());
-        }
-    }
-    
+public class YearSpan : UnitSpan<Year> {
     /**
      * Create a span of { link Year}s corresponding to the start and end years.
      */
@@ -77,13 +47,6 @@ public class YearSpan : UnitSpan<Year>, Collection.SimpleIterable<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());
     }
diff --git a/src/tests/tests-calendar-month-span.vala b/src/tests/tests-calendar-month-span.vala
index aa488ad..643aa4a 100644
--- a/src/tests/tests-calendar-month-span.vala
+++ b/src/tests/tests-calendar-month-span.vala
@@ -12,6 +12,7 @@ private class CalendarMonthSpan : UnitTest.Harness {
         add_case("contains-date", contains_date);
         add_case("has-month", has_month);
         add_case("iterator", iterator);
+        add_case("in-operator", in_operator);
     }
     
     protected override void setup() throws Error {
@@ -75,6 +76,15 @@ private class CalendarMonthSpan : UnitTest.Harness {
         
         return ctr == 6;
     }
+    
+    private bool in_operator() throws Error {
+        Calendar.Date first = new Calendar.Date(Calendar.DayOfMonth.for_checked(1), Calendar.Month.JAN, new 
Calendar.Year(2014));
+        Calendar.Date last = new Calendar.Date(Calendar.DayOfMonth.for_checked(30), Calendar.Month.MAR, new 
Calendar.Year(2014));
+        Calendar.MonthSpan span = new Calendar.MonthSpan.from_date_span(new Calendar.DateSpan(first, last));
+        Calendar.MonthOfYear month = new Calendar.MonthOfYear(Calendar.Month.FEB, new Calendar.Year(2014));
+        
+        return month in span;
+    }
 }
 
 }


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