1 | /* |
---|
2 | * $Id: GeoDateArray.java,v 1.5 2003/08/22 23:02:40 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | |
---|
13 | package gov.noaa.pmel.util; |
---|
14 | |
---|
15 | import java.util.Date; |
---|
16 | import java.io.Serializable; |
---|
17 | |
---|
18 | /** |
---|
19 | * <code>GeoDateArray</code> creates an efficient storage of |
---|
20 | * <code>GeoDate</code> objects. This is accomplished by using an |
---|
21 | * internal storage of <code>long</code> for the number of |
---|
22 | * milliseconds since January 1, 1970, 00:00:00 GMT. |
---|
23 | * |
---|
24 | * @author Donald Denbo |
---|
25 | * @version $Revision: 1.5 $, $Date: 2003/08/22 23:02:40 $ |
---|
26 | * @since sgt 3.0 |
---|
27 | */ |
---|
28 | public class GeoDateArray implements Serializable { |
---|
29 | private long[] date_; |
---|
30 | |
---|
31 | /** |
---|
32 | * Construct a new <code>GeoDateArray</code> from an array of |
---|
33 | * <code>GeoDate</code>s. |
---|
34 | * |
---|
35 | * @param dates an array of <code>GeoDate</code>s. |
---|
36 | */ |
---|
37 | public GeoDateArray(GeoDate[] dates) { |
---|
38 | date_ = new long[dates.length]; |
---|
39 | for(int i=0; i < dates.length; i++) { |
---|
40 | if(!(dates[i] == null || dates[i].isMissing())) { |
---|
41 | date_[i] = dates[i].getTime(); |
---|
42 | } else { |
---|
43 | date_[i] = Long.MAX_VALUE; |
---|
44 | } |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Construct a new <code>GeoDateArray</code> from an array of |
---|
50 | * <code>Date</code>s. |
---|
51 | * |
---|
52 | * @param dates an array of <code>Date</code>s. |
---|
53 | */ |
---|
54 | public GeoDateArray(Date[] dates) { |
---|
55 | date_ = new long[dates.length]; |
---|
56 | for(int i=0; i < dates.length; i++) { |
---|
57 | if(!(dates[i] == null)) { |
---|
58 | date_[i] = dates[i].getTime(); |
---|
59 | } else { |
---|
60 | date_[i] = Long.MAX_VALUE; |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Construct a new <code>GeoDateArray</code> from an array of |
---|
67 | * <code>long</code>s that represent the number of |
---|
68 | * milliseconds since January 1, 1970, 00:00:00 GMT. Missing value |
---|
69 | * for date is <code>Long.MAX_VALUE</code>. |
---|
70 | * |
---|
71 | * @param dates an array of <code>long</code>s. |
---|
72 | */ |
---|
73 | public GeoDateArray(long[] dates) { |
---|
74 | date_ = dates; |
---|
75 | } |
---|
76 | |
---|
77 | public long[] getTime() { |
---|
78 | return date_; |
---|
79 | } |
---|
80 | |
---|
81 | public long getTime(int index) { |
---|
82 | if(index < 0 || index >= date_.length) return Long.MAX_VALUE; |
---|
83 | return date_[index]; |
---|
84 | } |
---|
85 | |
---|
86 | public GeoDate getGeoDate(int index) { |
---|
87 | if(index < 0 || index >= date_.length) return null; |
---|
88 | return new GeoDate(date_[index]); |
---|
89 | } |
---|
90 | |
---|
91 | public GeoDate[] getGeoDate() { |
---|
92 | GeoDate[] gd = new GeoDate[date_.length]; |
---|
93 | for(int i=0; i < date_.length; i++) { |
---|
94 | gd[i] = new GeoDate(date_[i]); |
---|
95 | } |
---|
96 | return gd; |
---|
97 | } |
---|
98 | /** |
---|
99 | * Time offset for reference <code>GeoDate</code>. |
---|
100 | * |
---|
101 | * @param ref reference <code>GeoDate</code> |
---|
102 | * @return offset in days |
---|
103 | */ |
---|
104 | public double getOffset(int index, GeoDate ref) { |
---|
105 | if(index < 0 || index >= date_.length) return Double.NaN; |
---|
106 | return ((double)(date_[index] - ref.getTime()))/86400000.0; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Time offset for reference <code>GeoDate</code>. |
---|
111 | * |
---|
112 | * @param ref reference <code>GeoDate</code> |
---|
113 | * @return offset in days |
---|
114 | */ |
---|
115 | public double[] getOffset(GeoDate ref) { |
---|
116 | long refgd = ref.getTime(); |
---|
117 | double[] off = new double[date_.length]; |
---|
118 | for(int i=0; i < date_.length; i++) { |
---|
119 | off[i] = ((double)(date_[i] - refgd))/86400000.0; |
---|
120 | } |
---|
121 | return off; |
---|
122 | } |
---|
123 | /** |
---|
124 | * Time offset for reference <code>GeoDate</code>. |
---|
125 | * |
---|
126 | * @param ref reference <code>GeoDate</code> |
---|
127 | * @return offset in milliseconds |
---|
128 | */ |
---|
129 | public long getOffsetTime(int index, GeoDate ref) { |
---|
130 | if(index < 0 || index >= date_.length) return Long.MAX_VALUE; |
---|
131 | return date_[index] - ref.getTime(); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Time offset for reference <code>GeoDate</code>. |
---|
136 | * |
---|
137 | * @param ref reference <code>GeoDate</code> |
---|
138 | * @return offset in milliseconds |
---|
139 | */ |
---|
140 | public long[] getOffsetTime(GeoDate ref) { |
---|
141 | long refgd = ref.getTime(); |
---|
142 | long[] off = new long[date_.length]; |
---|
143 | for(int i=0; i < date_.length; i++) { |
---|
144 | off[i] = date_[i] - refgd; |
---|
145 | } |
---|
146 | return off; |
---|
147 | } |
---|
148 | /** |
---|
149 | * Add offset to all dates. |
---|
150 | */ |
---|
151 | public void addOffset(long offset) { |
---|
152 | for(int i=0; i < date_.length; i++) { |
---|
153 | date_[i] += offset; |
---|
154 | } |
---|
155 | } |
---|
156 | /** |
---|
157 | * Add offset to single date. |
---|
158 | */ |
---|
159 | public void addOffset(int index, long offset) { |
---|
160 | if(index < 0 || index >= date_.length) return; |
---|
161 | date_[index] += offset; |
---|
162 | } |
---|
163 | /** |
---|
164 | * Get length of array. |
---|
165 | */ |
---|
166 | public int getLength() { |
---|
167 | return date_.length; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | |
---|