Path: blob/master/test/jdk/java/util/Calendar/Koyomi.java
41149 views
/*1* Copyright (c) 2003, 2016, 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.GregorianCalendar;24import java.util.Locale;25import java.util.TimeZone;2627/**28* GregorianCalendar subclass for testing.29*/30@SuppressWarnings("serial")31public class Koyomi extends GregorianCalendar {3233static final String[] FIELD_NAMES = {34"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH",35"DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR",36"HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",37"DST_OFFSET"38};3940static final int ALL_FIELDS = (1 << FIELD_COUNT) - 1;4142public Koyomi() {43}4445public Koyomi(TimeZone tz) {46super(tz);47}4849public Koyomi(Locale loc) {50super(loc);51}5253public Koyomi(TimeZone tz, Locale loc) {54super(tz, loc);55}5657@Override58public void computeTime() {59super.computeTime();60}6162@Override63public void computeFields() {64super.computeFields();65}6667@Override68public void complete() {69super.complete();70}7172static String getFieldName(int field) {73return FIELD_NAMES[field];74}7576String toDateString() {77StringBuilder sb = new StringBuilder();78sb.append(internalGet(ERA) == 0 ? "BCE " : "");79sb.append(internalGet(YEAR)).append('-');80sb.append(internalGet(MONTH) + 1).append('-');81sb.append(internalGet(DAY_OF_MONTH));82return sb.toString();83}8485String toTimeString() {86StringBuilder sb = new StringBuilder();87sb.append(internalGet(HOUR_OF_DAY)).append(':');88sb.append(internalGet(MINUTE)).append(':');89sb.append(internalGet(SECOND)).append('.');90int ms = internalGet(MILLISECOND);91if (ms < 100) {92sb.append('0');93if (ms < 10) {94sb.append('0');95}96}97sb.append(ms);98int offset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);99offset /= 60000;100offset = (offset / 60) * 100 + (offset % 60);101if (offset >= 0) {102sb.append('+');103} else {104sb.append('-');105offset = -offset;106}107if (offset < 1000) {108sb.append('0');109if (offset < 100) {110sb.append('0');111}112}113sb.append(offset);114return sb.toString();115}116117String toDateTimeString() {118return toDateString() + "T" + toTimeString();119}120121StringBuilder msg = new StringBuilder();122123void initTest() {124msg = new StringBuilder();125}126127String getMessage() {128String s = msg.toString();129msg = new StringBuilder();130return " " + s;131}132133void setMessage(String msg) {134this.msg = new StringBuilder(msg);135}136137void appendMessage(String msg) {138this.msg.append(msg);139}140141boolean getStatus() {142return msg.length() == 0;143}144145int getSetStateFields() {146int mask = 0;147for (int i = 0; i < FIELD_COUNT; i++) {148if (isSet(i)) {149mask |= 1 << i;150}151}152return mask;153}154155int[] getFields() {156int[] fds = new int[fields.length];157System.arraycopy(fields, 0, fds, 0, fds.length);158return fds;159}160161boolean checkAllSet() {162initTest();163for (int i = 0; i < FIELD_COUNT; i++) {164checkFieldState(i, true);165}166return getStatus();167}168169boolean checkInternalDate(int year, int month, int dayOfMonth) {170initTest();171checkInternalFieldValue(YEAR, year);172checkInternalFieldValue(MONTH, month);173checkInternalFieldValue(DAY_OF_MONTH, dayOfMonth);174return getStatus();175}176177boolean checkInternalDate(int year, int month, int dayOfMonth, int dayOfWeek) {178initTest();179checkInternalFieldValue(YEAR, year);180checkInternalFieldValue(MONTH, month);181checkInternalFieldValue(DAY_OF_MONTH, dayOfMonth);182checkInternalFieldValue(DAY_OF_WEEK, dayOfWeek);183return getStatus();184}185186boolean checkActualMaximum(int field, int expectedValue) {187int val;188if ((val = getActualMaximum(field)) != expectedValue) {189appendMessage("getActualMaximum(" + FIELD_NAMES[field] + "): got " + val190+ " expected " + expectedValue);191}192return getStatus();193}194195boolean checkLeastMaximum(int field, int expectedValue) {196int val;197if ((val = getLeastMaximum(field)) != expectedValue) {198appendMessage("getLeastMaximum(" + FIELD_NAMES[field] + "): got " + val199+ " expected " + expectedValue);200}201return getStatus();202}203204boolean checkActualMinimum(int field, int expectedValue) {205int val;206if ((val = getActualMinimum(field)) != expectedValue) {207appendMessage("getActualMinimum(" + FIELD_NAMES[field] + "): got " + val208+ " expected " + expectedValue);209}210return getStatus();211}212213boolean checkGreatestMinimum(int field, int expectedValue) {214int val;215if ((val = getGreatestMinimum(field)) != expectedValue) {216appendMessage("getGreatestMinimum(" + FIELD_NAMES[field] + "): got " + val217+ " expected " + expectedValue);218}219return getStatus();220}221222boolean checkDate(int year, int month, int dayOfMonth) {223initTest();224checkFieldValue(YEAR, year);225checkFieldValue(MONTH, month);226checkFieldValue(DAY_OF_MONTH, dayOfMonth);227return getStatus();228}229230boolean checkDate(int year, int month, int dayOfMonth, int dayOfWeek) {231initTest();232checkFieldValue(YEAR, year);233checkFieldValue(MONTH, month);234checkFieldValue(DAY_OF_MONTH, dayOfMonth);235checkFieldValue(DAY_OF_WEEK, dayOfWeek);236return getStatus();237}238239boolean checkDateTime(int year, int month, int dayOfMonth,240int hourOfDay, int minute, int second, int ms) {241initTest();242checkFieldValue(YEAR, year);243checkFieldValue(MONTH, month);244checkFieldValue(DAY_OF_MONTH, dayOfMonth);245checkFieldValue(HOUR_OF_DAY, hourOfDay);246checkFieldValue(MINUTE, minute);247checkFieldValue(SECOND, second);248checkFieldValue(MILLISECOND, ms);249return getStatus();250}251252boolean checkTime(int hourOfDay, int minute, int second, int ms) {253initTest();254checkFieldValue(HOUR_OF_DAY, hourOfDay);255checkFieldValue(MINUTE, minute);256checkFieldValue(SECOND, second);257checkFieldValue(MILLISECOND, ms);258return getStatus();259}260261boolean checkFieldState(int field, boolean expectedState) {262if (isSet(field) != expectedState) {263appendMessage(FIELD_NAMES[field] + " state is not " + expectedState + "; ");264return false;265}266return true;267}268269boolean checkFieldValue(int field, int expectedValue) {270int val;271if ((val = get(field)) != expectedValue) {272appendMessage("get(" + FIELD_NAMES[field] + "): got " + val273+ ", expected " + expectedValue + "; ");274return false;275}276return true;277}278279boolean checkInternalFieldValue(int field, int expectedValue) {280int val;281if ((val = internalGet(field)) != expectedValue) {282appendMessage("internalGet(" + FIELD_NAMES[field] + "): got " + val283+ ", expected " + expectedValue + "; ");284return false;285}286return true;287}288}289290291