1 /*
2 * ==================================================================== The
3 * Apache Software License, Version 1.1
4 *
5 * Copyright (c) 2003 Digital Clash LLC. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met: 1.
9 * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer. 2. Redistributions in
11 * binary form must reproduce the above copyright notice, this list of
12 * conditions and the following disclaimer in the documentation and/or other
13 * materials provided with the distribution. 3. The end-user documentation
14 * included with the redistribution, if any, must include the following
15 * acknowledgment: "This product includes software developed by the ChronicJ
16 * team (http://www.chronicj.org/)." Alternately, this acknowledgment may
17 * appear in the software itself, if and wherever such third-party
18 * acknowledgments normally appear. 4. The names "ChronicJ" and "Digital Clash"
19 * not be used to endorse or promote products derived from this software
20 * without prior written permission. For written permission, please contact
21 * info@digitalclash.com. 5. Products derived from this software may not be
22 * called "ChronicJ", "Digital Clash", nor may "ChronicJ" or "Digital Clash"
23 * appear in their name, without prior written permission of Digital Clash LLC.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
27 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * ==================================================================== This
36 * product includes software developed by the by the Apache Software Foundation
37 * (http://www.apache.org/).
38 * ====================================================================
39 */
40 package org.chronicj;
41
42 import java.util.Iterator;
43
44 /***
45 * <p>
46 * Simple {@link java.util.Iterator Iterator}implmentation that can be used to
47 * traverse the sub ranges that occur between the start and end
48 * {@link org.chronicj.TimePoint TimePoints}of a given
49 * {@link org.chronicj.DateRange DateRange}.
50 * </p>
51 * <p>
52 * Iteration proceeds using the following semantics: <br />
53 * <ul>
54 * <li>{@link hasNext()}returns true if the <code>DateRange</code> returned
55 * by calling {@link next()}is included by the current range.</li>
56 * <li>{@link next()}returns a new <code>DateRange</code> instance created
57 * by using a start date of <code>current start date +1</code> and an end
58 * date of <code>current start date +2</code>.<b>NOTE:</b> increment will
59 * reflect the {@link org.chronicj.DatePrecision DatePrecision}of the
60 * underlying <code>TimePoint</code> s with which the current range was
61 * created.</li>
62 * <li>{@link remove()}is not supported.</li>
63 * </ul>
64 * </p>
65 *
66 * @author <a href="mlipper@US-ABP.com">Matthew Lipper</a>
67 *
68 * @see org.chronicj.DateRange
69 * @see org.chronicj.TimePoint
70 * @see java.util.Iterator
71 * @see <a href="http://martinfowler.com/ap2/range.html">Range</a>
72 */
73 public class RangeIterator implements Iterator
74 {
75 private final DateRange range;
76 private DateRange next;
77
78 public RangeIterator(DateRange aDateRange)
79 {
80 range = aDateRange;
81 }
82
83 /***
84 * Returns: <br/><code>currentRange.includes(nextRangeIndex));</code>
85 *
86 * @see java.util.Iterator#hasNext()
87 */
88 public boolean hasNext()
89 {
90 if (range.isEmpty())
91 {
92 return false;
93 }
94
95 if (next == null)
96 {
97 return range.includes(
98 new DateRange(
99 range.start().increment(1),
100 range.start().increment(2)));
101 }
102
103 return range.includes(
104 new DateRange(
105 next.start().increment(1),
106 next.start().increment(2)));
107 }
108
109 /***
110 * Returns: <br/><code>new DateRange(currentRangeIndex.start().increment(1), currentRangeIndex.start().increment(2));</code>
111 *
112 * @see java.util.Iterator#next()
113 */
114 public Object next()
115 {
116 if (next == null)
117 {
118 //First
119 next = new DateRange(range.start(), range.start().increment(1));
120
121 return next;
122 }
123
124 next =
125 new DateRange(next.start().increment(1), next.start().increment(2));
126
127 return next;
128 }
129
130 /***
131 * Currently, throws an <code>UnsupportedOperationException</code> as
132 * this method is not supported.
133 *
134 * @see java.util.Iterator#remove()
135 */
136 public void remove()
137 {
138 throw new UnsupportedOperationException();
139 }
140 }
This page was automatically generated by Maven