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