From 275a627719fc2231b16caea41130ff09f0f2b6a1 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 8 Apr 2016 13:28:09 -0400 Subject: Switch functional data to use interfaces --- .../java/bjc/utils/funcdata/FunctionalList.java | 316 ++++++--------------- 1 file changed, 94 insertions(+), 222 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 c680879..9574376 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -14,6 +14,7 @@ import java.util.function.Predicate; import bjc.utils.data.GenHolder; import bjc.utils.data.IHolder; +import bjc.utils.data.IPair; import bjc.utils.data.Pair; import java.util.ArrayList; @@ -28,7 +29,7 @@ import java.util.ArrayList; * @param * The type in this list */ -public class FunctionalList implements Cloneable { +public class FunctionalList implements Cloneable, IFunctionalList { /** * The list used as a backing store */ @@ -56,27 +57,6 @@ public class FunctionalList implements Cloneable { } } - /** - * Create a functional list using the same backing list as the provided - * list. - * - * @param sourceList - * The source for a backing list - */ - public FunctionalList(FunctionalList sourceList) { - if (sourceList == null) { - throw new NullPointerException("Source list must be non-null"); - } - - // Find out if this should make a copy of the source's wrapped list - // instead. - // - // I've decided it shouldn't. Call clone() if that's what you want. - // This method just creates another list that refers to the - // source's backing list - wrappedList = sourceList.wrappedList; - } - /** * Create a new functional list with the specified size. * @@ -102,26 +82,18 @@ public class FunctionalList implements Cloneable { wrappedList = backingList; } - /** - * Add an item to this list - * - * @param item - * The item to add to this list. - * @return Whether the item was added to the list succesfully. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#add(E) */ + @Override public boolean add(E item) { return wrappedList.add(item); } - /** - * Check if all of the elements of this list match the specified - * predicate. - * - * @param matchPredicate - * The predicate to use for checking. - * @return Whether all of the elements of the list match the specified - * predicate. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#allMatch(java.util.function.Predicate) */ + @Override public boolean allMatch(Predicate matchPredicate) { if (matchPredicate == null) { throw new NullPointerException("Predicate must be non-null"); @@ -138,14 +110,10 @@ public class FunctionalList implements Cloneable { return true; } - /** - * Check if any of the elements in this list match the specified list. - * - * @param matchPredicate - * The predicate to use for checking. - * @return Whether any element in the list matches the provided - * predicate. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#anyMatch(java.util.function.Predicate) */ + @Override public boolean anyMatch(Predicate matchPredicate) { if (matchPredicate == null) { throw new NullPointerException("Predicate must be not null"); @@ -163,8 +131,8 @@ public class FunctionalList implements Cloneable { } @Override - public FunctionalList clone() { - FunctionalList clonedList = new FunctionalList<>(); + public IFunctionalList clone() { + IFunctionalList clonedList = new FunctionalList<>(); for (E element : wrappedList) { clonedList.add(element); @@ -173,27 +141,12 @@ public class FunctionalList implements Cloneable { return clonedList; } - /** - * 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. - * Does not change the underlying list. - * - * NOTE: The returned list will have the length of the shorter of this - * list and the combined one. - * - * @param - * The type of the second list - * @param - * The type of the combined list - * - * @param rightList - * The list to combine with - * @param itemCombiner - * The function to use for combining element pairs. - * @return A new list containing the merged pairs of lists. - */ - public FunctionalList combineWith( - FunctionalList rightList, + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#combineWith(bjc.utils.funcdata.IFunctionalList, java.util.function.BiFunction) + */ + @Override + public IFunctionalList combineWith( + IFunctionalList rightList, BiFunction itemCombiner) { if (rightList == null) { throw new NullPointerException( @@ -202,7 +155,7 @@ public class FunctionalList implements Cloneable { throw new NullPointerException("Combiner must not be null"); } - FunctionalList returnedList = new FunctionalList<>(); + IFunctionalList returnedList = new FunctionalList<>(); // Get the iterator for the other list Iterator rightIterator = rightList.toIterable().iterator(); @@ -220,23 +173,19 @@ public class FunctionalList implements Cloneable { return returnedList; } - /** - * 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 + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#contains(E) */ + @Override public boolean contains(E item) { // Check if any items in the list match the provided item return this.anyMatch(item::equals); } - /** - * Get the first element in the list - * - * @return The first element in this list. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#first() */ + @Override public E first() { if (wrappedList.size() < 1) { throw new NoSuchElementException( @@ -246,29 +195,21 @@ public class FunctionalList implements Cloneable { return wrappedList.get(0); } - /** - * Apply a function to each member of the list, then flatten the - * results. Does not change the underlying list. - * - * @param - * The type of the flattened list - * - * @param elementExpander - * The function to apply to each member of the list. - * @return A new list containing the flattened results of applying the - * provided function. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#flatMap(java.util.function.Function) */ - public FunctionalList flatMap( - Function> elementExpander) { + @Override + public IFunctionalList flatMap( + Function> elementExpander) { if (elementExpander == null) { throw new NullPointerException("Expander must not be null"); } - FunctionalList returnedList = new FunctionalList<>( + IFunctionalList returnedList = new FunctionalList<>( this.wrappedList.size()); forEach(element -> { - FunctionalList expandedElement = elementExpander + IFunctionalList expandedElement = elementExpander .apply(element); if (expandedElement == null) { @@ -283,12 +224,10 @@ public class FunctionalList implements Cloneable { return returnedList; } - /** - * Apply a given action for each member of the list - * - * @param action - * The action to apply to each member of the list. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#forEach(java.util.function.Consumer) */ + @Override public void forEach(Consumer action) { if (action == null) { throw new NullPointerException("Action is null"); @@ -297,13 +236,10 @@ public class FunctionalList implements Cloneable { wrappedList.forEach(action); } - /** - * Apply a given function to each element in the list and its index. - * - * @param indexedAction - * The function to apply to each element in the list and its - * index. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#forEachIndexed(java.util.function.BiConsumer) */ + @Override public void forEachIndexed(BiConsumer indexedAction) { if (indexedAction == null) { throw new NullPointerException("Action must not be null"); @@ -322,13 +258,10 @@ public class FunctionalList implements Cloneable { }); } - /** - * Retrieve a value in the list by its index. - * - * @param index - * The index to retrieve a value from. - * @return The value at the specified index in the list. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#getByIndex(int) */ + @Override public E getByIndex(int index) { return wrappedList.get(index); } @@ -342,19 +275,16 @@ public class FunctionalList implements Cloneable { return wrappedList; } - /** - * Retrieve a list containing all elements matching a predicate - * - * @param matchPredicate - * The predicate to match by - * @return A list containing all elements that match the predicate + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#getMatching(java.util.function.Predicate) */ - public FunctionalList getMatching(Predicate matchPredicate) { + @Override + public IFunctionalList getMatching(Predicate matchPredicate) { if (matchPredicate == null) { throw new NullPointerException("Predicate must not be null"); } - FunctionalList returnedList = new FunctionalList<>(); + IFunctionalList returnedList = new FunctionalList<>(); wrappedList.forEach((element) -> { if (matchPredicate.test(element)) { @@ -366,41 +296,32 @@ public class FunctionalList implements Cloneable { return returnedList; } - /** - * Retrieve the size of the wrapped list - * - * @return The size of the wrapped list + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#getSize() */ + @Override public int getSize() { return wrappedList.size(); } - /** - * Check if this list is empty. - * - * @return Whether or not this list is empty. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#isEmpty() */ + @Override public boolean isEmpty() { return wrappedList.isEmpty(); } - /** - * Create a new list by applying the given function to each element in - * the list. Does not change the underlying list. - * - * @param - * The type of the transformed list - * - * @param elementTransformer - * The function to apply to each element in the list - * @return A new list containing the mapped elements of this list. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#map(java.util.function.Function) */ - public FunctionalList map(Function elementTransformer) { + @Override + public IFunctionalList map(Function elementTransformer) { if (elementTransformer == null) { throw new NullPointerException("Transformer must be not null"); } - FunctionalList returnedList = new FunctionalList<>( + IFunctionalList returnedList = new FunctionalList<>( this.wrappedList.size()); forEach(element -> { @@ -411,30 +332,20 @@ public class FunctionalList implements Cloneable { return returnedList; } - /** - * Zip two lists into a list of pairs - * - * @param - * The type of the second list - * - * @param rightList - * The list to use as the left side of the pair - * @return A list containing pairs of this element and the specified - * list + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#pairWith(bjc.utils.funcdata.IFunctionalList) */ - public FunctionalList> pairWith( - FunctionalList rightList) { + @Override + public IFunctionalList> pairWith( + IFunctionalList rightList) { return combineWith(rightList, Pair::new); } - /** - * Partition this list into a list of sublists - * - * @param numberPerPartition - * The size of elements to put into each one of the sublists - * @return A list partitioned into partitions of size nPerPart + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#partition(int) */ - public FunctionalList> partition( + @Override + public IFunctionalList> partition( int numberPerPartition) { if (numberPerPartition < 1 || numberPerPartition > wrappedList.size()) { @@ -443,10 +354,10 @@ public class FunctionalList implements Cloneable { + wrappedList.size()); } - FunctionalList> returnedList = new FunctionalList<>(); + IFunctionalList> returnedList = new FunctionalList<>(); // The current partition being filled - GenHolder> currentPartition = new GenHolder<>( + GenHolder> currentPartition = new GenHolder<>( new FunctionalList<>()); this.forEach((element) -> { @@ -472,29 +383,23 @@ public class FunctionalList implements Cloneable { * Check if a partition has room for another item */ private Boolean isPartitionFull(int numberPerPartition, - GenHolder> currentPartition) { + GenHolder> currentPartition) { return currentPartition.unwrap( (partition) -> partition.getSize() >= numberPerPartition); } - /** - * Prepend an item to the list - * - * @param item - * The item to prepend to the list + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#prepend(E) */ + @Override public void prepend(E item) { wrappedList.add(0, item); } - /** - * Select a random item from this list, using the provided random - * number generator. - * - * @param rnd - * The random number generator to use. - * @return A random element from this list. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#randItem(java.util.Random) */ + @Override public E randItem(Random rnd) { if (rnd == null) { throw new NullPointerException( @@ -506,25 +411,10 @@ public class FunctionalList implements Cloneable { return wrappedList.get(randomIndex); } - /** - * Reduce this list to a single value, using a accumulative approach. - * - * @param - * The in-between type of the values - * @param - * The final value type - * - * @param initialValue - * The initial value of the accumulative state. - * @param stateAccumulator - * The function to use to combine a list element with the - * accumulative state. - * @param resultTransformer - * The function to use to convert the accumulative state - * into a final result. - * @return A single value condensed from this list and transformed into - * its final state. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#reduceAux(T, java.util.function.BiFunction, java.util.function.Function) */ + @Override public F reduceAux(T initialValue, BiFunction stateAccumulator, Function resultTransformer) { @@ -547,13 +437,10 @@ public class FunctionalList implements Cloneable { return currentState.unwrap(resultTransformer); } - /** - * Remove all elements that match a given predicate - * - * @param removePredicate - * The predicate to use to determine elements to delete - * @return Whether there was anything that satisfied the predicate + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#removeIf(java.util.function.Predicate) */ + @Override public boolean removeIf(Predicate removePredicate) { if (removePredicate == null) { throw new NullPointerException("Predicate must be non-null"); @@ -562,28 +449,18 @@ public class FunctionalList implements Cloneable { return wrappedList.removeIf(removePredicate); } - /** - * Remove all parameters that match a given parameter - * - * @param desiredElement - * The object to remove all matching copies of + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#removeMatching(E) */ + @Override public void removeMatching(E desiredElement) { removeIf((element) -> element.equals(desiredElement)); } - /** - * 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. - * - * @param searchKey - * The key to search for. - * @param comparator - * The way to compare elements for searching. Pass null to - * use the natural ordering for E - * @return The element if it is in this list, or null if it is not. + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#search(E, java.util.Comparator) */ + @Override public E search(E searchKey, Comparator comparator) { // Search our internal list int foundIndex = Collections.binarySearch(wrappedList, searchKey, @@ -598,23 +475,18 @@ public class FunctionalList implements Cloneable { return null; } - /** - * Sort the elements of this list using the provided way of comparing - * elements. Does change the underlying list. - * - * @param comparator - * The way to compare elements for sorting. Pass null to use - * E's natural ordering + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#sort(java.util.Comparator) */ + @Override public void sort(Comparator comparator) { Collections.sort(wrappedList, comparator); } - /** - * Convert the list into a iterable - * - * @return An iterable view onto the list + /* (non-Javadoc) + * @see bjc.utils.funcdata.IFunctionalList#toIterable() */ + @Override public Iterable toIterable() { return wrappedList; } -- cgit v1.2.3