Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8267110: Update java.util to use instanceof pattern variable #4088

Closed
wants to merge 6 commits into from
17 changes: 7 additions & 10 deletions src/java.base/share/classes/java/util/AbstractMap.java
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/java.base/share/classes/java/util/BitSet.java
Expand Up @@ -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();

Expand Down
35 changes: 12 additions & 23 deletions src/java.base/share/classes/java/util/Collections.java
Expand Up @@ -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
}

/**
Expand All @@ -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();}
}
Expand Down Expand Up @@ -3933,11 +3928,8 @@ public <T> T[] toArray(T[] a) {
* 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));
}

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down
21 changes: 7 additions & 14 deletions src/java.base/share/classes/java/util/EnumMap.java
Expand Up @@ -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;
Expand Down Expand Up @@ -473,16 +472,12 @@ public Iterator<Map.Entry<K,V>> iterator() {
}

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;
Expand Down Expand Up @@ -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] &&
Expand Down Expand Up @@ -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;

Expand Down
17 changes: 6 additions & 11 deletions src/java.base/share/classes/java/util/HashMap.java
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -1100,16 +1097,14 @@ public final Iterator<Map.Entry<K,V>> iterator() {
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;
Expand Down
12 changes: 4 additions & 8 deletions src/java.base/share/classes/java/util/Hashtable.java
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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()));
Expand Down
26 changes: 9 additions & 17 deletions src/java.base/share/classes/java/util/IdentityHashMap.java
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -1189,16 +1185,12 @@ public Iterator<Map.Entry<K,V>> iterator() {
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;
Expand Down
15 changes: 5 additions & 10 deletions src/java.base/share/classes/java/util/JumboEnumSet.java
Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 3 additions & 4 deletions src/java.base/share/classes/java/util/KeyValueHolder.java
Expand Up @@ -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());
}

/**
Expand Down