View Javadoc

1   /*
2    *  File: IntervalImpl.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
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   * Simple Implementation of an interval. Listens on property changes on the begin and end date. However it is 
20   * better practice to use the JaretDate like an immutable type most of the time.
21   * 
22   * @author Peter Kliem
23   * @version $Id: IntervalImpl.java 845 2009-02-22 18:28:44Z kliem $
24   */
25  public class IntervalImpl extends PropertyObservableBase implements Interval, PropertyChangeListener {
26      /** begin date. */
27      protected JaretDate _begin;
28      /** end date. */
29      protected JaretDate _end;
30  
31      /**
32       * Construct a date with begin and end.
33       * 
34       * @param from begin date (will be copied)
35       * @param to end date (will be copied)
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       * Default constructor leaving the interval uninitialized.
46       */
47      public IntervalImpl() {
48  
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      public JaretDate getBegin() {
55          return _begin;
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public JaretDate getEnd() {
62          return _end;
63      }
64  
65      /**
66       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
93       */
94      public boolean contains(JaretDate date) {
95          return getBegin().compareTo(date) <= 0 && getEnd().compareTo(date) >= 0;
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     public boolean contains(Interval interval) {
102         return getBegin().compareTo(interval.getBegin())>=0 && getEnd().compareTo(interval.getEnd())>=0;
103     }
104     
105     /**
106      * Static helper, check whether a dat eis included in an interval.
107      * 
108      * @param interval interval
109      * @param date date to check for inclusion
110      * @return true if date is contained in interval
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      * {@inheritDoc}
118      */
119     public int getSeconds() {
120         return getEnd().diffSeconds(getBegin());
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     public String toString() {
127         return getBegin().toDisplayString() + "--" + getEnd().toDisplayString();
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     public boolean intersects(Interval interval) {
134         return intersect(this, interval);
135     }
136 
137     /**
138      * Helper method determing if two intervals intersect.
139      * 
140      * @param i1 interval1
141      * @param i2 interval2
142      * @return true if i1 and i2 intersect
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      * Static helper method, check intersection not including the interval edges.
156      * 
157      * @param i1 interval 1 
158      * @param i2 interval 2
159      * @return true if the intervals intersect
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         // special case: intervals are exactly the same
169         if (i1.getBegin().equals(i2.getBegin()) && i1.getEnd().equals(i2.getEnd())) {
170             return true;
171         }
172         return false;
173     }
174 
175     /**
176      * Check whether a given date is contained in the interval not including the exact boundaries.
177      * 
178      * @param interval interval
179      * @param date date to be checked
180      * @return true if the date is inside the interval
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      * {@inheritDoc} check begin and end instances.
188      */
189     public void propertyChange(PropertyChangeEvent evt) {
190         // instance check is intended!
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 }