diff --git a/src/share/classes/java/time/format/DateTimeFormatter.java b/src/share/classes/java/time/format/DateTimeFormatter.java index 39d91986f86ceb1b582717fcad5eb7fc2c22f92e..9f5eba72f9b93923e892ec7b8e7869e5ad2d4446 100644 --- a/src/share/classes/java/time/format/DateTimeFormatter.java +++ b/src/share/classes/java/time/format/DateTimeFormatter.java @@ -1644,12 +1644,13 @@ public final class DateTimeFormatter { * @return a formatter based on this formatter with the requested resolver style, not null */ public DateTimeFormatter withResolverFields(TemporalField... resolverFields) { - Objects.requireNonNull(resolverFields, "resolverFields"); - Set fields = new HashSet<>(Arrays.asList(resolverFields)); + Set fields = null; + if (resolverFields != null) { + fields = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(resolverFields))); + } if (Objects.equals(this.resolverFields, fields)) { return this; } - fields = Collections.unmodifiableSet(fields); return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, fields, chrono, zone); } @@ -1693,11 +1694,12 @@ public final class DateTimeFormatter { * @return a formatter based on this formatter with the requested resolver style, not null */ public DateTimeFormatter withResolverFields(Set resolverFields) { - Objects.requireNonNull(resolverFields, "resolverFields"); if (Objects.equals(this.resolverFields, resolverFields)) { return this; } - resolverFields = Collections.unmodifiableSet(new HashSet<>(resolverFields)); + if (resolverFields != null) { + resolverFields = Collections.unmodifiableSet(new HashSet<>(resolverFields)); + } return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, resolverFields, chrono, zone); } diff --git a/test/java/time/tck/java/time/format/TCKDateTimeFormatter.java b/test/java/time/tck/java/time/format/TCKDateTimeFormatter.java index 8e2091e98881d08d7edefd96c8a023a0f9eaef3d..9c0a783ec538e89e0b113e523d16286aebc8af52 100644 --- a/test/java/time/tck/java/time/format/TCKDateTimeFormatter.java +++ b/test/java/time/tck/java/time/format/TCKDateTimeFormatter.java @@ -254,14 +254,20 @@ public class TCKDateTimeFormatter { assertEquals(parsed.isSupported(YEAR), false); // not in the list of resolverFields } - @Test(expectedExceptions = NullPointerException.class) + @Test public void test_resolverFields_Array_null() throws Exception { - DateTimeFormatter.ISO_DATE.withResolverFields((TemporalField[]) null); + DateTimeFormatter f = DateTimeFormatter.ISO_DATE.withResolverFields(MONTH_OF_YEAR); + assertEquals(f.getResolverFields().size(), 1); + f = f.withResolverFields((TemporalField[]) null); + assertEquals(f.getResolverFields(), null); } - @Test(expectedExceptions = NullPointerException.class) + @Test public void test_resolverFields_Set_null() throws Exception { - DateTimeFormatter.ISO_DATE.withResolverFields((Set) null); + DateTimeFormatter f = DateTimeFormatter.ISO_DATE.withResolverFields(MONTH_OF_YEAR); + assertEquals(f.getResolverFields().size(), 1); + f = f.withResolverFields((Set) null); + assertEquals(f.getResolverFields(), null); } //-----------------------------------------------------------------------