8141652: Rename methods Objects.nonNullElse* to requireNonNullElse*
authorrriggs
Wed, 11 Nov 2015 22:38:15 -0500
changeset 33675 7d9d372a41df
parent 33674 566777f73c32
child 33676 7350cdfdf5ac
8141652: Rename methods Objects.nonNullElse* to requireNonNullElse* Summary: And some usages of replacing expr != null ? expr : otherexpr Reviewed-by: jrose, chegar
jdk/src/java.base/share/classes/java/net/InetAddress.java
jdk/src/java.base/share/classes/java/net/URLConnection.java
jdk/src/java.base/share/classes/java/nio/charset/Charset.java
jdk/src/java.base/share/classes/java/security/SecureRandom.java
jdk/src/java.base/share/classes/java/time/ZoneId.java
jdk/src/java.base/share/classes/java/time/chrono/Chronology.java
jdk/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java
jdk/src/java.base/share/classes/java/time/format/DateTimePrintContext.java
jdk/src/java.base/share/classes/java/util/Formatter.java
jdk/src/java.base/share/classes/java/util/Objects.java
jdk/test/java/util/Objects/BasicObjectsTest.java
--- a/jdk/src/java.base/share/classes/java/net/InetAddress.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/net/InetAddress.java	Wed Nov 11 22:38:15 2015 -0500
@@ -29,6 +29,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Objects;
 import java.util.ServiceLoader;
 import java.security.AccessController;
 import java.io.ObjectStreamException;
@@ -733,7 +734,7 @@
      */
     public String toString() {
         String hostName = holder().getHostName();
-        return ((hostName != null) ? hostName : "")
+        return Objects.toString(hostName, "")
             + "/" + getHostAddress();
     }
 
--- a/jdk/src/java.base/share/classes/java/net/URLConnection.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/net/URLConnection.java	Wed Nov 11 22:38:15 2015 -0500
@@ -32,6 +32,7 @@
 import java.util.Hashtable;
 import java.util.Date;
 import java.util.Iterator;
+import java.util.Objects;
 import java.util.ServiceConfigurationError;
 import java.util.ServiceLoader;
 import java.util.StringTokenizer;
