Home | History | Annotate | Download | only in impl
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  **********************************************************************
      5  * Copyright (c) 2003-2010, International Business Machines
      6  * Corporation and others.  All Rights Reserved.
      7  **********************************************************************
      8  * Author: Alan Liu
      9  * Created: October 2 2003
     10  * Since: ICU 2.8
     11  **********************************************************************
     12  */
     13 
     14 package com.ibm.icu.impl;
     15 import java.util.Date;
     16 
     17 import com.ibm.icu.util.TimeZone;
     18 
     19 /**
     20  * <code>TimeZoneAdapter</code> wraps a com.ibm.icu.util.TimeZone
     21  * subclass and inherits from java.util.TimeZone.
     22  * Without this class, we would need to 'port' java.util.Date to
     23  * com.ibm.icu.util as well, so that Date could interoperate properly
     24  * with the com.ibm.icu.util TimeZone and Calendar classes.  With this
     25  * class, we can use java.util.Date together with com.ibm.icu.util
     26  * classes.
     27  *
     28  * @see com.ibm.icu.util.TimeZone#setDefault
     29  * @author Alan Liu
     30  * @since ICU 2.8
     31  */
     32 public class TimeZoneAdapter extends java.util.TimeZone {
     33 
     34     // Generated by serialver from JDK 1.4.1_01
     35     static final long serialVersionUID = -2040072218820018557L;
     36 
     37     /**
     38      * The contained com.ibm.icu.util.TimeZone object.  Must not be null.
     39      * We delegate all methods to this object.
     40      */
     41     private TimeZone zone;
     42 
     43     /**
     44      * Given a java.util.TimeZone, wrap it in the appropriate adapter
     45      * subclass of com.ibm.icu.util.TimeZone and return the adapter.
     46      */
     47     public static java.util.TimeZone wrap(com.ibm.icu.util.TimeZone tz) {
     48         return new TimeZoneAdapter(tz);
     49     }
     50 
     51     /**
     52      * Return the java.util.TimeZone wrapped by this object.
     53      */
     54     public com.ibm.icu.util.TimeZone unwrap() {
     55         return zone;
     56     }
     57 
     58     /**
     59      * Constructs an adapter for a com.ibm.icu.util.TimeZone object.
     60      */
     61     public TimeZoneAdapter(TimeZone zone) {
     62         this.zone = zone;
     63         super.setID(zone.getID());
     64     }
     65 
     66     /**
     67      * TimeZone API; calls through to wrapped time zone.
     68      */
     69     @Override
     70     public void setID(String ID) {
     71         super.setID(ID);
     72         zone.setID(ID);
     73     }
     74 
     75     /**
     76      * TimeZone API; calls through to wrapped time zone.
     77      */
     78     @Override
     79     public boolean hasSameRules(java.util.TimeZone other) {
     80         return other instanceof TimeZoneAdapter &&
     81             zone.hasSameRules(((TimeZoneAdapter)other).zone);
     82     }
     83 
     84     /**
     85      * TimeZone API; calls through to wrapped time zone.
     86      */
     87     @Override
     88     public int getOffset(int era, int year, int month, int day, int dayOfWeek,
     89                          int millis) {
     90         return zone.getOffset(era, year, month, day, dayOfWeek, millis);
     91     }
     92 
     93     /**
     94      * TimeZone API; calls through to wrapped time zone.
     95      */
     96     @Override
     97     public int getRawOffset() {
     98         return zone.getRawOffset();
     99     }
    100 
    101     /**
    102      * TimeZone API; calls through to wrapped time zone.
    103      */
    104     @Override
    105     public void setRawOffset(int offsetMillis) {
    106         zone.setRawOffset(offsetMillis);
    107     }
    108 
    109     /**
    110      * TimeZone API; calls through to wrapped time zone.
    111      */
    112     @Override
    113     public boolean useDaylightTime() {
    114         return zone.useDaylightTime();
    115     }
    116 
    117     /**
    118      * TimeZone API; calls through to wrapped time zone.
    119      */
    120     @Override
    121     public boolean inDaylightTime(Date date) {
    122         return zone.inDaylightTime(date);
    123     }
    124 
    125     /**
    126      * Boilerplate API; calls through to wrapped object.
    127      */
    128     @Override
    129     public Object clone() {
    130         return new TimeZoneAdapter((TimeZone)zone.clone());
    131     }
    132 
    133     /**
    134      * Boilerplate API; calls through to wrapped object.
    135      */
    136     @Override
    137     public synchronized int hashCode() {
    138         return zone.hashCode();
    139     }
    140 
    141     /**
    142      * Boilerplate API; calls through to wrapped object.
    143      */
    144     @Override
    145     public boolean equals(Object obj) {
    146         if (obj instanceof TimeZoneAdapter) {
    147             obj = ((TimeZoneAdapter) obj).zone;
    148         }
    149         return zone.equals(obj);
    150     }
    151 
    152     /**
    153      * Returns a string representation of this object.
    154      * @return  a string representation of this object.
    155      */
    156     @Override
    157     public String toString() {
    158         return "TimeZoneAdapter: " + zone.toString();
    159     }
    160 }
    161