1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.date;
12
13 import java.beans.PropertyChangeEvent;
14 import java.beans.PropertyChangeListener;
15
16 import de.jaret.util.misc.PropertyObservableBase;
17
18
19
20
21
22
23
24
25 public class IntervalImpl extends PropertyObservableBase implements Interval, PropertyChangeListener {
26
27 protected JaretDate _begin;
28
29 protected JaretDate _end;
30
31
32
33
34
35
36
37 public IntervalImpl(JaretDate from, JaretDate to) {
38 _begin = from.copy();
39 _begin.addPropertyChangeListener(this);
40 _end = to.copy();
41 _end.addPropertyChangeListener(this);
42 }
43
44
45
46
47 public IntervalImpl() {
48
49 }
50
51
52
53
54 public JaretDate getBegin() {
55 return _begin;
56 }
57
58
59
60
61 public JaretDate getEnd() {
62 return _end;
63 }
64
65
66
67
68 public void setBegin(JaretDate begin) {
69 if (_begin != null) {
70 _begin.removePropertyChangeListener(this);
71 }
72 JaretDate oldVal = _begin;
73 _begin = begin;
74 _begin.addPropertyChangeListener(this);
75 firePropertyChange(PROP_BEGIN, oldVal, begin);
76 }
77
78
79
80
81 public void setEnd(JaretDate end) {
82 if (_end != null) {
83 _end.removePropertyChangeListener(this);
84 }
85 JaretDate oldVal = _end;
86 _end = end;
87 _end.addPropertyChangeListener(this);
88 firePropertyChange(PROP_END, oldVal, end);
89 }
90
91
92
93
94 public boolean contains(JaretDate date) {
95 return getBegin().compareTo(date) <= 0 && getEnd().compareTo(date) >= 0;
96 }
97
98
99
100
101 public boolean contains(Interval interval) {
102 return getBegin().compareTo(interval.getBegin())>=0 && getEnd().compareTo(interval.getEnd())>=0;
103 }
104
105
106
107
108
109
110
111
112 public static boolean containsStatic(Interval interval, JaretDate date) {
113 return interval.getBegin().compareTo(date) <= 0 && interval.getEnd().compareTo(date) >= 0;
114 }
115
116
117
118
119 public int getSeconds() {
120 return getEnd().diffSeconds(getBegin());
121 }
122
123
124
125
126 public String toString() {
127 return getBegin().toDisplayString() + "--" + getEnd().toDisplayString();
128 }
129
130
131
132
133 public boolean intersects(Interval interval) {
134 return intersect(this, interval);
135 }
136
137
138
139
140
141
142
143
144 public static boolean intersect(Interval i1, Interval i2) {
145 if (i1.contains(i2.getBegin()) || i1.contains(i2.getEnd())) {
146 return true;
147 }
148 if (i2.contains(i1.getBegin()) || i2.contains(i1.getEnd())) {
149 return true;
150 }
151 return false;
152 }
153
154
155
156
157
158
159
160
161 public static boolean intersectNonIncluding(Interval i1, Interval i2) {
162 if (containsNonIncluding(i1, i2.getBegin()) || containsNonIncluding(i1, i2.getEnd())) {
163 return true;
164 }
165 if (containsNonIncluding(i2, i1.getBegin()) || containsNonIncluding(i2, i1.getEnd())) {
166 return true;
167 }
168
169 if (i1.getBegin().equals(i2.getBegin()) && i1.getEnd().equals(i2.getEnd())) {
170 return true;
171 }
172 return false;
173 }
174
175
176
177
178
179
180
181
182 public static boolean containsNonIncluding(Interval interval, JaretDate date) {
183 return interval.getBegin().compareTo(date) < 0 && interval.getEnd().compareTo(date) > 0;
184 }
185
186
187
188
189 public void propertyChange(PropertyChangeEvent evt) {
190
191 if (evt.getSource() == _begin) {
192 firePropertyChange(PROP_BEGIN, evt.getOldValue(), evt.getNewValue());
193 } else if (evt.getSource() == _end) {
194 firePropertyChange(PROP_END, evt.getOldValue(), evt.getNewValue());
195 }
196 }
197
198 }