KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > JDKTimeZone


1 /*
2 **********************************************************************
3 * Copyright (c) 2003-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Author: Alan Liu
7 * Created: October 2 2003
8 * Since: ICU 2.8
9 **********************************************************************
10 */

11 package com.ibm.icu.impl;
12 import com.ibm.icu.util.TimeZone;
13 import com.ibm.icu.util.SimpleTimeZone;
14 import java.util.Date JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * Wrapper around OlsonTimeZone object. Due to serialziation constraints
19  * SimpleTimeZone cannot be a subclass of OlsonTimeZone.
20  *
21  * The complement of this is TimeZoneAdapter, which makes a
22  * com.ibm.icu.util.TimeZone look like a java.util.TimeZone.
23  *
24  * @see com.ibm.icu.impl.JDKTimeZone
25  * @author Alan Liu
26  * @since ICU 2.8
27  */

28 public class JDKTimeZone extends TimeZone {
29
30     private static final long serialVersionUID = -3724907649889455280L;
31     
32     /**
33      * The java.util.TimeZone wrapped by this object. Must not be null.
34      */

35     // give access to SimpleTimeZone
36
protected transient OlsonTimeZone zone;
37     /**
38      * Given a java.util.TimeZone, wrap it in the appropriate adapter
39      * subclass of com.ibm.icu.util.TimeZone and return the adapter.
40      */

41     public static TimeZone wrap(java.util.TimeZone JavaDoc tz) {
42         if (tz instanceof TimeZoneAdapter) {
43             return ((TimeZoneAdapter) tz).unwrap();
44         }
45         if (tz instanceof java.util.SimpleTimeZone JavaDoc) {
46             return new SimpleTimeZone((java.util.SimpleTimeZone JavaDoc) tz, tz.getID());
47         }
48         return new JDKTimeZone(tz);
49     }
50
51
52     /**
53      * Constructs a JDKTimeZone given a java.util.TimeZone reference
54      * which must not be null.
55      * @param tz the time zone to wrap
56      *
57      * @internal
58      * @deprecated This API is ICU internal only.
59      */

60     public JDKTimeZone(java.util.TimeZone JavaDoc tz) {
61         String JavaDoc id = tz.getID();
62         try{
63             zone = new OlsonTimeZone(id);
64         }catch(Exception JavaDoc ex){
65             // throw away exception
66
}
67         super.setID(id);
68     }
69     protected JDKTimeZone(OlsonTimeZone tz) {
70         zone = tz;
71         super.setID(zone.getID());
72     }
73     /**
74      * Default constructor
75      */

76     protected JDKTimeZone() {
77     }
78     /**
79      * Sets the time zone ID. This does not change any other data in
80      * the time zone object.
81      * @param ID the new time zone ID.
82      */

83     public void setID(String JavaDoc ID) {
84         super.setID(ID);
85         if(zone!=null){
86             zone.setID(ID);
87         }
88     }
89
90     /**
91      * TimeZone API; calls through to wrapped time zone.
92      */

93     public int getOffset(int era, int year, int month, int day,
94                          int dayOfWeek, int milliseconds) {
95
96         if(zone!=null){
97             return zone.getOffset(era, year, month, day,
98                                       dayOfWeek, milliseconds);
99         }
100         // should never occur except
101
// when old object of older version of JDKTimeZone are de-serialized.
102
// these objects may contain ids that OlsonTimeZone may not understand
103
// in such cases zone will be null
104
return 0;
105     }
106
107
108     public void getOffset(long date, boolean local, int[] offsets) {
109
110         if(zone!=null){
111             zone.getOffset(date, local, offsets);
112         }else{
113             super.getOffset(date, local, offsets);
114         }
115     }
116  
117     /**
118      * TimeZone API; calls through to wrapped time zone.
119      */

120     public void setRawOffset(int offsetMillis) {
121         if(zone!=null){
122             zone.setRawOffset(offsetMillis);
123         }
124     }
125
126     /**
127      * TimeZone API; calls through to wrapped time zone.
128      */

129     public int getRawOffset() {
130         if(zone!=null){
131             return zone.getRawOffset();
132         }
133         // should never occur except
134
// when old object of older version of JDKTimeZone are de-serialized.
135
// these objects may contain ids that OlsonTimeZone may not understand
136
// in such cases zone will be null
137
return 0;
138     }
139
140     /**
141      * TimeZone API; calls through to wrapped time zone.
142      */

143     public boolean useDaylightTime() {
144         if(zone!=null){
145             return zone.useDaylightTime();
146         }
147         // should never occur except
148
// when old object of older version of JDKTimeZone are de-serialized.
149
// these objects may contain ids that OlsonTimeZone may not understand
150
// in such cases zone will be null
151
return false;
152     }
153
154     /**
155      * TimeZone API; calls through to wrapped time zone.
156      */

157     public boolean inDaylightTime(Date JavaDoc date) {
158         if(zone!=null){
159             return zone.inDaylightTime(date);
160         }
161         // should never occur except
162
// when old object of older version of JDKTimeZone are de-serialized.
163
// these objects may contain ids that OlsonTimeZone may not understand
164
// in such cases zone will be null
165
return false;
166     }
167
168     /**
169      * TimeZone API.
170      */

171     public boolean hasSameRules(TimeZone other) {
172         if (other == null) {
173             return false;
174         }
175         if (other instanceof JDKTimeZone) {
176             if(zone!=null){
177                 return zone.hasSameRules(((JDKTimeZone) other).zone);
178             }
179         }
180         return super.hasSameRules(other);
181     }
182
183     /**
184      * Boilerplate API; calls through to wrapped object.
185      */

186     public Object JavaDoc clone() {
187         JDKTimeZone clone = new JDKTimeZone();
188         if(zone!=null){
189             clone.zone = (OlsonTimeZone)zone.clone();
190         }
191         return clone;
192     }
193
194     /**
195      * Boilerplate API; calls through to wrapped object.
196      */

197     public synchronized int hashCode() {
198         if(zone!=null){
199             return zone.hashCode();
200         }
201         return super.hashCode();
202     }
203
204     /**
205      * Returns the amount of time in ms that the clock is advanced during DST.
206      * @return the number of milliseconds the time is
207      * advanced with respect to standard time when the daylight savings rules
208      * are in effect. A positive number, typically one hour (3600000).
209      * @stable ICU 2.0
210      */

211     public int getDSTSavings() {
212         if (useDaylightTime()) {
213             if(zone!=null){
214                 return zone.getDSTSavings();
215             }
216             return 3600000;
217         }
218         return 0;
219     }
220
221     /**
222      * Boilerplate API; calls through to wrapped object.
223      */

224     public boolean equals(Object JavaDoc obj) {
225         try {
226             if(obj !=null){
227                 TimeZone tz1 = zone;
228                 TimeZone tz2 = ((JDKTimeZone) obj).zone;
229                 boolean equal = true;
230                 if(tz1!=null && tz2!=null){
231                     equal = tz1.equals(tz2);
232                 }
233                 return equal;
234             }
235             return false;
236         } catch (ClassCastException JavaDoc e) {
237             return false;
238         }
239     }
240
241     /**
242      * Returns a string representation of this object.
243      * @return a string representation of this object.
244      */

245     public String JavaDoc toString() {
246         return "JDKTimeZone: " + zone.toString();
247     }
248
249     private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
250         if(zone!=null){
251             out.writeObject(zone.getID());
252         }else{
253             out.writeObject(getID());
254         }
255     }
256
257     private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
258         String JavaDoc id = (String JavaDoc)in.readObject();
259
260         // create the TimeZone object if reading the old version of object
261
try{
262             zone = new OlsonTimeZone(id);
263         }catch(Exception JavaDoc ex){
264             //throw away exception
265
}
266         setID(id);
267     }
268 }
269
270 //eof
271
Popular Tags