1 /*
2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.util.GregorianCalendar;
25 import java.util.Locale;
26 import java.util.TimeZone;
27 
28 /**
29  * GregorianCalendar subclass for testing.
30  */
31 @SuppressWarnings("serial")
32 public class Koyomi extends GregorianCalendar {
33 
34     static final String[] FIELD_NAMES = {
35         "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH",
36         "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR",
37         "HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
38         "DST_OFFSET"
39     };
40 
41     static final int ALL_FIELDS = (1 << FIELD_COUNT) - 1;
42 
Koyomi()43     public Koyomi() {
44     }
45 
Koyomi(TimeZone tz)46     public Koyomi(TimeZone tz) {
47         super(tz);
48     }
49 
Koyomi(Locale loc)50     public Koyomi(Locale loc) {
51         super(loc);
52     }
53 
Koyomi(TimeZone tz, Locale loc)54     public Koyomi(TimeZone tz, Locale loc) {
55         super(tz, loc);
56     }
57 
58     @Override
computeTime()59     public void computeTime() {
60         super.computeTime();
61     }
62 
63     @Override
computeFields()64     public void computeFields() {
65         super.computeFields();
66     }
67 
68     @Override
complete()69     public void complete() {
70         super.complete();
71     }
72 
getFieldName(int field)73     static String getFieldName(int field) {
74         return FIELD_NAMES[field];
75     }
76 
toDateString()77     String toDateString() {
78         StringBuilder sb = new StringBuilder();
79         sb.append(internalGet(ERA) == 0 ? "BCE " : "");
80         sb.append(internalGet(YEAR)).append('-');
81         sb.append(internalGet(MONTH) + 1).append('-');
82         sb.append(internalGet(DAY_OF_MONTH));
83         return sb.toString();
84     }
85 
toTimeString()86     String toTimeString() {
87         StringBuilder sb = new StringBuilder();
88         sb.append(internalGet(HOUR_OF_DAY)).append(':');
89         sb.append(internalGet(MINUTE)).append(':');
90         sb.append(internalGet(SECOND)).append('.');
91         int ms = internalGet(MILLISECOND);
92         if (ms < 100) {
93             sb.append('0');
94             if (ms < 10) {
95                 sb.append('0');
96             }
97         }
98         sb.append(ms);
99         int offset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);
100         offset /= 60000;
101         offset = (offset / 60) * 100 + (offset % 60);
102         if (offset >= 0) {
103             sb.append('+');
104         } else {
105             sb.append('-');
106             offset = -offset;
107         }
108         if (offset < 1000) {
109             sb.append('0');
110             if (offset < 100) {
111                 sb.append('0');
112             }
113         }
114         sb.append(offset);
115         return sb.toString();
116     }
117 
toDateTimeString()118     String toDateTimeString() {
119         return toDateString() + "T" + toTimeString();
120     }
121 
122     StringBuilder msg = new StringBuilder();
123 
initTest()124     void initTest() {
125         msg = new StringBuilder();
126     }
127 
getMessage()128     String getMessage() {
129         String s = msg.toString();
130         msg = new StringBuilder();
131         return "    " + s;
132     }
133 
setMessage(String msg)134     void setMessage(String msg) {
135         this.msg = new StringBuilder(msg);
136     }
137 
appendMessage(String msg)138     void appendMessage(String msg) {
139         this.msg.append(msg);
140     }
141 
getStatus()142     boolean getStatus() {
143         return msg.length() == 0;
144     }
145 
getSetStateFields()146     int getSetStateFields() {
147         int mask = 0;
148         for (int i = 0; i < FIELD_COUNT; i++) {
149             if (isSet(i)) {
150                 mask |= 1 << i;
151             }
152         }
153         return mask;
154     }
155 
getFields()156     int[] getFields() {
157         int[] fds = new int[fields.length];
158         System.arraycopy(fields, 0, fds, 0, fds.length);
159         return fds;
160     }
161 
checkAllSet()162     boolean checkAllSet() {
163         initTest();
164         for (int i = 0; i < FIELD_COUNT; i++) {
165             checkFieldState(i, true);
166         }
167         return getStatus();
168     }
169 
checkInternalDate(int year, int month, int dayOfMonth)170     boolean checkInternalDate(int year, int month, int dayOfMonth) {
171         initTest();
172         checkInternalFieldValue(YEAR, year);
173         checkInternalFieldValue(MONTH, month);
174         checkInternalFieldValue(DAY_OF_MONTH, dayOfMonth);
175         return getStatus();
176     }
177 
checkInternalDate(int year, int month, int dayOfMonth, int dayOfWeek)178     boolean checkInternalDate(int year, int month, int dayOfMonth, int dayOfWeek) {
179         initTest();
180         checkInternalFieldValue(YEAR, year);
181         checkInternalFieldValue(MONTH, month);
182         checkInternalFieldValue(DAY_OF_MONTH, dayOfMonth);
183         checkInternalFieldValue(DAY_OF_WEEK, dayOfWeek);
184         return getStatus();
185     }
186 
checkActualMaximum(int field, int expectedValue)187     boolean checkActualMaximum(int field, int expectedValue) {
188         int val;
189         if ((val = getActualMaximum(field)) != expectedValue) {
190             appendMessage("getActualMaximum(" + FIELD_NAMES[field] + "): got " + val
191                     + " expected " + expectedValue);
192         }
193         return getStatus();
194     }
195 
checkLeastMaximum(int field, int expectedValue)196     boolean checkLeastMaximum(int field, int expectedValue) {
197         int val;
198         if ((val = getLeastMaximum(field)) != expectedValue) {
199             appendMessage("getLeastMaximum(" + FIELD_NAMES[field] + "): got " + val
200                     + " expected " + expectedValue);
201         }
202         return getStatus();
203     }
204 
checkActualMinimum(int field, int expectedValue)205     boolean checkActualMinimum(int field, int expectedValue) {
206         int val;
207         if ((val = getActualMinimum(field)) != expectedValue) {
208             appendMessage("getActualMinimum(" + FIELD_NAMES[field] + "): got " + val
209                     + " expected " + expectedValue);
210         }
211         return getStatus();
212     }
213 
checkGreatestMinimum(int field, int expectedValue)214     boolean checkGreatestMinimum(int field, int expectedValue) {
215         int val;
216         if ((val = getGreatestMinimum(field)) != expectedValue) {
217             appendMessage("getGreatestMinimum(" + FIELD_NAMES[field] + "): got " + val
218                     + " expected " + expectedValue);
219         }
220         return getStatus();
221     }
222 
checkDate(int year, int month, int dayOfMonth)223     boolean checkDate(int year, int month, int dayOfMonth) {
224         initTest();
225         checkFieldValue(YEAR, year);
226         checkFieldValue(MONTH, month);
227         checkFieldValue(DAY_OF_MONTH, dayOfMonth);
228         return getStatus();
229     }
230 
checkDate(int year, int month, int dayOfMonth, int dayOfWeek)231     boolean checkDate(int year, int month, int dayOfMonth, int dayOfWeek) {
232         initTest();
233         checkFieldValue(YEAR, year);
234         checkFieldValue(MONTH, month);
235         checkFieldValue(DAY_OF_MONTH, dayOfMonth);
236         checkFieldValue(DAY_OF_WEEK, dayOfWeek);
237         return getStatus();
238     }
239 
checkDateTime(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second, int ms)240     boolean checkDateTime(int year, int month, int dayOfMonth,
241             int hourOfDay, int minute, int second, int ms) {
242         initTest();
243         checkFieldValue(YEAR, year);
244         checkFieldValue(MONTH, month);
245         checkFieldValue(DAY_OF_MONTH, dayOfMonth);
246         checkFieldValue(HOUR_OF_DAY, hourOfDay);
247         checkFieldValue(MINUTE, minute);
248         checkFieldValue(SECOND, second);
249         checkFieldValue(MILLISECOND, ms);
250         return getStatus();
251     }
252 
checkTime(int hourOfDay, int minute, int second, int ms)253     boolean checkTime(int hourOfDay, int minute, int second, int ms) {
254         initTest();
255         checkFieldValue(HOUR_OF_DAY, hourOfDay);
256         checkFieldValue(MINUTE, minute);
257         checkFieldValue(SECOND, second);
258         checkFieldValue(MILLISECOND, ms);
259         return getStatus();
260     }
261 
checkFieldState(int field, boolean expectedState)262     boolean checkFieldState(int field, boolean expectedState) {
263         if (isSet(field) != expectedState) {
264             appendMessage(FIELD_NAMES[field] + " state is not " + expectedState + "; ");
265             return false;
266         }
267         return true;
268     }
269 
checkFieldValue(int field, int expectedValue)270     boolean checkFieldValue(int field, int expectedValue) {
271         int val;
272         if ((val = get(field)) != expectedValue) {
273             appendMessage("get(" + FIELD_NAMES[field] + "): got " + val
274                     + ", expected " + expectedValue + "; ");
275             return false;
276         }
277         return true;
278     }
279 
checkInternalFieldValue(int field, int expectedValue)280     boolean checkInternalFieldValue(int field, int expectedValue) {
281         int val;
282         if ((val = internalGet(field)) != expectedValue) {
283             appendMessage("internalGet(" + FIELD_NAMES[field] + "): got " + val
284                     + ", expected " + expectedValue + "; ");
285             return false;
286         }
287         return true;
288     }
289 }
290