8279185: Support for IsoFields in JapaneseDate/MinguoDate/ThaiBuddhistDate

Reviewed-by: joehw, rriggs
This commit is contained in:
Naoto Sato 2022-05-23 16:20:52 +00:00
parent ac274c4ca6
commit ef7a9f8170
13 changed files with 272 additions and 38 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -76,6 +76,7 @@ import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle; import java.time.format.ResolverStyle;
import java.time.format.TextStyle; import java.time.format.TextStyle;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField; import java.time.temporal.TemporalField;
import java.time.temporal.TemporalQueries; import java.time.temporal.TemporalQueries;
@ -736,7 +737,7 @@ public interface Chronology extends Comparable<Chronology> {
* @throws DateTimeException if any of the values are out of range * @throws DateTimeException if any of the values are out of range
* @since 9 * @since 9
*/ */
public default long epochSecond(int prolepticYear, int month, int dayOfMonth, default long epochSecond(int prolepticYear, int month, int dayOfMonth,
int hour, int minute, int second, ZoneOffset zoneOffset) { int hour, int minute, int second, ZoneOffset zoneOffset) {
Objects.requireNonNull(zoneOffset, "zoneOffset"); Objects.requireNonNull(zoneOffset, "zoneOffset");
HOUR_OF_DAY.checkValidValue(hour); HOUR_OF_DAY.checkValidValue(hour);
@ -765,11 +766,38 @@ public interface Chronology extends Comparable<Chronology> {
* @throws DateTimeException if any of the values are out of range * @throws DateTimeException if any of the values are out of range
* @since 9 * @since 9
*/ */
public default long epochSecond(Era era, int yearOfEra, int month, int dayOfMonth, default long epochSecond(Era era, int yearOfEra, int month, int dayOfMonth,
int hour, int minute, int second, ZoneOffset zoneOffset) { int hour, int minute, int second, ZoneOffset zoneOffset) {
Objects.requireNonNull(era, "era"); Objects.requireNonNull(era, "era");
return epochSecond(prolepticYear(era, yearOfEra), month, dayOfMonth, hour, minute, second, zoneOffset); return epochSecond(prolepticYear(era, yearOfEra), month, dayOfMonth, hour, minute, second, zoneOffset);
} }
/**
* Checks if this chronology is ISO based.
* <p>
* An ISO based chronology has the same basic structure as the {@link IsoChronology
* ISO chronology}, i.e., the chronology has the same number of months, the number
* of days in each month, and day-of-year and leap years are the same as ISO chronology.
* It also supports the concept of week-based-year of ISO chronology.
* For example, the {@link MinguoChronology Minguo}, {@link ThaiBuddhistChronology
* ThaiThaiBuddhist} and {@link JapaneseChronology Japanese} chronologies are ISO based.
*
* @implSpec
* The default implementation returns {@code false}.
*
* @return {@code true} only if all the fields of {@link IsoFields} are supported by
* this chronology. Otherwise, returns {@code false}.
* @see IsoChronology
* @see JapaneseChronology
* @see MinguoChronology
* @see ThaiBuddhistChronology
* @see IsoFields
* @since 19
*/
default boolean isIsoBased() {
return false;
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Compares this chronology to another chronology. * Compares this chronology to another chronology.

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -87,6 +87,7 @@ import java.time.ZoneId;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.format.ResolverStyle; import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField; import java.time.temporal.TemporalField;
import java.time.temporal.ValueRange; import java.time.temporal.ValueRange;
@ -677,6 +678,20 @@ public final class IsoChronology extends AbstractChronology implements Serializa
return Period.of(years, months, days); return Period.of(years, months, days);
} }
//-----------------------------------------------------------------------
/**
* {@code IsoChronology} is an ISO based chronology, which supports fields
* in {@link IsoFields}, such as {@link IsoFields#DAY_OF_QUARTER DAY_OF_QUARTER}
* and {@link IsoFields#QUARTER_OF_YEAR QUARTER_OF_YEAR}.
* @see IsoFields
* @return {@code true}
* @since 19
*/
@Override
public boolean isIsoBased() {
return true;
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Writes the Chronology using a * Writes the Chronology using a

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -76,6 +76,7 @@ import java.time.Year;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.ResolverStyle; import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField; import java.time.temporal.TemporalField;
@ -504,6 +505,20 @@ public final class JapaneseChronology extends AbstractChronology implements Seri
return dateYearDay(era, yoe, doy); // smart is same as strict return dateYearDay(era, yoe, doy); // smart is same as strict
} }
//-----------------------------------------------------------------------
/**
* {@code JapaneseChronology} is an ISO based chronology, which supports fields
* in {@link IsoFields}, such as {@link IsoFields#DAY_OF_QUARTER DAY_OF_QUARTER}
* and {@link IsoFields#QUARTER_OF_YEAR QUARTER_OF_YEAR}.
* @see IsoFields
* @return {@code true}
* @since 19
*/
@Override
public boolean isIsoBased() {
return true;
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Writes the Chronology using a * Writes the Chronology using a

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -465,13 +465,13 @@ public final class JapaneseDate
@Override @Override
public long getLong(TemporalField field) { public long getLong(TemporalField field) {
if (field instanceof ChronoField) { if (field instanceof ChronoField cf) {
// same as ISO: // same as ISO:
// DAY_OF_WEEK, DAY_OF_MONTH, EPOCH_DAY, MONTH_OF_YEAR, PROLEPTIC_MONTH, YEAR // DAY_OF_WEEK, DAY_OF_MONTH, EPOCH_DAY, MONTH_OF_YEAR, PROLEPTIC_MONTH, YEAR
// //
// calendar specific fields // calendar specific fields
// DAY_OF_YEAR, YEAR_OF_ERA, ERA // DAY_OF_YEAR, YEAR_OF_ERA, ERA
switch ((ChronoField) field) { switch (cf) {
case ALIGNED_DAY_OF_WEEK_IN_MONTH: case ALIGNED_DAY_OF_WEEK_IN_MONTH:
case ALIGNED_DAY_OF_WEEK_IN_YEAR: case ALIGNED_DAY_OF_WEEK_IN_YEAR:
case ALIGNED_WEEK_OF_MONTH: case ALIGNED_WEEK_OF_MONTH:

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -69,6 +69,7 @@ import java.time.LocalDate;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.ResolverStyle; import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField; import java.time.temporal.TemporalField;
import java.time.temporal.ValueRange; import java.time.temporal.ValueRange;
@ -335,6 +336,20 @@ public final class MinguoChronology extends AbstractChronology implements Serial
return (MinguoDate) super.resolveDate(fieldValues, resolverStyle); return (MinguoDate) super.resolveDate(fieldValues, resolverStyle);
} }
//-----------------------------------------------------------------------
/**
* {@code MinguoChronology} is an ISO based chronology, which supports fields
* in {@link IsoFields}, such as {@link IsoFields#DAY_OF_QUARTER DAY_OF_QUARTER}
* and {@link IsoFields#QUARTER_OF_YEAR QUARTER_OF_YEAR}.
* @see IsoFields
* @return {@code true}
* @since 19
*/
@Override
public boolean isIsoBased() {
return true;
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Writes the Chronology using a * Writes the Chronology using a

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -69,6 +69,7 @@ import java.time.LocalDate;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.ResolverStyle; import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField; import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField; import java.time.temporal.TemporalField;
import java.time.temporal.ValueRange; import java.time.temporal.ValueRange;
@ -335,6 +336,20 @@ public final class ThaiBuddhistChronology extends AbstractChronology implements
return (ThaiBuddhistDate) super.resolveDate(fieldValues, resolverStyle); return (ThaiBuddhistDate) super.resolveDate(fieldValues, resolverStyle);
} }
//-----------------------------------------------------------------------
/**
* {@code ThaiBuddhistChronology} is an ISO based chronology, which supports fields
* in {@link IsoFields}, such as {@link IsoFields#DAY_OF_QUARTER DAY_OF_QUARTER}
* and {@link IsoFields#QUARTER_OF_YEAR QUARTER_OF_YEAR}.
* @see IsoFields
* @return {@code true}
* @since 19
*/
@Override
public boolean isIsoBased() {
return true;
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Writes the Chronology using a * Writes the Chronology using a

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -546,7 +546,10 @@ public final class IsoFields {
if (isSupportedBy(temporal) == false) { if (isSupportedBy(temporal) == false) {
throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear"); throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear");
} }
return super.rangeRefinedBy(temporal); var range = super.rangeRefinedBy(temporal);
var chronoRange = Chronology.from(temporal).range(YEAR);
return ValueRange.of(Math.max(range.getMinimum(), chronoRange.getMinimum()),
Math.min(range.getMaximum(), chronoRange.getMaximum()));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
@ -591,12 +594,6 @@ public final class IsoFields {
private static final int[] QUARTER_DAYS = {0, 90, 181, 273, 0, 91, 182, 274}; private static final int[] QUARTER_DAYS = {0, 90, 181, 273, 0, 91, 182, 274};
private static void ensureIso(TemporalAccessor temporal) {
if (isIso(temporal) == false) {
throw new DateTimeException("Resolve requires IsoChronology");
}
}
private static ValueRange getWeekRange(LocalDate date) { private static ValueRange getWeekRange(LocalDate date) {
int wby = getWeekBasedYear(date); int wby = getWeekBasedYear(date);
return ValueRange.of(1, getWeekRange(wby)); return ValueRange.of(1, getWeekRange(wby));
@ -731,7 +728,14 @@ public final class IsoFields {
} }
} }
static boolean isIso(TemporalAccessor temporal) { private static void ensureIso(TemporalAccessor temporal) {
return Chronology.from(temporal).equals(IsoChronology.INSTANCE); if (!isIso(temporal)) {
throw new DateTimeException("Resolve requires ISO based chronology: " +
Chronology.from(temporal));
}
}
private static boolean isIso(TemporalAccessor temporal) {
return Chronology.from(temporal).isIsoBased();
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -64,10 +64,6 @@ import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertSame; import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.Clock; import java.time.Clock;
import java.time.DateTimeException; import java.time.DateTimeException;
import java.time.LocalDate; import java.time.LocalDate;
@ -424,4 +420,22 @@ public class TCKChronology {
chrono.epochSecond(y, m, d, h, min, s, offset); chrono.epochSecond(y, m, d, h, min, s, offset);
} }
@DataProvider
Object[][] data_isIsoBased() {
return new Object[][] {
{IsoChronology.INSTANCE, true},
{JapaneseChronology.INSTANCE, true},
{MinguoChronology.INSTANCE, true},
{ThaiBuddhistChronology.INSTANCE, true},
{HijrahChronology.INSTANCE, false},
};
}
//-----------------------------------------------------------------------
// isIsoBased()
//-----------------------------------------------------------------------
@Test(dataProvider = "data_isIsoBased")
public void test_isIsoBased(Chronology chrono, boolean expected) {
assertEquals(chrono.isIsoBased(), expected);
}
} }

View file

@ -1,4 +1,5 @@
/* /*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -79,6 +80,7 @@ public class TCKTestServiceLoader {
ChronoLocalDate copticDate = chrono.date(1729, 4, 27); ChronoLocalDate copticDate = chrono.date(1729, 4, 27);
LocalDate ld = LocalDate.from(copticDate); LocalDate ld = LocalDate.from(copticDate);
assertEquals(ld, LocalDate.of(2013, 1, 5), "CopticDate does not match LocalDate"); assertEquals(ld, LocalDate.of(2013, 1, 5), "CopticDate does not match LocalDate");
assertEquals(chrono.isIsoBased(), false);
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -71,7 +71,7 @@ import static org.testng.Assert.fail;
import java.time.DayOfWeek; import java.time.DayOfWeek;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.chrono.ThaiBuddhistDate; import java.time.chrono.HijrahDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder; import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
@ -463,7 +463,7 @@ public class TCKIsoFields {
@Test(dataProvider = "isofields", expectedExceptions = UnsupportedTemporalTypeException.class) @Test(dataProvider = "isofields", expectedExceptions = UnsupportedTemporalTypeException.class)
public void test_nonisofields_rangerefinedby(TemporalField field, int value, ValueRange valueRange) { public void test_nonisofields_rangerefinedby(TemporalField field, int value, ValueRange valueRange) {
field.rangeRefinedBy(ThaiBuddhistDate.now()); field.rangeRefinedBy(HijrahDate.now());
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -477,7 +477,7 @@ public class TCKIsoFields {
@Test(dataProvider = "isofields", expectedExceptions = UnsupportedTemporalTypeException.class) @Test(dataProvider = "isofields", expectedExceptions = UnsupportedTemporalTypeException.class)
public void test_nonisofields_getFrom(TemporalField field, int value, ValueRange valueRange) { public void test_nonisofields_getFrom(TemporalField field, int value, ValueRange valueRange) {
field.getFrom(ThaiBuddhistDate.now()); field.getFrom(HijrahDate.now());
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View file

@ -1,4 +1,5 @@
/* /*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -58,6 +59,7 @@
*/ */
package test.java.time.chrono; package test.java.time.chrono;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import java.time.chrono.Chronology; import java.time.chrono.Chronology;
@ -81,7 +83,9 @@ public class TestServiceLoader {
for (Chronology chrono : loader) { for (Chronology chrono : loader) {
chronos.put(chrono.getId(), chrono); chronos.put(chrono.getId(), chrono);
} }
assertNotNull(chronos.get("Coptic"), "CopticChronology not found"); var coptic = chronos.get("Coptic");
assertNotNull(coptic, "CopticChronology not found");
assertEquals(coptic.isIsoBased(), false);
} }
} }

View file

@ -0,0 +1,117 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package test.java.time.temporal;
import static java.time.temporal.IsoFields.DAY_OF_QUARTER;
import static java.time.temporal.IsoFields.QUARTER_OF_YEAR;
import static java.time.temporal.IsoFields.WEEK_BASED_YEAR;
import static java.time.temporal.IsoFields.WEEK_OF_WEEK_BASED_YEAR;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.time.LocalDate;
import java.time.chrono.ChronoLocalDate;
import java.time.chrono.JapaneseDate;
import java.time.chrono.MinguoDate;
import java.time.chrono.ThaiBuddhistDate;
import java.time.temporal.TemporalField;
import java.time.temporal.ValueRange;
import java.util.List;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Tests fields in IsoFields class are supported in Japanese/Minguo/ThaiBuddhist
* date classes
* @bug 8279185
*/
@Test
public class TestIsoFields {
private static final LocalDate ld = LocalDate.of(2022, 2, 25);
private static final ChronoLocalDate J_DATE = JapaneseDate.from(ld);
private static final ChronoLocalDate M_DATE = MinguoDate.from(ld);
private static final ChronoLocalDate TB_DATE = ThaiBuddhistDate.from(ld);
private static final List<ChronoLocalDate> CLDATES = List.of(J_DATE, M_DATE, TB_DATE);
@DataProvider(name = "isSupported")
Object[][] data_isSupported() {
return new Object[][]{
{DAY_OF_QUARTER},
{QUARTER_OF_YEAR},
{WEEK_BASED_YEAR},
{WEEK_OF_WEEK_BASED_YEAR},
};
}
@DataProvider(name = "range")
Object[][] data_range() {
return new Object[][]{
{J_DATE, DAY_OF_QUARTER, ValueRange.of(1, 90)},
{J_DATE, QUARTER_OF_YEAR, ValueRange.of(1, 4)},
{J_DATE, WEEK_BASED_YEAR, ValueRange.of(1_873, 999_999_999)},
{J_DATE, WEEK_OF_WEEK_BASED_YEAR, ValueRange.of(1, 52)},
{M_DATE, DAY_OF_QUARTER, ValueRange.of(1, 90)},
{M_DATE, QUARTER_OF_YEAR, ValueRange.of(1, 4)},
{M_DATE, WEEK_BASED_YEAR, ValueRange.of(-999_999_999, 999_998_088)},
{M_DATE, WEEK_OF_WEEK_BASED_YEAR, ValueRange.of(1, 52)},
{TB_DATE, DAY_OF_QUARTER, ValueRange.of(1, 90)},
{TB_DATE, QUARTER_OF_YEAR, ValueRange.of(1, 4)},
{TB_DATE, WEEK_BASED_YEAR, ValueRange.of(-999_999_456, 999_999_999)},
{TB_DATE, WEEK_OF_WEEK_BASED_YEAR, ValueRange.of(1, 52)},
};
}
@DataProvider(name = "with_getLong")
Object[][] data_with_getLong() {
return new Object[][]{
{DAY_OF_QUARTER, 45},
{QUARTER_OF_YEAR, 2},
{WEEK_BASED_YEAR, 2_022},
{WEEK_OF_WEEK_BASED_YEAR, 10},
};
}
@Test(dataProvider = "isSupported")
public void test_isSupported(TemporalField f) {
CLDATES.forEach(d -> assertTrue(d.isSupported(f)));
}
@Test(dataProvider = "range")
public void test_range(ChronoLocalDate cld, TemporalField f, ValueRange r) {
assertEquals(cld.range(f), r);
}
@Test(dataProvider = "with_getLong")
public void test_with_getLong(TemporalField f, long val) {
CLDATES.forEach(d -> {
var min = d.range(f).getMinimum();
var max = d.range(f).getMaximum();
assertEquals(d.with(f, min).getLong(f), min);
assertEquals(d.with(f, max).getLong(f), max);
assertEquals(d.with(f, val).getLong(f), val);
});
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -34,6 +34,7 @@ import java.time.LocalTime;
import java.time.MonthDay; import java.time.MonthDay;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.Year; import java.time.Year;
import java.time.chrono.HijrahDate;
import java.time.chrono.ThaiBuddhistDate; import java.time.chrono.ThaiBuddhistDate;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.time.temporal.IsoFields; import java.time.temporal.IsoFields;
@ -75,12 +76,13 @@ public class TestIsoWeekFields {
assertEquals(weekField.isSupportedBy(MonthDay.of(2, 1)), false); assertEquals(weekField.isSupportedBy(MonthDay.of(2, 1)), false);
assertEquals(weekField.isSupportedBy(LocalDate.MIN), true); assertEquals(weekField.isSupportedBy(LocalDate.MIN), true);
assertEquals(weekField.isSupportedBy(OffsetDateTime.MAX), true); assertEquals(weekField.isSupportedBy(OffsetDateTime.MAX), true);
assertEquals(weekField.isSupportedBy(ThaiBuddhistDate.now()), true);
} }
@Test @Test
public void test_WOWBY_isSupportedBy_fieldsDiffer() { public void test_WOWBY_isSupportedBy_fieldsDiffer() {
assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.isSupportedBy(ThaiBuddhistDate.now()), false); assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.isSupportedBy(HijrahDate.now()), false);
assertEquals(WeekFields.ISO.weekOfWeekBasedYear().isSupportedBy(ThaiBuddhistDate.now()), true); assertEquals(WeekFields.ISO.weekOfWeekBasedYear().isSupportedBy(HijrahDate.now()), true);
} }
@Test(dataProvider = "fields") @Test(dataProvider = "fields")
@ -116,19 +118,22 @@ public class TestIsoWeekFields {
assertEquals(yearField.isSupportedBy(MonthDay.of(2, 1)), false); assertEquals(yearField.isSupportedBy(MonthDay.of(2, 1)), false);
assertEquals(yearField.isSupportedBy(LocalDate.MIN), true); assertEquals(yearField.isSupportedBy(LocalDate.MIN), true);
assertEquals(yearField.isSupportedBy(OffsetDateTime.MAX), true); assertEquals(yearField.isSupportedBy(OffsetDateTime.MAX), true);
assertEquals(yearField.isSupportedBy(ThaiBuddhistDate.now()), true);
} }
@Test @Test
public void test_WBY_isSupportedBy_ISO() { public void test_WBY_isSupportedBy_ISO() {
assertEquals(IsoFields.WEEK_BASED_YEAR.isSupportedBy(ThaiBuddhistDate.now()), false); assertEquals(IsoFields.WEEK_BASED_YEAR.isSupportedBy(HijrahDate.now()), false);
} }
@Test @Test
public void test_Unit_isSupportedBy_ISO() { public void test_Unit_isSupportedBy_ISO() {
assertEquals(IsoFields.WEEK_BASED_YEARS.isSupportedBy(LocalDate.now()),true); assertEquals(IsoFields.WEEK_BASED_YEARS.isSupportedBy(LocalDate.now()), true);
assertEquals(IsoFields.WEEK_BASED_YEARS.isSupportedBy(ThaiBuddhistDate.now()),false); assertEquals(IsoFields.WEEK_BASED_YEARS.isSupportedBy(ThaiBuddhistDate.now()), true);
assertEquals(IsoFields.QUARTER_YEARS.isSupportedBy(LocalDate.now()),true); assertEquals(IsoFields.WEEK_BASED_YEARS.isSupportedBy(HijrahDate.now()), false);
assertEquals(IsoFields.QUARTER_YEARS.isSupportedBy(ThaiBuddhistDate.now()),false); assertEquals(IsoFields.QUARTER_YEARS.isSupportedBy(LocalDate.now()), true);
assertEquals(IsoFields.QUARTER_YEARS.isSupportedBy(ThaiBuddhistDate.now()), true);
assertEquals(IsoFields.QUARTER_YEARS.isSupportedBy(HijrahDate.now()), false);
} }
@Test(dataProvider = "fields") @Test(dataProvider = "fields")