Path: blob/master/test/jdk/sun/util/calendar/zi/Rule.java
41153 views
/*1* Copyright (c) 2000, 2018, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;24import java.util.Arrays;25import java.util.Comparator;26import java.util.List;27import java.util.StringTokenizer;2829/**30* Rule manipulates Rule records.31*32* @since 1.433*/34class Rule {3536private List<RuleRec> list;37private String name;3839/**40* Constructs a Rule which consists of a Rule record list. The41* specified name is given to this Rule.42* @param name the Rule name43*/44Rule(String name) {45this.name = name;46list = new ArrayList<RuleRec>();47}4849/**50* Added a RuleRec to the Rule record list.51*/52void add(RuleRec rec) {53list.add(rec);54}5556/**57* @return the Rule name58*/59String getName() {60return name;61}6263/**64* Gets all rule records that cover the given year.65*66* @param year the year number for which the rule is applicable.67* @return rules in List that are collated in time. If no rule is found, an empty68* List is returned.69*/70List<RuleRec> getRules(int year) {71List<RuleRec> rules = new ArrayList<RuleRec>(3);72for (RuleRec rec : list) {73if (year >= rec.getFromYear() && year <= rec.getToYear()) {74if ((rec.isOdd() && year % 2 == 0) || (rec.isEven() && year % 2 == 1))75continue;76rules.add(rec);77}78}79int n = rules.size();80if (n <= 1) {81return rules;82}83if (n == 2) {84RuleRec rec1 = rules.get(0);85RuleRec rec2 = rules.get(1);86if (rec1.getMonthNum() > rec2.getMonthNum()) {87rules.set(0, rec2);88rules.set(1, rec1);89} else if (rec1.getMonthNum() == rec2.getMonthNum()) {90// TODO: it's not accurate to ignore time types (STD, WALL, UTC)91long t1 = Time.getLocalTime(year, rec1.getMonth(),92rec1.getDay(), rec1.getTime().getTime());93long t2 = Time.getLocalTime(year, rec2.getMonth(),94rec2.getDay(), rec2.getTime().getTime());95if (t1 > t2) {96rules.set(0, rec2);97rules.set(1, rec1);98}99}100return rules;101}102103final int y = year;104RuleRec[] recs = new RuleRec[rules.size()];105rules.toArray(recs);106107Arrays.sort(recs, new Comparator<RuleRec>() {108public int compare(RuleRec r1, RuleRec r2) {109int n = r1.getMonthNum() - r2.getMonthNum();110if (n != 0) {111return n;112}113// TODO: it's not accurate to ignore time types (STD, WALL, UTC)114long t1 = Time.getLocalTime(y, r1.getMonth(),115r1.getDay(), r1.getTime().getTime());116long t2 = Time.getLocalTime(y, r2.getMonth(),117r2.getDay(), r2.getTime().getTime());118return Long.compare(t1, t2);119}120public boolean equals(Object o) {121return this == o;122}123});124rules.clear();125for (int i = 0; i < n; i++) {126if (i != 0 && recs[i -1].getSave() == recs[i].getSave()) {127// we have two recs back to back with same saving for the same year.128if (recs[i].isLastRule()) {129continue;130} else if (recs[i - 1].isLastRule()) {131rules.remove(rules.size() - 1);132}133}134rules.add(recs[i]);135}136return rules;137}138139/**140* Gets rule records that have either "max" or cover the endYear141* value in its DST schedule.142*143* @return rules that contain last DST schedule. An empty144* ArrayList is returned if no last rules are found.145*/146List<RuleRec> getLastRules() {147RuleRec start = null;148RuleRec end = null;149150for (int i = 0; i < list.size(); i++) {151RuleRec rec = list.get(i);152if (rec.isLastRule()) {153if (rec.getSave() > 0) {154start = rec;155} else {156end = rec;157}158}159}160if (start == null || end == null) {161int endYear = Zoneinfo.getEndYear();162for (int i = 0; i < list.size(); i++) {163RuleRec rec = list.get(i);164if (endYear >= rec.getFromYear() && endYear <= rec.getToYear()) {165if (start == null && rec.getSave() > 0) {166start = rec;167} else {168if (end == null && rec.getSave() == 0) {169end = rec;170}171}172}173}174}175176List<RuleRec> r = new ArrayList<RuleRec>(2);177if (start == null || end == null) {178if (start != null || end != null) {179Main.warning("found last rules for "+name+" inconsistent.");180}181return r;182}183184r.add(start);185r.add(end);186return r;187}188}189190191