summaryrefslogtreecommitdiff
path: root/src/org/unitConverter
diff options
context:
space:
mode:
authorAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-19 21:40:33 -0400
committerAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-19 21:40:33 -0400
commite28907d4bf4234cb988a5e76ebf7122c7c1ff6cb (patch)
tree0edb3496f31a7dd42245a8ee45eb84ec66accc71 /src/org/unitConverter
parentb491a1d67b513f6a6f549fe6fcb374d731e0beca (diff)
Added a generic ConditionalExistenceCollection
Diffstat (limited to 'src/org/unitConverter')
-rw-r--r--src/org/unitConverter/math/ConditionalExistenceCollections.java87
1 files changed, 86 insertions, 1 deletions
diff --git a/src/org/unitConverter/math/ConditionalExistenceCollections.java b/src/org/unitConverter/math/ConditionalExistenceCollections.java
index c0a69f0..9522885 100644
--- a/src/org/unitConverter/math/ConditionalExistenceCollections.java
+++ b/src/org/unitConverter/math/ConditionalExistenceCollections.java
@@ -16,6 +16,7 @@
*/
package org.unitConverter.math;
+import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
@@ -48,9 +49,75 @@ import java.util.function.Predicate;
* @author Adrien Hopkins
* @since 2019-10-17
*/
-// TODO add conditional existence Collections, Lists and Sorted/Navigable Sets/Maps
+// TODO add conditional existence Lists and Sorted/Navigable Sets/Maps
public final class ConditionalExistenceCollections {
/**
+ * Elements in this collection only exist if they meet a condition.
+ *
+ * @author Adrien Hopkins
+ * @since 2019-10-17
+ * @param <E>
+ * type of element in collection
+ */
+ static final class ConditionalExistenceCollection<E> extends AbstractCollection<E> {
+ final Collection<E> collection;
+ final Predicate<E> existenceCondition;
+
+ /**
+ * Creates the {@code ConditionalExistenceCollection}.
+ *
+ * @param collection
+ * @param existenceCondition
+ * @since 2019-10-17
+ */
+ private ConditionalExistenceCollection(final Collection<E> collection, final Predicate<E> existenceCondition) {
+ 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 execute
+ 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();
+ }
+ }
+
+ /**
* Elements in this wrapper iterator only exist if they pass a condition.
*
* @author Adrien Hopkins
@@ -258,6 +325,7 @@ public final class ConditionalExistenceCollections {
@Override
public boolean remove(final Object o) {
+ // remove() must be first in the && statement, otherwise it may not execute
final boolean containedObject = this.contains(o);
return this.set.remove(o) && containedObject;
}
@@ -269,6 +337,23 @@ public final class ConditionalExistenceCollections {
}
/**
+ * Elements in the returned wrapper collection are ignored if they don't pass a condition.
+ *
+ * @param <E>
+ * type of elements in collection
+ * @param collection
+ * collection to wrap
+ * @param existenceCondition
+ * elements only exist if this returns true
+ * @return wrapper collection
+ * @since 2019-10-17
+ */
+ public static final <E> Collection<E> conditionalExistenceCollection(final Collection<E> collection,
+ final Predicate<E> existenceCondition) {
+ return new ConditionalExistenceCollection<>(collection, existenceCondition);
+ }
+
+ /**
* Elements in the returned wrapper iterator are ignored if they don't pass a condition.
*
* @param <E>