Use Pattern Matching for instanceof in PropertyKeySet (#2086)

Dieser Commit ist enthalten in:
Hannes Greule 2023-02-07 23:07:31 +01:00 committet von GitHub
Ursprung 07217d0b81
Commit f9d6b127e6
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23

Datei anzeigen

@ -18,9 +18,9 @@ public class PropertyKeySet implements Set<PropertyKey> {
public static PropertyKeySet ofCollection(Collection<? extends PropertyKey> collection) {
PropertyKeySet set = new PropertyKeySet();
if (collection instanceof PropertyKeySet) {
if (collection instanceof PropertyKeySet pks) {
// simple copy
set.bits.or(((PropertyKeySet) collection).bits);
set.bits.or(pks.bits);
return set;
}
for (PropertyKey key : collection) {
@ -51,10 +51,10 @@ public class PropertyKeySet implements Set<PropertyKey> {
@Override
public boolean contains(Object o) {
if (!(o instanceof PropertyKey)) {
if (!(o instanceof PropertyKey pk)) {
return false;
}
return this.bits.get(((PropertyKey) o).getId());
return this.bits.get(pk.getId());
}
@Nonnull
@ -92,26 +92,26 @@ public class PropertyKeySet implements Set<PropertyKey> {
@Override
public boolean remove(Object o) {
if (!(o instanceof PropertyKey)) {
if (!(o instanceof PropertyKey pk)) {
return false;
}
if (!this.bits.get(((PropertyKey) o).getId())) {
if (!this.bits.get(pk.getId())) {
return false;
}
this.bits.clear(((PropertyKey) o).getId());
this.bits.clear(pk.getId());
return true;
}
@Override
public boolean containsAll(@Nonnull Collection<?> c) {
if (c instanceof PropertyKeySet) {
return ((PropertyKeySet) c).bits.intersects(this.bits);
if (c instanceof PropertyKeySet pks) {
return pks.bits.intersects(this.bits);
}
for (Object o : c) {
if (!(o instanceof PropertyKey)) {
if (!(o instanceof PropertyKey pk)) {
return false;
}
if (!this.bits.get(((PropertyKey) o).getId())) {
if (!this.bits.get(pk.getId())) {
return false;
}
}
@ -121,8 +121,8 @@ public class PropertyKeySet implements Set<PropertyKey> {
@Override
public boolean addAll(@Nonnull Collection<? extends PropertyKey> c) {
int cardinality = this.bits.cardinality();
if (c instanceof PropertyKeySet) {
this.bits.or(((PropertyKeySet) c).bits);
if (c instanceof PropertyKeySet pks) {
this.bits.or(pks.bits);
} else {
for (PropertyKey key : c) {
this.bits.set(key.getId());
@ -135,8 +135,8 @@ public class PropertyKeySet implements Set<PropertyKey> {
public boolean retainAll(@Nonnull Collection<?> c) {
int cardinality = this.bits.cardinality();
BitSet removal;
if (c instanceof PropertyKeySet) {
removal = ((PropertyKeySet) c).bits;
if (c instanceof PropertyKeySet pks) {
removal = pks.bits;
} else {
removal = new BitSet(this.bits.length());
for (PropertyKey key : this) {
@ -152,12 +152,12 @@ public class PropertyKeySet implements Set<PropertyKey> {
@Override
public boolean removeAll(@Nonnull Collection<?> c) {
int cardinality = this.bits.cardinality();
if (c instanceof PropertyKeySet) {
this.bits.andNot(((PropertyKeySet) c).bits);
if (c instanceof PropertyKeySet pks) {
this.bits.andNot(pks.bits);
} else {
for (Object o : c) { // mh
if (o instanceof PropertyKey) {
this.bits.clear(((PropertyKey) o).getId());
if (o instanceof PropertyKey pk) {
this.bits.clear(pk.getId());
}
}
}