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