@@ -1250,7 +1251,7 @@
 
         if (handler != null) {
             ContentHandler h = handlers.putIfAbsent(contentType, handler);
-            return h != null ? h : handler;
+            return Objects.requireNonNullElse(h, handler);
         }
 
         try {
@@ -1263,7 +1264,7 @@
         assert handler != null;
 
         ContentHandler h = handlers.putIfAbsent(contentType, handler);
-        return h != null ? h : handler;
+        return Objects.requireNonNullElse(h, handler);
     }
 
     /*
--- a/jdk/src/java.base/share/classes/java/nio/charset/Charset.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/nio/charset/Charset.java	Wed Nov 11 22:38:15 2015 -0500
@@ -37,6 +37,7 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.Objects;
 import java.util.Set;
 import java.util.ServiceLoader;
 import java.util.ServiceConfigurationError;
@@ -625,6 +626,7 @@
 
     private final String name;          // tickles a bug in oldjavac
     private final String[] aliases;     // tickles a bug in oldjavac
+    private final String[] zeroAliases = new String[0];
     private Set<String> aliasSet = null;
 
     /**
@@ -642,7 +644,7 @@
      */
     protected Charset(String canonicalName, String[] aliases) {
         checkName(canonicalName);
-        String[] as = (aliases == null) ? new String[0] : aliases;
+        String[] as = Objects.requireNonNullElse(aliases, zeroAliases);
         for (int i = 0; i < as.length; i++)
             checkName(as[i]);
         this.name = canonicalName;
--- a/jdk/src/java.base/share/classes/java/security/SecureRandom.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/security/SecureRandom.java	Wed Nov 11 22:38:15 2015 -0500
@@ -419,7 +419,7 @@
      * @since 1.5
      */
     public String getAlgorithm() {
-        return (algorithm != null) ? algorithm : "unknown";
+        return Objects.toString(algorithm, "unknown");
     }
 
     /**
--- a/jdk/src/java.base/share/classes/java/time/ZoneId.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/time/ZoneId.java	Wed Nov 11 22:38:15 2015 -0500
@@ -310,8 +310,7 @@
     public static ZoneId of(String zoneId, Map<String, String> aliasMap) {
         Objects.requireNonNull(zoneId, "zoneId");
         Objects.requireNonNull(aliasMap, "aliasMap");
-        String id = aliasMap.get(zoneId);
-        id = (id != null ? id : zoneId);
+        String id = Objects.requireNonNullElse(aliasMap.get(zoneId), zoneId);
         return of(id);
     }
 
--- a/jdk/src/java.base/share/classes/java/time/chrono/Chronology.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/time/chrono/Chronology.java	Wed Nov 11 22:38:15 2015 -0500
@@ -177,7 +177,7 @@
     static Chronology from(TemporalAccessor temporal) {
         Objects.requireNonNull(temporal, "temporal");
         Chronology obj = temporal.query(TemporalQueries.chronology());
-        return (obj != null ? obj : IsoChronology.INSTANCE);
+        return Objects.requireNonNullElse(obj, IsoChronology.INSTANCE);
     }
 
     //-----------------------------------------------------------------------
--- a/jdk/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java	Wed Nov 11 22:38:15 2015 -0500
@@ -2892,7 +2892,8 @@
 
         @Override
         public String toString() {
-            return "ReducedValue(" + field + "," + minWidth + "," + maxWidth + "," + (baseDate != null ? baseDate : baseValue) + ")";
+            return "ReducedValue(" + field + "," + minWidth + "," + maxWidth +
+                    "," + Objects.requireNonNullElse(baseDate, baseValue) + ")";
         }
     }
 
@@ -4332,7 +4333,7 @@
         private String getChronologyName(Chronology chrono, Locale locale) {
             String key = "calendarname." + chrono.getCalendarType();
             String name = DateTimeTextProvider.getLocalizedResource(key, locale);
-            return name != null ? name : chrono.getId();
+            return Objects.requireNonNullElseGet(name, () -> chrono.getId());
         }
     }
 
--- a/jdk/src/java.base/share/classes/java/time/format/DateTimePrintContext.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/time/format/DateTimePrintContext.java	Wed Nov 11 22:38:15 2015 -0500
@@ -146,7 +146,7 @@
         if (overrideZone != null) {
             // if have zone and instant, calculation is simple, defaulting chrono if necessary
             if (temporal.isSupported(INSTANT_SECONDS)) {
-                Chronology chrono = (effectiveChrono != null ? effectiveChrono : IsoChronology.INSTANCE);
+                Chronology chrono = Objects.requireNonNullElse(effectiveChrono, IsoChronology.INSTANCE);
                 return chrono.zonedDateTime(Instant.from(temporal), overrideZone);
             }
             // block changing zone on OffsetTime, and similar problem cases
--- a/jdk/src/java.base/share/classes/java/util/Formatter.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/util/Formatter.java	Wed Nov 11 22:38:15 2015 -0500
@@ -49,6 +49,7 @@
 import java.text.NumberFormat;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.Objects;
 
 import java.time.DateTimeException;
 import java.time.Instant;
@@ -3860,7 +3861,7 @@
                     ampm = dfs.getAmPmStrings();
                 }
                 String s = ampm[t.get(Calendar.AM_PM)];
-                sb.append(s.toLowerCase(l != null ? l : Locale.US));
+                sb.append(s.toLowerCase(Objects.requireNonNullElse(l, Locale.US)));
                 break;
             }
             case DateTime.SECONDS_SINCE_EPOCH: { // 's' (0 - 99...?)
@@ -3893,7 +3894,7 @@
                 TimeZone tz = t.getTimeZone();
                 sb.append(tz.getDisplayName((t.get(Calendar.DST_OFFSET) != 0),
                                            TimeZone.SHORT,
-                                            (l == null) ? Locale.US : l));
+                                           Objects.requireNonNullElse(l, Locale.US)));
                 break;
             }
 
@@ -3901,7 +3902,7 @@
             case DateTime.NAME_OF_DAY_ABBREV:     // 'a'
             case DateTime.NAME_OF_DAY:          { // 'A'
                 int i = t.get(Calendar.DAY_OF_WEEK);
-                Locale lt = ((l == null) ? Locale.US : l);
+                Locale lt = Objects.requireNonNullElse(l, Locale.US);
                 DateFormatSymbols dfs = DateFormatSymbols.getInstance(lt);
                 if (c == DateTime.NAME_OF_DAY)
                     sb.append(dfs.getWeekdays()[i]);
@@ -3913,7 +3914,7 @@
             case DateTime.NAME_OF_MONTH_ABBREV_X: // 'h' -- same b
             case DateTime.NAME_OF_MONTH:        { // 'B'
                 int i = t.get(Calendar.MONTH);
-                Locale lt = ((l == null) ? Locale.US : l);
+                Locale lt = Objects.requireNonNullElse(l, Locale.US);
                 DateFormatSymbols dfs = DateFormatSymbols.getInstance(lt);
                 if (c == DateTime.NAME_OF_MONTH)
                     sb.append(dfs.getMonths()[i]);
@@ -3984,7 +3985,7 @@
                 StringBuilder tsb = new StringBuilder();
                 print(tsb, t, DateTime.AM_PM, l);
 
-                sb.append(tsb.toString().toUpperCase(l != null ? l : Locale.US));
+                sb.append(tsb.toString().toUpperCase(Objects.requireNonNullElse(l, Locale.US)));
                 break;
             }
             case DateTime.DATE_TIME:    { // 'c' (Sat Nov 04 12:02:33 EST 1999)
@@ -4092,7 +4093,7 @@
                         ampm = dfs.getAmPmStrings();
                     }
                     String s = ampm[t.get(ChronoField.AMPM_OF_DAY)];
-                    sb.append(s.toLowerCase(l != null ? l : Locale.US));
+                    sb.append(s.toLowerCase(Objects.requireNonNullElse(l, Locale.US)));
                     break;
                 }
                 case DateTime.SECONDS_SINCE_EPOCH: { // 's' (0 - 99...?)
@@ -4131,7 +4132,7 @@
                         sb.append(TimeZone.getTimeZone(zid.getId())
                                           .getDisplayName(zid.getRules().isDaylightSavings(instant),
                                                           TimeZone.SHORT,
-                                                          (l == null) ? Locale.US : l));
+                                                          Objects.requireNonNullElse(l, Locale.US)));
                         break;
                     }
                     sb.append(zid.getId());
@@ -4141,7 +4142,7 @@
                 case DateTime.NAME_OF_DAY_ABBREV:     // 'a'
                 case DateTime.NAME_OF_DAY:          { // 'A'
                     int i = t.get(ChronoField.DAY_OF_WEEK) % 7 + 1;
-                    Locale lt = ((l == null) ? Locale.US : l);
+                    Locale lt = Objects.requireNonNullElse(l, Locale.US);
                     DateFormatSymbols dfs = DateFormatSymbols.getInstance(lt);
                     if (c == DateTime.NAME_OF_DAY)
                         sb.append(dfs.getWeekdays()[i]);
@@ -4153,7 +4154,7 @@
                 case DateTime.NAME_OF_MONTH_ABBREV_X: // 'h' -- same b
                 case DateTime.NAME_OF_MONTH:        { // 'B'
                     int i = t.get(ChronoField.MONTH_OF_YEAR) - 1;
-                    Locale lt = ((l == null) ? Locale.US : l);
+                    Locale lt = Objects.requireNonNullElse(l, Locale.US);
                     DateFormatSymbols dfs = DateFormatSymbols.getInstance(lt);
                     if (c == DateTime.NAME_OF_MONTH)
                         sb.append(dfs.getMonths()[i]);
@@ -4223,7 +4224,7 @@
                     // this may be in wrong place for some locales
                     StringBuilder tsb = new StringBuilder();
                     print(tsb, t, DateTime.AM_PM, l);
-                    sb.append(tsb.toString().toUpperCase(l != null ? l : Locale.US));
+                    sb.append(tsb.toString().toUpperCase(Objects.requireNonNullElse(l, Locale.US)));
                     break;
                 }
                 case DateTime.DATE_TIME:    { // 'c' (Sat Nov 04 12:02:33 EST 1999)
--- a/jdk/src/java.base/share/classes/java/util/Objects.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/src/java.base/share/classes/java/util/Objects.java	Wed Nov 11 22:38:15 2015 -0500
@@ -295,7 +295,7 @@
      *        {@code defaultObj} is {@code null}
      * @since 9
      */
-    public static <T> T nonNullElse(T obj, T defaultObj) {
+    public static <T> T requireNonNullElse(T obj, T defaultObj) {
         return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj");
     }
 
@@ -314,8 +314,9 @@
      *        the {@code supplier.get()} value is {@code null}
      * @since 9
      */
-    public static <T> T nonNullElseGet(T obj, Supplier<? extends T> supplier) {
-        return (obj != null) ? obj : requireNonNull(requireNonNull(supplier, "supplier").get(), "supplier.get()");
+    public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) {
+        return (obj != null) ? obj
+                : requireNonNull(requireNonNull(supplier, "supplier").get(), "supplier.get()");
     }
 
     /**
--- a/jdk/test/java/util/Objects/BasicObjectsTest.java	Wed Nov 11 09:19:12 2015 +0000
+++ b/jdk/test/java/util/Objects/BasicObjectsTest.java	Wed Nov 11 22:38:15 2015 -0500
@@ -241,12 +241,12 @@
         String nonNullString = "non-null";
 
         // Confirm the compile time return type matches
-        String result = Objects.nonNullElse(nullString, defString);
+        String result = Objects.requireNonNullElse(nullString, defString);
         errors += (result == defString) ? 0 : 1;
-        errors += (Objects.nonNullElse(nonNullString, defString) == nonNullString) ? 0 : 1;
-        errors += (Objects.nonNullElse(nonNullString, null) == nonNullString) ? 0 : 1;
+        errors += (Objects.requireNonNullElse(nonNullString, defString) == nonNullString) ? 0 : 1;
+        errors += (Objects.requireNonNullElse(nonNullString, null) == nonNullString) ? 0 : 1;
         try {
-            Objects.nonNullElse(null, null);
+            Objects.requireNonNullElse(null, null);
             errors += 1;
         } catch (NullPointerException npe) {
             // expected
@@ -254,20 +254,20 @@
         }
 
 
-        // Test nonNullElseGet with a supplier
-        errors += (Objects.nonNullElseGet(nullString, () -> defString) == defString) ? 0 : 1;
-        errors += (Objects.nonNullElseGet(nonNullString, () -> defString) == nonNullString) ? 0 : 1;
-        errors += (Objects.nonNullElseGet(nonNullString, () -> null) == nonNullString) ? 0 : 1;
+        // Test requireNonNullElseGet with a supplier
+        errors += (Objects.requireNonNullElseGet(nullString, () -> defString) == defString) ? 0 : 1;
+        errors += (Objects.requireNonNullElseGet(nonNullString, () -> defString) == nonNullString) ? 0 : 1;
+        errors += (Objects.requireNonNullElseGet(nonNullString, () -> null) == nonNullString) ? 0 : 1;
 
         try {
-            Objects.nonNullElseGet(null, () -> null);
+            Objects.requireNonNullElseGet(null, () -> null);
             errors += 1;
         } catch (NullPointerException npe) {
             // expected
             errors += npe.getMessage().equals("supplier.get()") ? 0 : 1;
         }
         try {       // supplier is null
-            Objects.nonNullElseGet(null, null);
+            Objects.requireNonNullElseGet(null, null);
             errors += 1;
         } catch (NullPointerException npe) {
             // expected