Skip to content
Permalink
Browse files
8267110: Update java.util to use instanceof pattern variable
Reviewed-by: lancea, naoto
  • Loading branch information
pconcannon committed May 25, 2021
1 parent 0a03fc8 commit a52c4ede2f043b7d4a234c7d06f91871312e9654
Showing with 142 additions and 266 deletions.
  1. +7 −10 src/java.base/share/classes/java/util/AbstractMap.java
  2. +1 −3 src/java.base/share/classes/java/util/BitSet.java
  3. +12 −23 src/java.base/share/classes/java/util/Collections.java
  4. +7 −14 src/java.base/share/classes/java/util/EnumMap.java
  5. +6 −11 src/java.base/share/classes/java/util/HashMap.java
  6. +4 −8 src/java.base/share/classes/java/util/Hashtable.java
  7. +9 −17 src/java.base/share/classes/java/util/IdentityHashMap.java
  8. +5 −10 src/java.base/share/classes/java/util/JumboEnumSet.java
  9. +3 −4 src/java.base/share/classes/java/util/KeyValueHolder.java
  10. +2 −4 src/java.base/share/classes/java/util/LinkedHashMap.java
  11. +4 −8 src/java.base/share/classes/java/util/Locale.java
  12. +2 −6 src/java.base/share/classes/java/util/Optional.java
  13. +4 −8 src/java.base/share/classes/java/util/OptionalDouble.java
  14. +3 −7 src/java.base/share/classes/java/util/OptionalInt.java
  15. +3 −7 src/java.base/share/classes/java/util/OptionalLong.java
  16. +8 −18 src/java.base/share/classes/java/util/PropertyPermission.java
  17. +5 −10 src/java.base/share/classes/java/util/RegularEnumSet.java
  18. +2 −4 src/java.base/share/classes/java/util/Scanner.java
  19. +2 −5 src/java.base/share/classes/java/util/ServiceLoader.java
  20. +24 −31 src/java.base/share/classes/java/util/SimpleTimeZone.java
  21. +7 −13 src/java.base/share/classes/java/util/TreeMap.java
  22. +1 −2 src/java.base/share/classes/java/util/TreeSet.java
  23. +5 −9 src/java.base/share/classes/java/util/WeakHashMap.java
  24. +2 −6 src/java.base/share/classes/java/util/jar/Attributes.java
  25. +3 −5 src/java.base/share/classes/java/util/jar/JarVerifier.java
  26. +3 −7 src/java.base/share/classes/java/util/jar/Manifest.java
  27. +2 −4 src/java.base/share/classes/java/util/regex/Pattern.java
  28. +5 −10 src/java.base/share/classes/java/util/regex/PrintPattern.java
  29. +1 −2 src/java.base/share/classes/java/util/zip/ZipFile.java
@@ -476,9 +476,8 @@ public boolean equals(Object o) {
if (o == this)
return true;

if (!(o instanceof Map))
if (!(o instanceof Map<?, ?> m))
return false;
Map<?,?> m = (Map<?,?>) o;
if (m.size() != size())
return false;

@@ -688,10 +687,9 @@ public V setValue(V value) {
* @see #hashCode
*/
public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
return eq(key, e.getKey()) && eq(value, e.getValue());
return o instanceof Map.Entry<?, ?> e
&& eq(key, e.getKey())
&& eq(value, e.getValue());
}

/**
@@ -822,10 +820,9 @@ public V setValue(V value) {
* @see #hashCode
*/
public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
return eq(key, e.getKey()) && eq(value, e.getValue());
return o instanceof Map.Entry<?, ?> e
&& eq(key, e.getKey())
&& eq(value, e.getValue());
}

/**
@@ -1066,13 +1066,11 @@ public int size() {
* @see #size()
*/
public boolean equals(Object obj) {
if (!(obj instanceof BitSet))
if (!(obj instanceof BitSet set))
return false;
if (this == obj)
return true;

BitSet set = (BitSet) obj;

checkInvariants();
set.checkInvariants();

@@ -1775,12 +1775,9 @@ public boolean equals(Object o) {
if (o == this)
return true;

if (!(o instanceof Set))
return false;
Set<?> s = (Set<?>) o;
if (s.size() != c.size())
return false;
return containsAll(s); // Invokes safe containsAll() above
return o instanceof Set<?> s
&& s.size() == c.size()
&& containsAll(s); // Invokes safe containsAll() above
}

/**
@@ -1805,11 +1802,9 @@ public V setValue(V value) {
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> t = (Map.Entry<?,?>)o;
return eq(e.getKey(), t.getKey()) &&
eq(e.getValue(), t.getValue());
return o instanceof Map.Entry<?, ?> t
&& eq(e.getKey(), t.getKey())
&& eq(e.getValue(), t.getValue());
}
public String toString() {return e.toString();}
}
@@ -3933,11 +3928,8 @@ public void forEachRemaining(Consumer<? super Entry<K, V>> action) {
* setValue method.
*/
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
return s.contains(
(e instanceof CheckedEntry) ? e : checkedEntry(e, valueType));
return o instanceof Map.Entry<?, ?> e
&& s.contains((e instanceof CheckedEntry) ? e : checkedEntry(e, valueType));
}

