@Test public void test_getDayOfYear() { // Test all the Eras for (JapaneseEra era : JapaneseEra.values()) { int firstYear = (era == JapaneseEra.MEIJI) ? 6 : 1; // Until Era supports range(YEAR_OF_ERA) JapaneseDate hd1 = JapaneseChronology.INSTANCE.dateYearDay(era, firstYear, 1); ValueRange range = hd1.range(DAY_OF_YEAR); assertEquals(range.getMaximum(), hd1.lengthOfYear(), "lengthOfYear should match range.getMaximum()"); for (int i = 1; i <= hd1.lengthOfYear(); i++) { JapaneseDate hd = JapaneseChronology.INSTANCE.dateYearDay(era, firstYear, i); int doy = hd.get(DAY_OF_YEAR); assertEquals(doy, i, "get(DAY_OF_YEAR) incorrect for " + i + ", of date: " + hd); } } }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case YEAR_OF_ERA: { Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE); jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET); jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth()); return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR)); } } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); // TODO // TODO does the limited range of valid years cause years to // start/end part way through? that would affect range } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
ChronoLocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR); if (resolverStyle == ResolverStyle.LENIENT) { long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1); long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1); return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS); } int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR); ValueRange domRange = range(DAY_OF_MONTH); int dom = domRange.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH); if (resolverStyle == ResolverStyle.SMART) { // previous valid try { return date(y, moy, dom); } catch (DateTimeException ex) { return date(y, moy, 1).with(TemporalAdjusters.lastDayOfMonth()); } } return date(y, moy, dom); }
@Override public ValueRange range(ChronoField field) { checkCalendarInit(); if (field instanceof ChronoField) { ChronoField f = field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, 1, getMinimumMonthLength(), getMaximumMonthLength()); case DAY_OF_YEAR: return ValueRange.of(1, getMaximumDayOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); case YEAR: case YEAR_OF_ERA: return ValueRange.of(getMinimumYear(), getMaximumYear()); case ERA: return ValueRange.of(1, 1); default: return field.range(); } } return field.range(); }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, month == 13 ? 1 : 5); case YEAR: case YEAR_OF_ERA: return (prolepticYear <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE)); // TODO } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
@Test(dataProvider="weekFields") public void test_rangeWeekOfWeekBasedYear(DayOfWeek firstDayOfWeek, int minDays) { WeekFields weekFields = WeekFields.of(firstDayOfWeek, minDays); TemporalField dowField = weekFields.dayOfWeek(); TemporalField wowByField = weekFields.weekOfWeekBasedYear(); LocalDate day1 = LocalDate.of(2012, 1, weekFields.getMinimalDaysInFirstWeek()); day1 = day1.with(wowByField, 1).with(dowField, 1); LocalDate day2 = LocalDate.of(2013, 1, weekFields.getMinimalDaysInFirstWeek()); day2 = day2.with(wowByField, 1).with(dowField, 1); int expectedWeeks = (int)ChronoUnit.DAYS.between(day1, day2) / 7; ValueRange range = day1.range(wowByField); assertEquals(range.getMaximum(), expectedWeeks, "Range incorrect"); }
@Test(dataProvider = "fields") public void test_WOWBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField) { assertEquals(weekField.rangeRefinedBy(LocalDate.of(2012, 12, 31)), ValueRange.of(1, 52)); assertEquals(weekField.rangeRefinedBy(LocalDate.of(2013, 12, 29)), ValueRange.of(1, 52)); assertEquals(weekField.rangeRefinedBy(LocalDate.of(2013, 12, 30)), ValueRange.of(1, 52)); assertEquals(weekField.rangeRefinedBy(LocalDate.of(2014, 12, 28)), ValueRange.of(1, 52)); assertEquals(weekField.rangeRefinedBy(LocalDate.of(2014, 12, 29)), ValueRange.of(1, 53)); assertEquals(weekField.rangeRefinedBy(LocalDate.of(2016, 1, 3)), ValueRange.of(1, 53)); assertEquals(weekField.rangeRefinedBy(LocalDate.of(2016, 1, 4)), ValueRange.of(1, 52)); }
@Override default ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) { return field.range(); } return toLocalDateTime().range(field); } return field.rangeRefinedBy(this); }
/** * Calculates the range of months. * * @return the month range, -1 if not fixed range */ private long monthRange() { ValueRange startRange = chrono.range(MONTH_OF_YEAR); if (startRange.isFixed() && startRange.isIntValue()) { return startRange.getMaximum() - startRange.getMinimum() + 1; } return -1; }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { return field.range(); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
private long monthsUntil(ChronoLocalDate end) { ValueRange range = getChronology().range(MONTH_OF_YEAR); if (range.getMaximum() != 12) { throw new IllegalStateException("ChronoLocalDateImpl only supports Chronologies with 12 months per year"); } long packed1 = getLong(PROLEPTIC_MONTH) * 32L + get(DAY_OF_MONTH); // no overflow long packed2 = end.getLong(PROLEPTIC_MONTH) * 32L + end.get(DAY_OF_MONTH); // no overflow return (packed2 - packed1) / 32; }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; return (f.isTimeBased() ? time.range(field) : date.range(field)); } return field.rangeRefinedBy(this); }
public void test_loop() { // loop round at least one 400 year cycle, including before 1970 LocalDate date = LocalDate.of(1960, 1, 5); // Tuseday of week 1 1960 int year = 1960; int wby = 1960; int weekLen = 52; int week = 1; while (date.getYear() < 2400) { DayOfWeek loopDow = date.getDayOfWeek(); if (date.getYear() != year) { year = date.getYear(); } if (loopDow == MONDAY) { week++; if ((week == 53 && weekLen == 52) || week == 54) { week = 1; LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1); DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek(); weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52); wby++; } } assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek()); assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week, "Failed on " + date + " " + date.getDayOfWeek()); assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek()); assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby, "Failed on " + date + " " + date.getDayOfWeek()); assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek()); date = date.plusDays(1); } }
public void test_of_longlong() { ValueRange test = ValueRange.of(1, 12); assertEquals(test.getMinimum(), 1); assertEquals(test.getLargestMinimum(), 1); assertEquals(test.getSmallestMaximum(), 12); assertEquals(test.getMaximum(), 12); assertEquals(test.isFixed(), true); assertEquals(test.isIntValue(), true); }
/** * Creates an instance. * * @param prolepticYear the Coptic proleptic-year * @param month the Coptic month, from 1 to 13 * @param dayOfMonth the Coptic day-of-month, from 1 to 30 * @throws DateTimeException if the date is invalid */ CopticDate(int prolepticYear, int month, int dayOfMonth) { CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR); ValueRange range; if (month == 13) { range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP; } else { range = CopticChronology.DOM_RANGE; } range.checkValidValue(dayOfMonth, DAY_OF_MONTH); this.prolepticYear = prolepticYear; this.month = (short) month; this.day = (short) dayOfMonth; }
@Test(dataProvider="weekFields") public void test_fieldRanges(DayOfWeek firstDayOfWeek, int minDays) { WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays); TemporalField womField = weekDef.weekOfMonth(); TemporalField woyField = weekDef.weekOfYear(); LocalDate day = LocalDate.of(2012, 11, 30); LocalDate endDay = LocalDate.of(2013, 1, 2); while (day.isBefore(endDay)) { LocalDate last = day.with(DAY_OF_MONTH, day.lengthOfMonth()); int lastWOM = last.get(womField); LocalDate first = day.with(DAY_OF_MONTH, 1); int firstWOM = first.get(womField); ValueRange rangeWOM = day.range(womField); assertEquals(rangeWOM.getMinimum(), firstWOM, "Range min should be same as WeekOfMonth for first day of month: " + first + ", " + weekDef); assertEquals(rangeWOM.getMaximum(), lastWOM, "Range max should be same as WeekOfMonth for last day of month: " + last + ", " + weekDef); last = day.with(DAY_OF_YEAR, day.lengthOfYear()); int lastWOY = last.get(woyField); first = day.with(DAY_OF_YEAR, 1); int firstWOY = first.get(woyField); ValueRange rangeWOY = day.range(woyField); assertEquals(rangeWOY.getMinimum(), firstWOY, "Range min should be same as WeekOfYear for first day of Year: " + day + ", " + weekDef); assertEquals(rangeWOY.getMaximum(), lastWOY, "Range max should be same as WeekOfYear for last day of Year: " + day + ", " + weekDef); day = day.plusDays(1); } }
@Test (dataProvider="monthDays") public void test_valueRange_monthDays(int year, int month, int maxlength) { ChronoLocalDate date = HijrahChronology.INSTANCE.date(year, month, 1); ValueRange range = null; for (int i=1; i<=12; i++) { range = date.range(ChronoField.DAY_OF_MONTH); date = date.plus(1, ChronoUnit.MONTHS); assertEquals(range.getMaximum(), month, maxlength); } }
@Override public ValueRange range(ChronoField field) { switch (field) { case DAY_OF_MONTH: return ValueRange.of(1, 5, 30); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 1, 5); case MONTH_OF_YEAR: return ValueRange.of(1, 13); case PROLEPTIC_MONTH: return ValueRange.of(-1000, 1000); // TODO case YEAR_OF_ERA: return ValueRange.of(1, 999, 1000); // TODO case YEAR: return ValueRange.of(-1000, 1000); // TODO } return field.range(); }
/** * Verify Serialized bytes of a ValueRange. * @throws IOException if thrown during serialization is an unexpected test tailure */ public void test_valueRangeSerialized() throws IOException { byte[] expected = { (byte)172, (byte)237, 0, 5, 115, 114, 0, 29, 106, 97, /* \u00ac \u00ed \u0000 \u0005 s r \u0000 \u001d j a */ 118, 97, 46, 116, 105, 109, 101, 46, 116, 101, /* v a . t i m e . t e */ 109, 112, 111, 114, 97, 108, 46, 86, 97, 108, /* m p o r a l . V a l */ 117, 101, 82, 97, 110, 103, 101, (byte)154, 113, (byte)169, /* u e R a n g e \u009a q \u00a9 */ 86, (byte)242, (byte)205, 90, (byte)184, 2, 0, 4, 74, 0, /* V \u00f2 \u00cd Z \u00b8 \u0002 \u0000 \u0004 J \u0000 */ 10, 109, 97, 120, 76, 97, 114, 103, 101, 115, /* m a x L a r g e s */ 116, 74, 0, 11, 109, 97, 120, 83, 109, 97, /* t J \u0000 \u000b m a x S m a */ 108, 108, 101, 115, 116, 74, 0, 10, 109, 105,/* l l e s t J \u0000 m i */ 110, 76, 97, 114, 103, 101, 115, 116, 74, 0, /* n L a r g e s t J \u0000 */ 11, 109, 105, 110, 83, 109, 97, 108, 108, 101, /* \u000b m i n S m a l l e */ 115, 116, 120, 112, 0, 0, 0, 0, 0, 0, /* s t x p \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 */ 0, 40, 0, 0, 0, 0, 0, 0, 0, 30, /* \u0000 ( \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 \u001e */ 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, /* \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 \u0014 \u0000 \u0000 */ 0, 0, 0, 0, 0, 10, /* \u0000 \u0000 \u0000 \u0000 \u0000 */ }; ValueRange range = ValueRange.of(10, 20, 30, 40); try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos) ) { oos.writeObject(range); byte[] actual = baos.toByteArray(); assertEquals(actual, expected, "Serialized bytes incorrect"); } }
@Test public void test_Chrono_range() { long minYear = LocalDate.MIN.getYear() + YDIFF; long maxYear = LocalDate.MAX.getYear() + YDIFF; assertEquals(ThaiBuddhistChronology.INSTANCE.range(YEAR), ValueRange.of(minYear, maxYear)); assertEquals(ThaiBuddhistChronology.INSTANCE.range(YEAR_OF_ERA), ValueRange.of(1, -minYear + 1, maxYear)); assertEquals(ThaiBuddhistChronology.INSTANCE.range(DAY_OF_MONTH), DAY_OF_MONTH.range()); assertEquals(ThaiBuddhistChronology.INSTANCE.range(DAY_OF_YEAR), DAY_OF_YEAR.range()); assertEquals(ThaiBuddhistChronology.INSTANCE.range(MONTH_OF_YEAR), MONTH_OF_YEAR.range()); }
public void test_of_longlong_big() { ValueRange test = ValueRange.of(1, 123456789012345L); assertEquals(test.getMinimum(), 1); assertEquals(test.getLargestMinimum(), 1); assertEquals(test.getSmallestMaximum(), 123456789012345L); assertEquals(test.getMaximum(), 123456789012345L); assertEquals(test.isFixed(), true); assertEquals(test.isIntValue(), false); }
public void test_of_longlonglong() { ValueRange test = ValueRange.of(1, 28, 31); assertEquals(test.getMinimum(), 1); assertEquals(test.getLargestMinimum(), 1); assertEquals(test.getSmallestMaximum(), 28); assertEquals(test.getMaximum(), 31); assertEquals(test.isFixed(), false); assertEquals(test.isIntValue(), true); }
@Test(dataProvider="valid") public void test_of_longlonglonglong(long sMin, long lMin, long sMax, long lMax) { ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax); assertEquals(test.getMinimum(), sMin); assertEquals(test.getLargestMinimum(), lMin); assertEquals(test.getSmallestMaximum(), sMax); assertEquals(test.getMaximum(), lMax); assertEquals(test.isFixed(), sMin == lMin && sMax == lMax); assertEquals(test.isIntValue(), true); }
public void test_equals1() { ValueRange a = ValueRange.of(1, 2, 3, 4); ValueRange b = ValueRange.of(1, 2, 3, 4); assertEquals(a.equals(a), true); assertEquals(a.equals(b), true); assertEquals(b.equals(a), true); assertEquals(b.equals(b), true); assertEquals(a.hashCode() == b.hashCode(), true); }
@Override public ValueRange rangeRefinedBy(TemporalAccessor temporal) { throw new UnsupportedOperationException("Not supported yet."); }