Use Pattern Matching for instanceof in PropertyKeySet (#2086)

This commit is contained in:
Hannes Greule 2023-02-07 23:07:31 +01:00 committed by GitHub
parent 07217d0b81
commit f9d6b127e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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