8036818: DateTimeFormatter withResolverFields() fails to accept null
authorscolebourne
Thu, 06 Mar 2014 17:31:07 +0000
changeset 23722 877a047171bb
parent 23721 83da5b498504
child 23723 46b29c0e9656
8036818: DateTimeFormatter withResolverFields() fails to accept null Reviewed-by: chegar, rriggs Contributed-by: scolebourne@joda.org
jdk/src/share/classes/java/time/format/DateTimeFormatter.java
jdk/test/java/time/tck/java/time/format/TCKDateTimeFormatter.java
--- a/jdk/src/share/classes/java/time/format/DateTimeFormatter.java	Fri Apr 04 13:01:26 2014 +0200
+++ b/jdk/src/share/classes/java/time/format/DateTimeFormatter.java	Thu Mar 06 17:31:07 2014 +0000
@@ -1644,12 +1644,13 @@
      * @return a formatter based on this formatter with the requested resolver style, not null
      */
     public DateTimeFormatter withResolverFields(TemporalField... resolverFields) {
-        Objects.requireNonNull(resolverFields, "resolverFields");
-        Set<TemporalField> fields = new HashSet<>(Arrays.asList(resolverFields));
+        Set<TemporalField> 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 @@
      * @return a formatter based on this formatter with the requested resolver style, not null
      */
     public DateTimeFormatter withResolverFields(Set<TemporalField> 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);
     }
 
--- a/jdk/test/java/time/tck/java/time/format/TCKDateTimeFormatter.java	Fri Apr 04 13:01:26 2014 +0200
+++ b/jdk/test/java/time/tck/java/time/format/TCKDateTimeFormatter.java	Thu Mar 06 17:31:07 2014 +0000
@@ -254,14 +254,20 @@
         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<TemporalField>) null);
+        DateTimeFormatter f = DateTimeFormatter.ISO_DATE.withResolverFields(MONTH_OF_YEAR);
+        assertEquals(f.getResolverFields().size(), 1);
+        f = f.withResolverFields((Set<TemporalField>) null);
+        assertEquals(f.getResolverFields(), null);
     }
 
     //-----------------------------------------------------------------------