summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:41:14 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:41:14 -0500
commit41e85b1493a9253f11aefd179d14c3c01a7c9287 (patch)
tree02bff1a9601edd4096615022d3225fa56eba554a /BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
parentd7648dd32feedd293be253f827d0a9618d53d043 (diff)
Commenting of various things
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java108
1 files changed, 85 insertions, 23 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
index 88db172..9eb8e17 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
@@ -26,6 +26,9 @@ import bjc.utils.data.Pair;
* The type in this list
*/
public class FunctionalList<E> implements Cloneable {
+ /**
+ * The list used as a backing store
+ */
private List<E> wrap;
/**
@@ -92,14 +95,6 @@ public class FunctionalList<E> implements Cloneable {
public boolean add(E item) {
return wrap.add(item);
}
-
- /**
- * Prepend an item to the list
- * @param item The item to prepend to the list
- */
- public void prepend(E item) {
- wrap.add(0, item);
- }
/**
* Check if all of the elements of this list match the specified
@@ -136,6 +131,17 @@ public class FunctionalList<E> implements Cloneable {
return false;
}
+ @Override
+ public FunctionalList<E> clone() {
+ FunctionalList<E> fl = new FunctionalList<>();
+
+ for (E ele : wrap) {
+ fl.add(ele);
+ }
+
+ return fl;
+ }
+
/**
* Combine this list with another one into a new list and merge the
* results. Works sort of like a combined zip/map over resulting pairs.
@@ -165,6 +171,17 @@ public class FunctionalList<E> implements Cloneable {
}
/**
+ * Check if the list contains the specified item
+ *
+ * @param item
+ * The item to see if it is contained
+ * @return Whether or not the specified item is in the list
+ */
+ public boolean contains(E item) {
+ return this.anyMatch(item::equals);
+ }
+
+ /**
* Get the first element in the list
*
* @return The first element in this list.
@@ -240,6 +257,25 @@ public class FunctionalList<E> implements Cloneable {
}
/**
+ * Retrieve a list containing all elements matching a predicate
+ *
+ * @param matchPred
+ * The predicate to match by
+ * @return A list containing all elements that match the predicate
+ */
+ public FunctionalList<E> getMatching(Predicate<E> matchPred) {
+ FunctionalList<E> fl = new FunctionalList<>();
+
+ this.forEach((elem) -> {
+ if (matchPred.test(elem)) {
+ fl.add(elem);
+ }
+ });
+
+ return fl;
+ }
+
+ /**
* Check if this list is empty.
*
* @return Whether or not this list is empty.
@@ -265,6 +301,28 @@ public class FunctionalList<E> implements Cloneable {
}
/**
+ * Zip two lists into a list of pairs
+ *
+ * @param fl
+ * The list to use as the left side of the pair
+ * @return A list containing pairs of this element and the specified
+ * list
+ */
+ public <T> FunctionalList<Pair<E, T>> pairWith(FunctionalList<T> fl) {
+ return combineWith(fl, Pair<E, T>::new);
+ }
+
+ /**
+ * Prepend an item to the list
+ *
+ * @param item
+ * The item to prepend to the list
+ */
+ public void prepend(E item) {
+ wrap.add(0, item);
+ }
+
+ /**
* Select a random item from this list, using the provided random
* number generator.
*
@@ -302,6 +360,17 @@ public class FunctionalList<E> implements Cloneable {
}
/**
+ * Remove all elements that match a given predicate
+ *
+ * @param remPred
+ * The predicate to use to determine elements to delete
+ * @return Whether there was anything that satisfied the predicate
+ */
+ public boolean removeIf(Predicate<E> remPred) {
+ return wrap.removeIf(remPred);
+ }
+
+ /**
* Perform a binary search for the specified key using the provided
* means of comparing elements. Since this IS a binary search, the list
* must have been sorted before hand.
@@ -329,6 +398,14 @@ public class FunctionalList<E> implements Cloneable {
Collections.sort(wrap, cmp);
}
+ /**
+ * Convert the list into a iterable
+ * @return An iterable view onto the list
+ */
+ public Iterable<E> toIterable() {
+ return wrap;
+ }
+
/*
* Reduce this item to a form useful for looking at in the debugger.
* (non-Javadoc)
@@ -345,19 +422,4 @@ public class FunctionalList<E> implements Cloneable {
return sb.toString();
}
-
- public <T> FunctionalList<Pair<E, T>> pairWith(FunctionalList<T> fl) {
- return combineWith(fl, Pair<E, T>::new);
- }
-
- @Override
- public FunctionalList<E> clone() {
- FunctionalList<E> fl = new FunctionalList<>();
-
- for (E ele : wrap) {
- fl.add(ele);
- }
-
- return fl;
- }
}