From 41e85b1493a9253f11aefd179d14c3c01a7c9287 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 21 Feb 2016 15:41:14 -0500 Subject: Commenting of various things --- .../java/bjc/utils/funcdata/FunctionalList.java | 108 ++++++++++++++++----- 1 file changed, 85 insertions(+), 23 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java') 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 implements Cloneable { + /** + * The list used as a backing store + */ private List wrap; /** @@ -92,14 +95,6 @@ public class FunctionalList 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 implements Cloneable { return false; } + @Override + public FunctionalList clone() { + FunctionalList 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. @@ -164,6 +170,17 @@ public class FunctionalList implements Cloneable { return r; } + /** + * 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 * @@ -239,6 +256,25 @@ public class FunctionalList implements Cloneable { return wrap; } + /** + * 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 getMatching(Predicate matchPred) { + FunctionalList fl = new FunctionalList<>(); + + this.forEach((elem) -> { + if (matchPred.test(elem)) { + fl.add(elem); + } + }); + + return fl; + } + /** * Check if this list is empty. * @@ -264,6 +300,28 @@ public class FunctionalList implements Cloneable { return fl; } + /** + * 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 FunctionalList> pairWith(FunctionalList fl) { + return combineWith(fl, Pair::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. @@ -301,6 +359,17 @@ public class FunctionalList implements Cloneable { return finl.apply(acum.held); } + /** + * 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 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 @@ -329,6 +398,14 @@ public class FunctionalList implements Cloneable { Collections.sort(wrap, cmp); } + /** + * Convert the list into a iterable + * @return An iterable view onto the list + */ + public Iterable 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 implements Cloneable { return sb.toString(); } - - public FunctionalList> pairWith(FunctionalList fl) { - return combineWith(fl, Pair::new); - } - - @Override - public FunctionalList clone() { - FunctionalList fl = new FunctionalList<>(); - - for (E ele : wrap) { - fl.add(ele); - } - - return fl; - } } -- cgit v1.2.3