Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / DefaultSerialInfo.java @ 2623

History | View | Annotate | Download (8.12 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.raster.impl.datastruct;
23

    
24
import java.text.ParseException;
25
import java.text.SimpleDateFormat;
26
import java.util.ArrayList;
27
import java.util.Date;
28

    
29
import org.gvsig.fmap.dal.coverage.datastruct.SerialInfo;
30
import org.gvsig.timesupport.AbsoluteInstant;
31
import org.gvsig.timesupport.AbsoluteInterval;
32
import org.gvsig.timesupport.RelativeInstant;
33
import org.gvsig.timesupport.RelativeInterval;
34
import org.gvsig.timesupport.Time;
35

    
36
/**
37
 * Time information
38
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 */
40
public class DefaultSerialInfo implements SerialInfo {
41
        public static final int    SINGLE_VALUES              = 0;
42
        public static final int    RANGES                     = 1;
43
        
44
        private ArrayList<Date>    timeDate                   = new ArrayList<Date>();
45
        private ArrayList<Double>  timeNumeric                = new ArrayList<Double>();
46
        
47
        private int                timeType                   = SINGLE_VALUES;
48
        private int                dataType                   = DATE;
49
        private String             timeFormat                 = null;
50
        private String             serialName                 = null;
51
        private String             description                = null;
52
        
53
        public DefaultSerialInfo() {
54
                timeFormat = "dd/MM/yyyy-hh:mm:ss";
55
        }
56
        
57
        public DefaultSerialInfo(String timeFormat) {
58
                this.timeFormat = timeFormat;
59
        }
60
        
61
        public DefaultSerialInfo(Time time, String description, String name) throws ParseException {
62
                timeFormat = "dd/MM/yyyy-hh:mm:ss";
63
                if(time instanceof RelativeInstant) {
64
                        setTimeType(DefaultSerialInfo.SINGLE_VALUES);
65
                        setDataType(SerialInfo.NUMERIC);
66
                        addValue((double)((RelativeInstant)time).toMillis());
67
                        setDescription(description);
68
                        setSerialName(name);
69
                }
70
                if(time instanceof AbsoluteInstant) {
71
                        AbsoluteInstant t = ((AbsoluteInstant)time);
72
                        String dateA = t.getDays() + "/" + t.getMonths() + "/" + t.getYears() + "-" + t.getHours() + "/" + t.getMinutes() + "/" + t.getSeconds();
73
                        setTimeType(DefaultSerialInfo.SINGLE_VALUES);
74
                        setDataType(SerialInfo.DATE);
75
                        addValue(dateA);
76
                        setDescription(description);
77
                        setSerialName(name);
78
                }
79
                if(time instanceof AbsoluteInterval) {
80
                        AbsoluteInstant t = ((AbsoluteInterval)time).getStart();
81
                        String dateA = t.getDays() + "/" + t.getMonths() + "/" + t.getYears() + "-" + t.getHours() + "/" + t.getMinutes() + "/" + t.getSeconds();
82
                        t = ((AbsoluteInterval)time).getEnd();
83
                        String dateB = t.getDays() + "/" + t.getMonths() + "/" + t.getYears() + "-" + t.getHours() + "/" + t.getMinutes() + "/" + t.getSeconds();
84
                        setTimeType(DefaultSerialInfo.RANGES);
85
                        setDataType(SerialInfo.DATE);
86
                        addValue(dateA);
87
                        addValue(dateB);
88
                        setDescription(description);
89
                        setSerialName(name);
90
                }
91
                if(time instanceof RelativeInterval) {
92
                        RelativeInstant t = ((RelativeInterval)time).getStart();
93
                        double dateA = t.toMillis();
94
                        t = ((RelativeInterval)time).getEnd();
95
                        double dateB = t.toMillis();
96
                        setTimeType(DefaultSerialInfo.RANGES);
97
                        setDataType(SerialInfo.NUMERIC);
98
                        addValue(dateA);
99
                        addValue(dateB);
100
                        setDescription(description);
101
                        setSerialName(name);
102
                }
103
        }
104
        
105
        public String getSerialName() {
106
                return serialName;
107
        }
108

    
109
        public void setSerialName(String serialName) {
110
                this.serialName = serialName;
111
        }
112

    
113
        public ArrayList<?> getTime() {
114
                return dataType == DATE ? timeDate : timeNumeric ;
115
        }
116

    
117
        @SuppressWarnings("unchecked")
118
        public void setTime(ArrayList<?> time) {
119
                if(dataType == DATE)
120
                        timeDate = (ArrayList<Date>)time;
121
                else
122
                        timeNumeric = (ArrayList<Double>)time;
123
        }
124

    
125
        public int getTimeType() {
126
                return timeType;
127
        }
128

    
129
        public void setTimeType(int timeType) {
130
                this.timeType = timeType;
131
        }
132
        
133
        public int getDataType() {
134
                return dataType;
135
        }
136

    
137
        public void setDataType(int dataType) {
138
                this.dataType = dataType;
139
        }
140
        
141
        public void setTimeType(String timeType) {
142
                if(timeType.compareTo("Single values") == 0)
143
                        this.timeType = 0;
144
                if(timeType.compareTo("Ranges") == 0)
145
                        this.timeType = 1;
146
        }
147
        
148
        public void addValue(String value) throws ParseException {
149
                this.dataType = DATE;
150
                SimpleDateFormat formatter = new SimpleDateFormat(timeFormat);
151
                Date date = null;
152
                try {
153
                        date = formatter.parse(value);
154
                } catch (ParseException e) {
155
                        date = formatter.parse(value + "-00:00:00");
156
                }
157
                
158
                this.timeDate.add(date);
159
        }
160
        
161
        public void addValue(double value) {
162
                this.dataType = NUMERIC;
163
                this.timeNumeric.add(new Double(value));
164
        }
165
        
166
        public String getTimeInfo(int pos) {
167
                if(dataType == DATE) {
168
                        SimpleDateFormat formatter = new SimpleDateFormat(timeFormat);
169
                        return formatter.format(timeDate.get(pos));
170
                }
171
                if(dataType == NUMERIC) {
172
                        return timeNumeric.get(pos).doubleValue() + "";
173
                }
174
                return null;
175
        }
176

    
177
        public String getDescription() {
178
                return description;
179
        }
180

    
181
        public void setDescription(String description) {
182
                this.description = description;
183
        }
184
        
185
        public boolean isInRange(SerialInfo tInfo) {
186
                if(dataType == DATE) {
187
                        return isInRangeDateType(tInfo);
188
                }
189
                if(dataType == NUMERIC) {
190
                        return isInRangeNumericType(tInfo);
191
                }
192
                return false;
193
        }
194
        
195
        /**
196
         * Returns true if the range selected in the parameters is 
197
         * inside the current.
198
         * @param tInfo
199
         * @return
200
         */
201
        @SuppressWarnings("unchecked")
202
        private boolean isInRangeDateType(SerialInfo tInfo) {
203
                ArrayList<Date> t1 = (ArrayList<Date>)tInfo.getTime();
204
                ArrayList<Date> t2 = (ArrayList<Date>)getTime();
205
                if(t1.size() == 1 && t2.size() == 1) {
206
                        long dif = t1.get(0).getTime() - t2.get(0).getTime();
207
                        if(dif == 0)
208
                                return true;
209
                }
210
                
211
                if(t1.size() == 1 && t2.size() > 1) {
212
                        long dif1 = t1.get(0).getTime() - t2.get(0).getTime();
213
                        long dif2 = t1.get(0).getTime() - t2.get(t2.size() - 1).getTime();
214
                        if(dif1 > 0 && dif2 < 0)
215
                                return true;
216
                }
217
                
218
                if(t1.size() > 1 && t2.size() == 1) {
219
                        long dif1 = t1.get(0).getTime() - t2.get(0).getTime();
220
                        long dif2 = t1.get(t1.size() - 1).getTime() - t2.get(0).getTime();
221
                        if(dif1 == 0 && dif2 == 0)
222
                                return true;
223
                }
224
                
225
                if(t1.size() > 1 && t2.size() > 1) {
226
                        long dif1 = t1.get(0).getTime() - t2.get(0).getTime();
227
                        long dif2 = t1.get(t1.size() - 1).getTime() - t2.get(t2.size() - 1).getTime();
228
                        if(dif1 >= 0 && dif2 <= 0) 
229
                                return true;
230
                }
231
                return false;
232
        }
233
        
234
        /**
235
         * Returns true if the range selected in the parameters is 
236
         * inside the current.
237
         * @param tInfo
238
         * @return
239
         */
240
        @SuppressWarnings("unchecked")
241
        private boolean isInRangeNumericType(SerialInfo tInfo) {
242
                ArrayList<Double> t1 = (ArrayList<Double>)tInfo.getTime();
243
                ArrayList<Double> t2 = (ArrayList<Double>)getTime();
244
                if(t1.size() == 1 && t2.size() == 1) {
245
                        double dif = t1.get(0).doubleValue() - t2.get(0).doubleValue();
246
                        if(dif == 0)
247
                                return true;
248
                }
249
                
250
                if(t1.size() == 1 && t2.size() > 1) {
251
                        double dif1 = t1.get(0).doubleValue() - t2.get(0).doubleValue();
252
                        double dif2 = t1.get(0).doubleValue() - t2.get(t2.size() - 1).doubleValue();
253
                        if(dif1 > 0 && dif2 < 0)
254
                                return true;
255
                }
256
                
257
                if(t1.size() > 1 && t2.size() == 1) {
258
                        double dif1 = t1.get(0).doubleValue() - t2.get(0).doubleValue();
259
                        double dif2 = t1.get(t1.size() - 1).doubleValue() - t2.get(0).doubleValue();
260
                        if(dif1 == 0 && dif2 == 0)
261
                                return true;
262
                }
263
                
264
                if(t1.size() > 1 && t2.size() > 1) {
265
                        double dif1 = t1.get(0).doubleValue() - t2.get(0).doubleValue();
266
                        double dif2 = t1.get(t1.size() - 1).doubleValue() - t2.get(t2.size() - 1).doubleValue();
267
                        if(dif1 >= 0 && dif2 <= 0) 
268
                                return true;
269
                }
270
                return false;
271
        }
272
}