1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.promotego.logic.storehours;
20
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25
26 import org.apache.commons.lang.math.IntRange;
27
28 class WeeklyHours
29 {
30 private static IntRangeComparator s_intRangeComparator = new IntRangeComparator();
31
32 private List<IntRange> m_ranges;
33
34 public WeeklyHours()
35 {
36 m_ranges = new LinkedList<IntRange>();
37 }
38
39 public boolean isOpen(IntRange theRange)
40 {
41 if (theRange.getMinimumInteger() >= StoreHours.SECONDS_IN_WEEK)
42 {
43 throw new IllegalArgumentException("Range must begin with a number less than the number of seconds in a week");
44 }
45
46 if (theRange.getMaximumInteger() > StoreHours.SECONDS_IN_WEEK)
47 {
48
49 IntRange range1 = new IntRange(theRange.getMinimumInteger(), StoreHours.SECONDS_IN_WEEK);
50
51 IntRange range2 = new IntRange(0, theRange.getMaximumInteger()-StoreHours.SECONDS_IN_WEEK);
52 return isOpen(range1) && isOpen(range2);
53 }
54 else
55 {
56 int searchResult = Collections.binarySearch(m_ranges, theRange, s_intRangeComparator);
57
58 if (searchResult >= 0)
59 {
60 IntRange foundRange = m_ranges.get(searchResult);
61
62 if (foundRange.containsRange(theRange))
63 {
64 return true;
65 }
66 }
67
68 return false;
69 }
70 }
71
72 public void addRange(IntRange theRange)
73 {
74 if (theRange.getMinimumInteger() >= StoreHours.SECONDS_IN_WEEK)
75 {
76 throw new IllegalArgumentException("Range must begin with a number less than the number of seconds in a week");
77 }
78
79 if (theRange.getMaximumInteger() > StoreHours.SECONDS_IN_WEEK)
80 {
81
82 IntRange range1 = new IntRange(theRange.getMinimumInteger(), StoreHours.SECONDS_IN_WEEK);
83 addRange(range1);
84
85 IntRange range2 = new IntRange(0, theRange.getMaximumInteger()-StoreHours.SECONDS_IN_WEEK);
86 addRange(range2);
87 }
88 else
89 {
90 addRangeInternal(theRange);
91 }
92 }
93
94 /***
95 * @param theRange
96 */
97 private void addRangeInternal(IntRange theRange)
98 {
99 int searchResult = Collections.binarySearch(m_ranges, theRange, s_intRangeComparator);
100
101 if (searchResult >= 0)
102 {
103
104 IntRange currentRange = m_ranges.get(searchResult);
105 if (currentRange.containsRange(theRange))
106 {
107
108 }
109 else if (theRange.containsRange(currentRange))
110 {
111
112 m_ranges.remove(searchResult);
113 m_ranges.add(searchResult, theRange);
114 }
115 else
116 {
117
118 int newMinimum = minimum(currentRange.getMinimumInteger(), theRange.getMinimumInteger());
119 int newMaximum = maximum(currentRange.getMaximumInteger(), theRange.getMaximumInteger());
120 IntRange newRange = new IntRange(newMinimum, newMaximum);
121 m_ranges.remove(searchResult);
122 m_ranges.add(searchResult, newRange);
123 }
124 }
125 else
126 {
127
128 int insertionPoint = -searchResult - 1;
129 m_ranges.add(insertionPoint, theRange);
130 }
131 }
132
133 private int minimum(int a, int b)
134 {
135 if (a < b)
136 {
137 return a;
138 }
139 else
140 {
141 return b;
142 }
143 }
144
145 private int maximum(int a, int b)
146 {
147 if (a > b)
148 {
149 return a;
150 }
151 else
152 {
153 return b;
154 }
155 }
156
157 public boolean equals(Object anotherObject)
158 {
159 if (!(anotherObject instanceof WeeklyHours))
160 {
161 return false;
162 }
163
164 WeeklyHours otherHours = (WeeklyHours)anotherObject;
165
166 if (m_ranges.size() != otherHours.m_ranges.size())
167 {
168 return false;
169 }
170
171
172
173
174 Iterator<IntRange> myIterator = m_ranges.iterator();
175 Iterator<IntRange> otherIterator = otherHours.m_ranges.iterator();
176 while(myIterator.hasNext())
177 {
178 if (!myIterator.next().equals(otherIterator.next()))
179 {
180 return false;
181 }
182 }
183
184 return true;
185 }
186
187 public int hashCode()
188 {
189 int retval = 0;
190
191 for (IntRange thisRange : m_ranges)
192 {
193 retval ^= thisRange.hashCode();
194 }
195
196 return retval;
197 }
198 }