• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 /*
3  *******************************************************************************
4  * Copyright (C) 2007-2010, International Business Machines Corporation and    *
5  * others. All Rights Reserved.                                                *
6  *******************************************************************************
7  */
8 package android.icu.util;
9 import java.util.Arrays;
10 import java.util.Date;
11 
12 /**
13  * <code>TimeArrayTimeZoneRule</code> represents a time zone rule whose start times are
14  * defined by an array of milliseconds since the standard base time.
15  *
16  * @hide Only a subset of ICU is exposed in Android
17  */
18 public class TimeArrayTimeZoneRule extends TimeZoneRule {
19 
20     private static final long serialVersionUID = -1117109130077415245L;
21 
22     private final long[] startTimes;
23     private final int timeType;
24 
25     /**
26      * Constructs a <code>TimeArrayTimeZoneRule</code> with the name, the GMT offset of its
27      * standard time, the amount of daylight saving offset adjustment and
28      * the array of times when this rule takes effect.
29      *
30      * @param name          The time zone name.
31      * @param rawOffset     The UTC offset of its standard time in milliseconds.
32      * @param dstSavings    The amount of daylight saving offset adjustment in
33      *                      milliseconds.  If this ia a rule for standard time,
34      *                      the value of this argument is 0.
35      * @param startTimes    The start times in milliseconds since the base time
36      *                      (January 1, 1970, 00:00:00).
37      * @param timeType      The time type of the start times, which is one of
38      *                      <code>DataTimeRule.WALL_TIME</code>, <code>STANDARD_TIME</code>
39      *                      and <code>UTC_TIME</code>.
40      */
TimeArrayTimeZoneRule(String name, int rawOffset, int dstSavings, long[] startTimes, int timeType)41     public TimeArrayTimeZoneRule(String name, int rawOffset, int dstSavings, long[] startTimes, int timeType) {
42         super(name, rawOffset, dstSavings);
43         if (startTimes == null || startTimes.length == 0) {
44             throw new IllegalArgumentException("No start times are specified.");
45         } else {
46             this.startTimes = startTimes.clone();
47             Arrays.sort(this.startTimes);
48         }
49         this.timeType = timeType;
50     }
51 
52     /**
53      * Gets the array of start times used by this rule.
54      *
55      * @return  An array of the start times in milliseconds since the base time
56      *          (January 1, 1970, 00:00:00 GMT).
57      */
getStartTimes()58     public long[] getStartTimes() {
59         return startTimes.clone();
60     }
61 
62     /**
63      * Gets the time type of the start times used by this rule.  The return value
64      * is either <code>DateTimeRule.WALL_TIME</code> or <code>DateTimeRule.STANDARD_TIME</code>
65      * or <code>DateTimeRule.UTC_TIME</code>.
66      *
67      * @return The time type used of the start times used by this rule.
68      */
getTimeType()69     public int getTimeType() {
70         return timeType;
71     }
72 
73     /**
74      * {@inheritDoc}
75      */
getFirstStart(int prevRawOffset, int prevDSTSavings)76     public Date getFirstStart(int prevRawOffset, int prevDSTSavings) {
77         return new Date(getUTC(startTimes[0], prevRawOffset, prevDSTSavings));
78     }
79 
80     /**
81      * {@inheritDoc}
82      */
getFinalStart(int prevRawOffset, int prevDSTSavings)83     public Date getFinalStart(int prevRawOffset, int prevDSTSavings) {
84         return new Date(getUTC(startTimes[startTimes.length - 1], prevRawOffset, prevDSTSavings));
85     }
86 
87     /**
88      * {@inheritDoc}
89      */
getNextStart(long base, int prevOffset, int prevDSTSavings, boolean inclusive)90     public Date getNextStart(long base, int prevOffset, int prevDSTSavings, boolean inclusive) {
91         int i = startTimes.length - 1;
92         for (; i >= 0; i--) {
93             long time = getUTC(startTimes[i], prevOffset, prevDSTSavings);
94             if (time < base || (!inclusive && time == base)) {
95                 break;
96             }
97         }
98         if (i == startTimes.length - 1) {
99             return null;
100         }
101         return new Date(getUTC(startTimes[i + 1], prevOffset, prevDSTSavings));
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
getPreviousStart(long base, int prevOffset, int prevDSTSavings, boolean inclusive)107     public Date getPreviousStart(long base, int prevOffset, int prevDSTSavings, boolean inclusive) {
108         int i = startTimes.length - 1;
109         for (; i >= 0; i--) {
110             long time = getUTC(startTimes[i], prevOffset, prevDSTSavings);
111             if (time < base || (inclusive && time == base)) {
112                 return new Date(time);
113             }
114         }
115         return null;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
isEquivalentTo(TimeZoneRule other)121     public boolean isEquivalentTo(TimeZoneRule other) {
122         if (!(other instanceof TimeArrayTimeZoneRule)) {
123             return false;
124         }
125         if (timeType == ((TimeArrayTimeZoneRule)other).timeType
126                 && Arrays.equals(startTimes, ((TimeArrayTimeZoneRule)other).startTimes)) {
127             return super.isEquivalentTo(other);
128         }
129         return false;
130     }
131 
132     /**
133      * {@inheritDoc}<br><br>
134      * Note: This method in <code>TimeArrayTimeZoneRule</code> always returns true.
135      */
isTransitionRule()136     public boolean isTransitionRule() {
137         return true;
138     }
139 
140     /* Get UTC of the time with the raw/dst offset */
getUTC(long time, int raw, int dst)141     private long getUTC(long time, int raw, int dst) {
142         if (timeType != DateTimeRule.UTC_TIME) {
143             time -= raw;
144         }
145         if (timeType == DateTimeRule.WALL_TIME) {
146             time -= dst;
147         }
148         return time;
149     }
150 
151     /**
152      * Returns a <code>String</code> representation of this <code>TimeArrayTimeZoneRule</code> object.
153      * This method is used for debugging purpose only.  The string representation can be changed
154      * in future version of ICU without any notice.
155      */
toString()156     public String toString() {
157         StringBuilder buf = new StringBuilder();
158         buf.append(super.toString());
159         buf.append(", timeType=");
160         buf.append(timeType);
161         buf.append(", startTimes=[");
162         for (int i = 0; i < startTimes.length; i++) {
163             if (i != 0) {
164                 buf.append(", ");
165             }
166             buf.append(Long.toString(startTimes[i]));
167         }
168         buf.append("]");
169         return buf.toString();
170     }
171 }
172