001: /*
002: **********************************************************************
003: * Copyright (c) 2003-2006, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **********************************************************************
006: * Author: Alan Liu
007: * Created: October 2 2003
008: * Since: ICU 2.8
009: **********************************************************************
010: */
011: package com.ibm.icu.impl;
012:
013: import com.ibm.icu.util.TimeZone;
014: import com.ibm.icu.util.SimpleTimeZone;
015: import java.util.Date;
016: import java.io.IOException;
017:
018: /**
019: * Wrapper around OlsonTimeZone object. Due to serialziation constraints
020: * SimpleTimeZone cannot be a subclass of OlsonTimeZone.
021: *
022: * The complement of this is TimeZoneAdapter, which makes a
023: * com.ibm.icu.util.TimeZone look like a java.util.TimeZone.
024: *
025: * @see com.ibm.icu.impl.JDKTimeZone
026: * @author Alan Liu
027: * @since ICU 2.8
028: */
029: public class JDKTimeZone extends TimeZone {
030:
031: private static final long serialVersionUID = -3724907649889455280L;
032:
033: /**
034: * The java.util.TimeZone wrapped by this object. Must not be null.
035: */
036: // give access to SimpleTimeZone
037: protected transient OlsonTimeZone zone;
038:
039: /**
040: * Given a java.util.TimeZone, wrap it in the appropriate adapter
041: * subclass of com.ibm.icu.util.TimeZone and return the adapter.
042: */
043: public static TimeZone wrap(java.util.TimeZone tz) {
044: if (tz instanceof TimeZoneAdapter) {
045: return ((TimeZoneAdapter) tz).unwrap();
046: }
047: if (tz instanceof java.util.SimpleTimeZone) {
048: return new SimpleTimeZone((java.util.SimpleTimeZone) tz, tz
049: .getID());
050: }
051: return new JDKTimeZone(tz);
052: }
053:
054: /**
055: * Constructs a JDKTimeZone given a java.util.TimeZone reference
056: * which must not be null.
057: * @param tz the time zone to wrap
058: *
059: * @internal
060: * @deprecated This API is ICU internal only.
061: */
062: public JDKTimeZone(java.util.TimeZone tz) {
063: String id = tz.getID();
064: try {
065: zone = new OlsonTimeZone(id);
066: } catch (Exception ex) {
067: // throw away exception
068: }
069: super .setID(id);
070: }
071:
072: protected JDKTimeZone(OlsonTimeZone tz) {
073: zone = tz;
074: super .setID(zone.getID());
075: }
076:
077: /**
078: * Default constructor
079: */
080: protected JDKTimeZone() {
081: }
082:
083: /**
084: * Sets the time zone ID. This does not change any other data in
085: * the time zone object.
086: * @param ID the new time zone ID.
087: */
088: public void setID(String ID) {
089: super .setID(ID);
090: if (zone != null) {
091: zone.setID(ID);
092: }
093: }
094:
095: /**
096: * TimeZone API; calls through to wrapped time zone.
097: */
098: public int getOffset(int era, int year, int month, int day,
099: int dayOfWeek, int milliseconds) {
100:
101: if (zone != null) {
102: return zone.getOffset(era, year, month, day, dayOfWeek,
103: milliseconds);
104: }
105: // should never occur except
106: // when old object of older version of JDKTimeZone are de-serialized.
107: // these objects may contain ids that OlsonTimeZone may not understand
108: // in such cases zone will be null
109: return 0;
110: }
111:
112: public void getOffset(long date, boolean local, int[] offsets) {
113:
114: if (zone != null) {
115: zone.getOffset(date, local, offsets);
116: } else {
117: super .getOffset(date, local, offsets);
118: }
119: }
120:
121: /**
122: * TimeZone API; calls through to wrapped time zone.
123: */
124: public void setRawOffset(int offsetMillis) {
125: if (zone != null) {
126: zone.setRawOffset(offsetMillis);
127: }
128: }
129:
130: /**
131: * TimeZone API; calls through to wrapped time zone.
132: */
133: public int getRawOffset() {
134: if (zone != null) {
135: return zone.getRawOffset();
136: }
137: // should never occur except
138: // when old object of older version of JDKTimeZone are de-serialized.
139: // these objects may contain ids that OlsonTimeZone may not understand
140: // in such cases zone will be null
141: return 0;
142: }
143:
144: /**
145: * TimeZone API; calls through to wrapped time zone.
146: */
147: public boolean useDaylightTime() {
148: if (zone != null) {
149: return zone.useDaylightTime();
150: }
151: // should never occur except
152: // when old object of older version of JDKTimeZone are de-serialized.
153: // these objects may contain ids that OlsonTimeZone may not understand
154: // in such cases zone will be null
155: return false;
156: }
157:
158: /**
159: * TimeZone API; calls through to wrapped time zone.
160: */
161: public boolean inDaylightTime(Date date) {
162: if (zone != null) {
163: return zone.inDaylightTime(date);
164: }
165: // should never occur except
166: // when old object of older version of JDKTimeZone are de-serialized.
167: // these objects may contain ids that OlsonTimeZone may not understand
168: // in such cases zone will be null
169: return false;
170: }
171:
172: /**
173: * TimeZone API.
174: */
175: public boolean hasSameRules(TimeZone other) {
176: if (other == null) {
177: return false;
178: }
179: if (other instanceof JDKTimeZone) {
180: if (zone != null) {
181: return zone.hasSameRules(((JDKTimeZone) other).zone);
182: }
183: }
184: return super .hasSameRules(other);
185: }
186:
187: /**
188: * Boilerplate API; calls through to wrapped object.
189: */
190: public Object clone() {
191: JDKTimeZone clone = new JDKTimeZone();
192: if (zone != null) {
193: clone.zone = (OlsonTimeZone) zone.clone();
194: }
195: return clone;
196: }
197:
198: /**
199: * Boilerplate API; calls through to wrapped object.
200: */
201: public synchronized int hashCode() {
202: if (zone != null) {
203: return zone.hashCode();
204: }
205: return super .hashCode();
206: }
207:
208: /**
209: * Returns the amount of time in ms that the clock is advanced during DST.
210: * @return the number of milliseconds the time is
211: * advanced with respect to standard time when the daylight savings rules
212: * are in effect. A positive number, typically one hour (3600000).
213: * @stable ICU 2.0
214: */
215: public int getDSTSavings() {
216: if (useDaylightTime()) {
217: if (zone != null) {
218: return zone.getDSTSavings();
219: }
220: return 3600000;
221: }
222: return 0;
223: }
224:
225: /**
226: * Boilerplate API; calls through to wrapped object.
227: */
228: public boolean equals(Object obj) {
229: try {
230: if (obj != null) {
231: TimeZone tz1 = zone;
232: TimeZone tz2 = ((JDKTimeZone) obj).zone;
233: boolean equal = true;
234: if (tz1 != null && tz2 != null) {
235: equal = tz1.equals(tz2);
236: }
237: return equal;
238: }
239: return false;
240: } catch (ClassCastException e) {
241: return false;
242: }
243: }
244:
245: /**
246: * Returns a string representation of this object.
247: * @return a string representation of this object.
248: */
249: public String toString() {
250: return "JDKTimeZone: " + zone.toString();
251: }
252:
253: private void writeObject(java.io.ObjectOutputStream out)
254: throws IOException {
255: if (zone != null) {
256: out.writeObject(zone.getID());
257: } else {
258: out.writeObject(getID());
259: }
260: }
261:
262: private void readObject(java.io.ObjectInputStream in)
263: throws IOException, ClassNotFoundException {
264: String id = (String) in.readObject();
265:
266: // create the TimeZone object if reading the old version of object
267: try {
268: zone = new OlsonTimeZone(id);
269: } catch (Exception ex) {
270: //throw away exception
271: }
272: setID(id);
273: }
274: }
275:
276: //eof
|