Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/calendar/zi/Rule.java
38855 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.util.ArrayList;26import java.util.Arrays;27import java.util.Comparator;28import java.util.List;29import java.util.StringTokenizer;3031/**32* Rule manipulates Rule records.33*34* @since 1.435*/36class Rule {3738private List<RuleRec> list;39private String name;4041/**42* Constructs a Rule which consists of a Rule record list. The43* specified name is given to this Rule.44* @param name the Rule name45*/46Rule(String name) {47this.name = name;48list = new ArrayList<RuleRec>();49}5051/**52* Added a RuleRec to the Rule record list.53*/54void add(RuleRec rec) {55list.add(rec);56}5758/**59* @return the Rule name60*/61String getName() {62return name;63}6465/**66* Gets all rule records that cover the given year.67*68* @param year the year number for which the rule is applicable.69* @return rules in List that are collated in time. If no rule is found, an empty70* List is returned.71*/72List<RuleRec> getRules(int year) {73List<RuleRec> rules = new ArrayList<RuleRec>(3);74for (RuleRec rec : list) {75if (year >= rec.getFromYear() && year <= rec.getToYear()) {76if ((rec.isOdd() && year % 2 == 0) || (rec.isEven() && year % 2 == 1))77continue;78rules.add(rec);79}80}81int n = rules.size();82if (n <= 1) {83return rules;84}85if (n == 2) {86RuleRec rec1 = rules.get(0);87RuleRec rec2 = rules.get(1);88if (rec1.getMonthNum() > rec2.getMonthNum()) {89rules.set(0, rec2);90rules.set(1, rec1);91} else if (rec1.getMonthNum() == rec2.getMonthNum()) {92// TODO: it's not accurate to ignore time types (STD, WALL, UTC)93long t1 = Time.getLocalTime(year, rec1.getMonth(),94rec1.getDay(), rec1.getTime().getTime());95long t2 = Time.getLocalTime(year, rec2.getMonth(),96rec2.getDay(), rec2.getTime().getTime());97if (t1 > t2) {98rules.set(0, rec2);99rules.set(1, rec1);100}101}102return rules;103}104105final int y = year;106RuleRec[] recs = new RuleRec[rules.size()];107rules.toArray(recs);108109Arrays.sort(recs, new Comparator<RuleRec>() {110public int compare(RuleRec r1, RuleRec r2) {111int n = r1.getMonthNum() - r2.getMonthNum();112if (n != 0) {113return n;114}115// TODO: it's not accurate to ignore time types (STD, WALL, UTC)116long t1 = Time.getLocalTime(y, r1.getMonth(),117r1.getDay(), r1.getTime().getTime());118long t2 = Time.getLocalTime(y, r2.getMonth(),119r2.getDay(), r2.getTime().getTime());120return Long.compare(t1, t2);121}122public boolean equals(Object o) {123return this == o;124}125});126rules.clear();127for (int i = 0; i < n; i++) {128if (i != 0 && recs[i -1].getSave() == recs[i].getSave()) {129// we have two recs back to back with same saving for the same year.130if (recs[i].isLastRule()) {131continue;132} else if (recs[i - 1].isLastRule()) {133rules.remove(rules.size() - 1);134}135}136rules.add(recs[i]);137}138return rules;139}140141/**142* Gets rule records that have either "max" or cover the endYear143* value in its DST schedule.144*145* @return rules that contain last DST schedule. An empty146* ArrayList is returned if no last rules are found.147*/148List<RuleRec> getLastRules() {149RuleRec start = null;150RuleRec end = null;151152for (int i = 0; i < list.size(); i++) {153RuleRec rec = list.get(i);154if (rec.isLastRule()) {155if (rec.getSave() > 0) {156start = rec;157} else {158end = rec;159}160}161}162if (start == null || end == null) {163int endYear = Zoneinfo.getEndYear();164for (int i = 0; i < list.size(); i++) {165RuleRec rec = list.get(i);166if (endYear >= rec.getFromYear() && endYear <= rec.getToYear()) {167if (start == null && rec.getSave() > 0) {168start = rec;169} else {170if (end == null && rec.getSave() == 0) {171end = rec;172}173}174}175}176}177178List<RuleRec> r = new ArrayList<RuleRec>(2);179if (start == null || end == null) {180if (start != null || end != null) {181Main.warning("found last rules for "+name+" inconsistent.");182}183return r;184}185186r.add(start);187r.add(end);188return r;189}190}191192193