/**
@@ -3981,11 +3973,9 @@ private boolean batchRemove(Collection<?> c, boolean complement) {
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Set))
return false;
Set<?> that = (Set<?>) o;
return that.size() == s.size()
&& containsAll(that); // Invokes safe containsAll() above
return o instanceof Set<?> that
&& that.size() == s.size()
&& containsAll(that); // Invokes safe containsAll() above
}

static <K,V,T> CheckedEntry<K,V,T> checkedEntry(Map.Entry<K,V> e,
@@ -5251,8 +5241,7 @@ public int hashCode() {
public boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof CopiesList) {
CopiesList<?> other = (CopiesList<?>) o;
if (o instanceof CopiesList<?> other) {
return n == other.n && (n == 0 || eq(element, other.element));
}
if (!(o instanceof List))
@@ -328,8 +328,7 @@ private boolean isValidKey(Object key) {
* one or more keys in the specified map are null
*/
public void putAll(Map<? extends K, ? extends V> m) {
if (m instanceof EnumMap) {
EnumMap<?, ?> em = (EnumMap<?, ?>)m;
if (m instanceof EnumMap<?, ?> em) {
if (em.keyType != keyType) {
if (em.isEmpty())
return;
@@ -473,16 +472,12 @@ public void clear() {
}

public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
return containsMapping(entry.getKey(), entry.getValue());
return o instanceof Map.Entry<?, ?> entry
&& containsMapping(entry.getKey(), entry.getValue());
}
public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
return removeMapping(entry.getKey(), entry.getValue());
return o instanceof Map.Entry<?, ?> entry
&& removeMapping(entry.getKey(), entry.getValue());
}
public int size() {
return size;
@@ -606,10 +601,9 @@ public boolean equals(Object o) {
if (index < 0)
return o == this;

if (!(o instanceof Map.Entry))
if (!(o instanceof Map.Entry<?, ?> e))
return false;

Map.Entry<?,?> e = (Map.Entry<?,?>)o;
V ourValue = unmaskNull(vals[index]);
Object hisValue = e.getValue();
return (e.getKey() == keyUniverse[index] &&
@@ -655,10 +649,9 @@ public boolean equals(Object o) {
return true;
if (o instanceof EnumMap)
return equals((EnumMap<?,?>)o);
if (!(o instanceof Map))
if (!(o instanceof Map<?, ?> m))
return false;

Map<?,?> m = (Map<?,?>)o;
if (size != m.size())
return false;

@@ -307,13 +307,10 @@ public final V setValue(V newValue) {
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;

return o instanceof Map.Entry<?, ?> e
&& Objects.equals(key, e.getKey())
&& Objects.equals(value, e.getValue());
}
}

@@ -1100,16 +1097,14 @@ public final void forEach(Consumer<? super V> action) {
return new EntryIterator();
}
public final boolean contains(Object o) {
if (!(o instanceof Map.Entry))
if (!(o instanceof Map.Entry<?, ?> e))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Node<K,V> candidate = getNode(key);
return candidate != null && candidate.equals(e);
}
public final boolean remove(Object o) {
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
if (o instanceof Map.Entry<?, ?> e) {
Object key = e.getKey();
Object value = e.getValue();
return removeNode(hash(key), key, value, true, true) != null;
@@ -714,9 +714,8 @@ public boolean add(Map.Entry<K,V> o) {
}

public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
if (!(o instanceof Map.Entry<?, ?> entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
Object key = entry.getKey();
Entry<?,?>[] tab = table;
int hash = key.hashCode();
@@ -729,9 +728,8 @@ public boolean contains(Object o) {
}

public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
if (!(o instanceof Map.Entry<?, ?> entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
Object key = entry.getKey();
Entry<?,?>[] tab = table;
int hash = key.hashCode();
@@ -816,9 +814,8 @@ public synchronized boolean equals(Object o) {
if (o == this)
return true;

if (!(o instanceof Map))
if (!(o instanceof Map<?, ?> t))
return false;
Map<?,?> t = (Map<?,?>) o;
if (t.size() != size())
return false;

@@ -1393,9 +1390,8 @@ public V setValue(V value) {
}

public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
if (!(o instanceof Map.Entry<?, ?> e))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>)o;

return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
(value==null ? e.getValue()==null : value.equals(e.getValue()));
@@ -642,8 +642,7 @@ public void clear() {
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (o instanceof IdentityHashMap) {
IdentityHashMap<?,?> m = (IdentityHashMap<?,?>) o;
} else if (o instanceof IdentityHashMap<?, ?> m) {
if (m.size() != size)
return false;

@@ -654,8 +653,7 @@ public boolean equals(Object o) {
return false;
}
return true;
} else if (o instanceof Map) {
Map<?,?> m = (Map<?,?>)o;
} else if (o instanceof Map<?, ?> m) {
return entrySet().equals(m.entrySet());
} else {
return false; // o is not a Map
@@ -888,11 +886,9 @@ public boolean equals(Object o) {
if (index < 0)
return super.equals(o);

if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
return (e.getKey() == unmaskNull(traversalTable[index]) &&
e.getValue() == traversalTable[index+1]);
return o instanceof Map.Entry<?, ?> e
&& e.getKey() == unmaskNull(traversalTable[index])
&& e.getValue() == traversalTable[index+1];
}

public int hashCode() {
@@ -1189,16 +1185,12 @@ public void clear() {
return new EntryIterator();
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
return containsMapping(entry.getKey(), entry.getValue());
return o instanceof Entry<?, ?> entry
&& containsMapping(entry.getKey(), entry.getValue());
}
public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
return removeMapping(entry.getKey(), entry.getValue());
return o instanceof Entry<?, ?> entry
&& removeMapping(entry.getKey(), entry.getValue());
}
public int size() {
return size;
@@ -248,10 +248,9 @@ public boolean remove(Object e) {
* @throws NullPointerException if the specified collection is null
*/
public boolean containsAll(Collection<?> c) {
if (!(c instanceof JumboEnumSet))
if (!(c instanceof JumboEnumSet<?> es))
return super.containsAll(c);

JumboEnumSet<?> es = (JumboEnumSet<?>)c;
if (es.elementType != elementType)
return es.isEmpty();

@@ -270,10 +269,9 @@ public boolean containsAll(Collection<?> c) {
* its elements are null
*/
public boolean addAll(Collection<? extends E> c) {
if (!(c instanceof JumboEnumSet))
if (!(c instanceof JumboEnumSet<?> es))
return super.addAll(c);

JumboEnumSet<?> es = (JumboEnumSet<?>)c;
if (es.elementType != elementType) {
if (es.isEmpty())
return false;
@@ -296,10 +294,9 @@ public boolean addAll(Collection<? extends E> c) {
* @throws NullPointerException if the specified collection is null
*/
public boolean removeAll(Collection<?> c) {
if (!(c instanceof JumboEnumSet))
if (!(c instanceof JumboEnumSet<?> es))
return super.removeAll(c);

JumboEnumSet<?> es = (JumboEnumSet<?>)c;
if (es.elementType != elementType)
return false;

@@ -317,10 +314,9 @@ public boolean removeAll(Collection<?> c) {
* @throws NullPointerException if the specified collection is null
*/
public boolean retainAll(Collection<?> c) {
if (!(c instanceof JumboEnumSet))
if (!(c instanceof JumboEnumSet<?> es))
return super.retainAll(c);

JumboEnumSet<?> es = (JumboEnumSet<?>)c;
if (es.elementType != elementType) {
boolean changed = (size != 0);
clear();
@@ -350,10 +346,9 @@ public void clear() {
* @return {@code true} if the specified object is equal to this set
*/
public boolean equals(Object o) {
if (!(o instanceof JumboEnumSet))
if (!(o instanceof JumboEnumSet<?> es))
return super.equals(o);

JumboEnumSet<?> es = (JumboEnumSet<?>)o;
if (es.elementType != elementType)
return size == 0 && es.size == 0;

@@ -101,10 +101,9 @@ public V setValue(V value) {
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
return key.equals(e.getKey()) && value.equals(e.getValue());
return o instanceof Map.Entry<?, ?> e
&& key.equals(e.getKey())
&& value.equals(e.getValue());
}

/**
Loading

1 comment on commit a52c4ed

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on a52c4ed May 25, 2021

Please sign in to comment.