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

@@ -3018,20 +3018,17 @@ private void printCharacter(Object arg, Locale l) throws IOException {
String s = null;
if (arg instanceof Character) {
s = ((Character)arg).toString();
} else if (arg instanceof Byte) {
byte i = (Byte) arg;
} else if (arg instanceof Byte i) {
if (Character.isValidCodePoint(i))
s = new String(Character.toChars(i));
else
throw new IllegalFormatCodePointException(i);
} else if (arg instanceof Short) {
short i = (Short) arg;
} else if (arg instanceof Short i) {
if (Character.isValidCodePoint(i))
s = new String(Character.toChars(i));
else
throw new IllegalFormatCodePointException(i);
} else if (arg instanceof Integer) {
int i = (Integer) arg;
} else if (arg instanceof Integer i) {
if (Character.isValidCodePoint(i))
s = new String(Character.toChars(i));
else
@@ -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;