svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.timesupport / org.gvsig.timesupport.lib / org.gvsig.timesupport.lib.impl / src / main / java / org / gvsig / timesupport / impl / DefaultRelativeInterval.java @ 44136
History | View | Annotate | Download (5.38 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.timesupport.impl; |
23 |
|
24 |
import org.gvsig.timesupport.Chronology; |
25 |
import org.gvsig.timesupport.Duration; |
26 |
import org.gvsig.timesupport.Instant; |
27 |
import org.gvsig.timesupport.RelativeInstant; |
28 |
import org.gvsig.timesupport.RelativeInterval; |
29 |
import org.gvsig.timesupport.Time; |
30 |
|
31 |
/**
|
32 |
* @author gvSIG team
|
33 |
* @version $Id$
|
34 |
*/
|
35 |
public class DefaultRelativeInterval implements RelativeInterval { |
36 |
private org.joda.time.Interval jodaInterval = null; |
37 |
private Chronology chronology = null; |
38 |
|
39 |
DefaultRelativeInterval(long startInstant, long endInstant, Chronology chronology) { |
40 |
super();
|
41 |
this.jodaInterval = new org.joda.time.Interval(startInstant, endInstant, ((DefaultChronology)chronology).getJodaType()); |
42 |
this.chronology = chronology;
|
43 |
} |
44 |
|
45 |
DefaultRelativeInterval(RelativeInstant startInstant, RelativeInstant endInstant, Chronology chronology) { |
46 |
this(startInstant.toMillis(), endInstant.toMillis(), chronology);
|
47 |
} |
48 |
|
49 |
DefaultRelativeInterval(Chronology chronology, org.joda.time.Interval interval) { |
50 |
super();
|
51 |
this.jodaInterval = interval;
|
52 |
this.chronology = chronology;
|
53 |
} |
54 |
|
55 |
public long toDurationMillis() { |
56 |
return jodaInterval.toDurationMillis();
|
57 |
} |
58 |
|
59 |
public boolean contains(RelativeInstant instant) { |
60 |
return jodaInterval.contains(((DefaultRelativeInstant)instant).getJodaType());
|
61 |
} |
62 |
|
63 |
public boolean isAfter(RelativeInstant instant) { |
64 |
return jodaInterval.isAfter(((DefaultRelativeInstant)instant).getJodaType());
|
65 |
} |
66 |
|
67 |
public boolean isAfter(RelativeInterval interval) { |
68 |
return jodaInterval.isAfter(((DefaultRelativeInterval)interval).getJodaType());
|
69 |
} |
70 |
|
71 |
public boolean isBefore(RelativeInstant instant) { |
72 |
return jodaInterval.isBefore(((DefaultRelativeInstant)instant).getJodaType());
|
73 |
} |
74 |
|
75 |
public boolean isBefore(RelativeInterval interval) { |
76 |
return jodaInterval.isBefore(((DefaultRelativeInterval)interval).getJodaType());
|
77 |
} |
78 |
|
79 |
public Duration toDuration() { |
80 |
return new DefaultDuration(toDurationMillis()); |
81 |
} |
82 |
|
83 |
org.joda.time.Interval getJodaType() { |
84 |
return jodaInterval;
|
85 |
} |
86 |
|
87 |
public String toString() { |
88 |
return jodaInterval.toString();
|
89 |
} |
90 |
|
91 |
public boolean equals(Object obj) { |
92 |
if ((obj == null) || ((Time)obj).isAbsolute() || ((Time)obj).isInstant()){ |
93 |
return false; |
94 |
} |
95 |
return ((DefaultRelativeInterval)obj).getJodaType().equals(jodaInterval);
|
96 |
} |
97 |
|
98 |
public boolean contains(RelativeInterval interval) { |
99 |
return jodaInterval.contains(((DefaultRelativeInterval)interval).getJodaType());
|
100 |
} |
101 |
|
102 |
public boolean overlaps(RelativeInterval interval) { |
103 |
return jodaInterval.overlaps(((DefaultRelativeInterval)interval).getJodaType());
|
104 |
} |
105 |
|
106 |
public Chronology getChronology() {
|
107 |
return chronology;
|
108 |
} |
109 |
|
110 |
public Object clone() throws CloneNotSupportedException { |
111 |
return new DefaultRelativeInterval(chronology, new org.joda.time.Interval(jodaInterval)); |
112 |
} |
113 |
|
114 |
public RelativeInstant getStart(){
|
115 |
return new DefaultRelativeInstant(chronology, jodaInterval.getStart()); |
116 |
} |
117 |
|
118 |
public RelativeInstant getEnd(){
|
119 |
return new DefaultRelativeInstant(chronology, jodaInterval.getEnd()); |
120 |
} |
121 |
|
122 |
public boolean isRelative() { |
123 |
return true; |
124 |
} |
125 |
|
126 |
public boolean isAbsolute() { |
127 |
return false; |
128 |
} |
129 |
|
130 |
public boolean isInterval() { |
131 |
return true; |
132 |
} |
133 |
|
134 |
public boolean isInstant() { |
135 |
return false; |
136 |
} |
137 |
|
138 |
public boolean contains(Instant instant) { |
139 |
if (instant.isRelative()){
|
140 |
return contains((RelativeInstant)instant);
|
141 |
} |
142 |
return false; |
143 |
} |
144 |
|
145 |
public boolean intersects(Time time) { |
146 |
if (time.isRelative()){
|
147 |
if (time.isInterval()){
|
148 |
return intersects((RelativeInterval)time);
|
149 |
}else{
|
150 |
return contains((RelativeInstant)time);
|
151 |
} |
152 |
} |
153 |
return false; |
154 |
} |
155 |
|
156 |
public boolean intersects(RelativeInterval relativeInterval){ |
157 |
return (this.getStart().isAfter((RelativeInstant)relativeInterval.getStart()) && this.getEnd().isBefore((RelativeInstant)relativeInterval.getStart())) || |
158 |
(this.getEnd().isBefore((RelativeInstant)relativeInterval.getEnd()) && this.getEnd().isAfter((RelativeInstant)relativeInterval.getEnd())); |
159 |
} |
160 |
|
161 |
public Duration toStandardDuration() { |
162 |
return new DefaultDuration(jodaInterval.toDuration()); |
163 |
} |
164 |
} |
165 |
|