changeset 9685:cc1e17f7848c

8036818: DateTimeFormatter withResolverFields() fails to accept null Reviewed-by: chegar, rriggs Contributed-by: scolebourne@joda.org
author scolebourne
date Thu, 06 Mar 2014 17:31:07 +0000
parents ef93162d53d6
children acf7fb2c0526
files src/share/classes/java/time/format/DateTimeFormatter.java test/java/time/tck/java/time/format/TCKDateTimeFormatter.java
diffstat 2 files changed, 17 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/time/format/DateTimeFormatter.java	Fri Apr 04 13:01:26 2014 +0200
+++ b/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/test/java/time/tck/java/time/format/TCKDateTimeFormatter.java	Fri Apr 04 13:01:26 2014 +0200
+++ b/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);
     }
 
     //-----------------------------------------------------------------------