8226831: Use Objects.equals() when appropriate
authorigerasim
Tue, 27 Aug 2019 14:44:21 -0700
changeset 57895 82a71d82e326
parent 57894 01b9c26e2651
child 57896 48f52ad5a2c3
8226831: Use Objects.equals() when appropriate Reviewed-by: rriggs, bpb
src/java.base/share/classes/java/net/InterfaceAddress.java
src/java.base/share/classes/java/net/URLStreamHandler.java
src/java.base/share/classes/java/text/AttributedString.java
src/java.base/share/classes/java/util/TreeMap.java
src/java.base/share/classes/java/util/TreeSet.java
src/java.base/share/classes/sun/net/www/protocol/ftp/Handler.java
src/java.base/share/classes/sun/security/x509/AlgorithmId.java
--- a/src/java.base/share/classes/java/net/InterfaceAddress.java	Tue Aug 27 22:26:42 2019 +0200
+++ b/src/java.base/share/classes/java/net/InterfaceAddress.java	Tue Aug 27 14:44:21 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2019, 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
@@ -25,6 +25,8 @@
 
 package java.net;
 
+import java.util.Objects;
+
 /**
  * This class represents a Network Interface address. In short it's an
  * IP address, a subnet mask and a broadcast address when the address is
@@ -99,17 +101,17 @@
      * @see     java.net.InterfaceAddress#hashCode()
      */
     public boolean equals(Object obj) {
-        if (!(obj instanceof InterfaceAddress)) {
-            return false;
+        if (obj instanceof InterfaceAddress) {
+            InterfaceAddress cmp = (InterfaceAddress) obj;
+
+            if (Objects.equals(address, cmp.address) &&
+                Objects.equals(broadcast, cmp.broadcast) &&
+                maskLength == cmp.maskLength)
+            {
+                return true;
+            }
         }
-        InterfaceAddress cmp = (InterfaceAddress) obj;
-        if ( !(address == null ? cmp.address == null : address.equals(cmp.address)) )
-            return false;
-        if ( !(broadcast  == null ? cmp.broadcast == null : broadcast.equals(cmp.broadcast)) )
-            return false;
-        if (maskLength != cmp.maskLength)
-            return false;
-        return true;
+        return false;
     }
 
     /**
--- a/src/java.base/share/classes/java/net/URLStreamHandler.java	Tue Aug 27 22:26:42 2019 +0200
+++ b/src/java.base/share/classes/java/net/URLStreamHandler.java	Tue Aug 27 14:44:21 2019 -0700
@@ -30,6 +30,7 @@
 import java.io.File;
 import java.io.OutputStream;
 import java.util.Hashtable;
+import java.util.Objects;
 import sun.net.util.IPAddressUtil;
 import sun.net.www.ParseUtil;
 
@@ -343,10 +344,7 @@
      * @since 1.3
      */
     protected boolean equals(URL u1, URL u2) {
-        String ref1 = u1.getRef();
-        String ref2 = u2.getRef();
-        return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
-               sameFile(u1, u2);
+        return Objects.equals(u1.getRef(), u2.getRef()) && sameFile(u1, u2);
     }
 
     /**
--- a/src/java.base/share/classes/java/text/AttributedString.java	Tue Aug 27 22:26:42 2019 +0200
+++ b/src/java.base/share/classes/java/text/AttributedString.java	Tue Aug 27 14:44:21 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, 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
@@ -1100,8 +1100,7 @@
             return false;
         }
         AttributeEntry other = (AttributeEntry) o;
-        return other.key.equals(key) &&
-            (value == null ? other.value == null : other.value.equals(value));
+        return other.key.equals(key) && Objects.equals(other.value, value);
     }
 
     public Attribute getKey() {
--- a/src/java.base/share/classes/java/util/TreeMap.java	Tue Aug 27 22:26:42 2019 +0200
+++ b/src/java.base/share/classes/java/util/TreeMap.java	Tue Aug 27 14:44:21 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, 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
@@ -311,8 +311,7 @@
     public void putAll(Map<? extends K, ? extends V> map) {
         int mapSize = map.size();
         if (size==0 && mapSize!=0 && map instanceof SortedMap) {
-            Comparator<?> c = ((SortedMap<?,?>)map).comparator();
-            if (c == comparator || (c != null && c.equals(comparator))) {
+            if (Objects.equals(comparator, ((SortedMap<?,?>)map).comparator())) {
                 ++modCount;
                 try {
                     buildFromSorted(mapSize, map.entrySet().iterator(),
--- a/src/java.base/share/classes/java/util/TreeSet.java	Tue Aug 27 22:26:42 2019 +0200
+++ b/src/java.base/share/classes/java/util/TreeSet.java	Tue Aug 27 14:44:21 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2019, 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
@@ -302,9 +302,7 @@
             m instanceof TreeMap) {
             SortedSet<? extends E> set = (SortedSet<? extends E>) c;
             TreeMap<E,Object> map = (TreeMap<E, Object>) m;
-            Comparator<?> cc = set.comparator();
-            Comparator<? super E> mc = map.comparator();
-            if (cc==mc || (cc != null && cc.equals(mc))) {
+            if (Objects.equals(set.comparator(), map.comparator())) {
                 map.addAllForTreeSet(set, PRESENT);
                 return true;
             }
--- a/src/java.base/share/classes/sun/net/www/protocol/ftp/Handler.java	Tue Aug 27 22:26:42 2019 +0200
+++ b/src/java.base/share/classes/sun/net/www/protocol/ftp/Handler.java	Tue Aug 27 14:44:21 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2019, 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
@@ -23,7 +23,7 @@
  * questions.
  */
 
-/*-
+/*
  *      FTP stream opener
  */
 
@@ -34,6 +34,7 @@
 import java.net.Proxy;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Objects;
 import sun.net.ftp.FtpClient;
 import sun.net.www.protocol.http.HttpURLConnection;
 
@@ -47,8 +48,7 @@
     protected boolean equals(URL u1, URL u2) {
         String userInfo1 = u1.getUserInfo();
         String userInfo2 = u2.getUserInfo();
-        return super.equals(u1, u2) &&
-            (userInfo1 == null? userInfo2 == null: userInfo1.equals(userInfo2));
+        return super.equals(u1, u2) && Objects.equals(userInfo1, userInfo2);
     }
 
     protected java.net.URLConnection openConnection(URL u)
--- a/src/java.base/share/classes/sun/security/x509/AlgorithmId.java	Tue Aug 27 22:26:42 2019 +0200
+++ b/src/java.base/share/classes/sun/security/x509/AlgorithmId.java	Tue Aug 27 14:44:21 2019 -0700
@@ -274,8 +274,7 @@
      * with the same parameters.
      */
     public boolean equals(AlgorithmId other) {
-        boolean paramsEqual =
-          (params == null ? other.params == null : params.equals(other.params));
+        boolean paramsEqual = Objects.equals(other.params, params);
         return (algid.equals((Object)other.algid) && paramsEqual);
     }