summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2024-03-24 13:14:11 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2024-03-24 13:14:11 -0500
commit26291e672b0e683edc9d57710a9a9d96ca199c45 (patch)
treedf88f3d3f110e50f38b8a2752d55df4a0c777677 /src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java
parentcc45a65c78c578eb404d8773b22e5b046917621f (diff)
Format source code & set explicit UTF-8
Diffstat (limited to 'src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java')
-rw-r--r--src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java104
1 files changed, 52 insertions, 52 deletions
diff --git a/src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java b/src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java
index bee4dd1..b71a4e0 100644
--- a/src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java
+++ b/src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java
@@ -67,7 +67,7 @@ public final class ConditionalExistenceCollections {
extends AbstractCollection<E> {
final Collection<E> collection;
final Predicate<E> existenceCondition;
-
+
/**
* Creates the {@code ConditionalExistenceCollection}.
*
@@ -80,38 +80,38 @@ public final class ConditionalExistenceCollections {
this.collection = collection;
this.existenceCondition = existenceCondition;
}
-
+
@Override
public boolean add(final E e) {
return this.collection.add(e) && this.existenceCondition.test(e);
}
-
+
@Override
public void clear() {
this.collection.clear();
}
-
+
@Override
public boolean contains(final Object o) {
if (!this.collection.contains(o))
return false;
-
+
// this collection can only contain instances of E
// since the object is in the collection, we know that it must be an
// instance of E
// therefore this cast will always work
@SuppressWarnings("unchecked")
final E e = (E) o;
-
+
return this.existenceCondition.test(e);
}
-
+
@Override
public Iterator<E> iterator() {
return conditionalExistenceIterator(this.collection.iterator(),
this.existenceCondition);
}
-
+
@Override
public boolean remove(final Object o) {
// remove() must be first in the && statement, otherwise it may not
@@ -119,32 +119,32 @@ public final class ConditionalExistenceCollections {
final boolean containedObject = this.contains(o);
return this.collection.remove(o) && containedObject;
}
-
+
@Override
public int size() {
return (int) this.collection.stream().filter(this.existenceCondition)
.count();
}
-
+
@Override
public Object[] toArray() {
// ensure the toArray operation is supported
this.collection.toArray();
-
+
// if it works, do it for real
return super.toArray();
}
-
+
@Override
public <T> T[] toArray(T[] a) {
// ensure the toArray operation is supported
this.collection.toArray();
-
+
// if it works, do it for real
return super.toArray(a);
}
}
-
+
/**
* Elements in this wrapper iterator only exist if they pass a condition.
*
@@ -157,7 +157,7 @@ public final class ConditionalExistenceCollections {
final Predicate<E> existenceCondition;
E nextElement;
boolean hasNext;
-
+
/**
* Creates the {@code ConditionalExistenceIterator}.
*
@@ -171,7 +171,7 @@ public final class ConditionalExistenceCollections {
this.existenceCondition = condition;
this.getAndSetNextElement();
}
-
+
/**
* Gets the next element, and sets nextElement and hasNext accordingly.
*
@@ -188,12 +188,12 @@ public final class ConditionalExistenceCollections {
} while (!this.existenceCondition.test(this.nextElement));
this.hasNext = true;
}
-
+
@Override
public boolean hasNext() {
return this.hasNext;
}
-
+
@Override
public E next() {
if (this.hasNext()) {
@@ -203,13 +203,13 @@ public final class ConditionalExistenceCollections {
} else
throw new NoSuchElementException();
}
-
+
@Override
public void remove() {
this.iterator.remove();
}
}
-
+
/**
* Mappings in this map only exist if the entry passes some condition.
*
@@ -221,7 +221,7 @@ public final class ConditionalExistenceCollections {
static final class ConditionalExistenceMap<K, V> extends AbstractMap<K, V> {
Map<K, V> map;
Predicate<Entry<K, V>> entryExistenceCondition;
-
+
/**
* Creates the {@code ConditionalExistenceMap}.
*
@@ -234,81 +234,81 @@ public final class ConditionalExistenceCollections {
this.map = map;
this.entryExistenceCondition = entryExistenceCondition;
}
-
+
@Override
public boolean containsKey(final Object key) {
if (!this.map.containsKey(key))
return false;
-
+
// only instances of K have mappings in the backing map
// since we know that key is a valid key, it must be an instance of K
@SuppressWarnings("unchecked")
final K keyAsK = (K) key;
-
+
// get and test entry
final V value = this.map.get(key);
final Entry<K, V> entry = new SimpleEntry<>(keyAsK, value);
return this.entryExistenceCondition.test(entry);
}
-
+
@Override
public Set<Entry<K, V>> entrySet() {
return conditionalExistenceSet(this.map.entrySet(),
this.entryExistenceCondition);
}
-
+
@Override
public V get(final Object key) {
return this.containsKey(key) ? this.map.get(key) : null;
}
-
+
private final Entry<K, V> getEntry(K key) {
return new Entry<>() {
@Override
public K getKey() {
return key;
}
-
+
@Override
public V getValue() {
return ConditionalExistenceMap.this.map.get(key);
}
-
+
@Override
public V setValue(V value) {
return ConditionalExistenceMap.this.map.put(key, value);
}
};
}
-
+
@Override
public Set<K> keySet() {
return conditionalExistenceSet(this.map.keySet(),
k -> this.entryExistenceCondition.test(this.getEntry(k)));
}
-
+
@Override
public V put(final K key, final V value) {
final V oldValue = this.map.put(key, value);
-
+
// get and test entry
final Entry<K, V> entry = new SimpleEntry<>(key, oldValue);
return this.entryExistenceCondition.test(entry) ? oldValue : null;
}
-
+
@Override
public V remove(final Object key) {
final V oldValue = this.map.remove(key);
return this.containsKey(key) ? oldValue : null;
}
-
+
@Override
public Collection<V> values() {
// maybe change this to use ConditionalExistenceCollection
return super.values();
}
}
-
+
/**
* Elements in this set only exist if a certain condition is true.
*
@@ -319,7 +319,7 @@ public final class ConditionalExistenceCollections {
static final class ConditionalExistenceSet<E> extends AbstractSet<E> {
private final Set<E> set;
private final Predicate<E> existenceCondition;
-
+
/**
* Creates the {@code ConditionalNonexistenceSet}.
*
@@ -332,7 +332,7 @@ public final class ConditionalExistenceCollections {
this.set = set;
this.existenceCondition = existenceCondition;
}
-
+
/**
* {@inheritDoc}
* <p>
@@ -343,33 +343,33 @@ public final class ConditionalExistenceCollections {
public boolean add(final E e) {
return this.set.add(e) && this.existenceCondition.test(e);
}
-
+
@Override
public void clear() {
this.set.clear();
}
-
+
@Override
public boolean contains(final Object o) {
if (!this.set.contains(o))
return false;
-
+
// this set can only contain instances of E
// since the object is in the set, we know that it must be an instance
// of E
// therefore this cast will always work
@SuppressWarnings("unchecked")
final E e = (E) o;
-
+
return this.existenceCondition.test(e);
}
-
+
@Override
public Iterator<E> iterator() {
return conditionalExistenceIterator(this.set.iterator(),
this.existenceCondition);
}
-
+
@Override
public boolean remove(final Object o) {
// remove() must be first in the && statement, otherwise it may not
@@ -377,31 +377,31 @@ public final class ConditionalExistenceCollections {
final boolean containedObject = this.contains(o);
return this.set.remove(o) && containedObject;
}
-
+
@Override
public int size() {
return (int) this.set.stream().filter(this.existenceCondition).count();
}
-
+
@Override
public Object[] toArray() {
// ensure the toArray operation is supported
this.set.toArray();
-
+
// if it works, do it for real
return super.toArray();
}
-
+
@Override
public <T> T[] toArray(T[] a) {
// ensure the toArray operation is supported
this.set.toArray();
-
+
// if it works, do it for real
return super.toArray(a);
}
}
-
+
/**
* Elements in the returned wrapper collection are ignored if they don't pass
* a condition.
@@ -418,7 +418,7 @@ public final class ConditionalExistenceCollections {
return new ConditionalExistenceCollection<>(collection,
existenceCondition);
}
-
+
/**
* Elements in the returned wrapper iterator are ignored if they don't pass a
* condition.
@@ -433,7 +433,7 @@ public final class ConditionalExistenceCollections {
final Iterator<E> iterator, final Predicate<E> existenceCondition) {
return new ConditionalExistenceIterator<>(iterator, existenceCondition);
}
-
+
/**
* Mappings in the returned wrapper map are ignored if the corresponding
* entry doesn't pass a condition
@@ -450,7 +450,7 @@ public final class ConditionalExistenceCollections {
final Predicate<Entry<K, V>> entryExistenceCondition) {
return new ConditionalExistenceMap<>(map, entryExistenceCondition);
}
-
+
/**
* Elements in the returned wrapper set are ignored if they don't pass a
* condition.