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