From bcf6d395d44638997f1fd42e173d11200d49907f Mon Sep 17 00:00:00 2001 From: Klemek Date: Thu, 30 Aug 2018 21:22:06 +0100 Subject: [PATCH] Added maven framework --- .gitignore | 2 +- pom.xml | 75 ++ .../klemek/betterlists/BetterArrayList.java | 188 ++-- .../BetterCopyOnWriteArrayList.java | 0 .../klemek/betterlists/BetterLinkedList.java | 172 ++-- .../fr/klemek/betterlists/BetterList.java | 914 +++++++++--------- .../fr/klemek/betterlists/BetterStack.java | 92 +- .../fr/klemek/betterlists/BetterVector.java | 218 ++--- .../klemek/betterlists}/BetterListsTests.java | 3 +- 9 files changed, 869 insertions(+), 795 deletions(-) create mode 100644 pom.xml rename src/{ => main/java}/fr/klemek/betterlists/BetterArrayList.java (97%) rename src/{ => main/java}/fr/klemek/betterlists/BetterCopyOnWriteArrayList.java (100%) rename src/{ => main/java}/fr/klemek/betterlists/BetterLinkedList.java (97%) rename src/{ => main/java}/fr/klemek/betterlists/BetterList.java (97%) rename src/{ => main/java}/fr/klemek/betterlists/BetterStack.java (97%) rename src/{ => main/java}/fr/klemek/betterlists/BetterVector.java (97%) rename {test/fr/klemek/betterlists/test => src/test/java/fr/klemek/betterlists}/BetterListsTests.java (99%) diff --git a/.gitignore b/.gitignore index 2b984b6..9c333a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/out/ +/target/ /maven-repo.bat /.idea/ *.iml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ac76b79 --- /dev/null +++ b/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + fr.klemek + betterlists + 1.4 + + BetterLists + + + UTF-8 + + + + + + junit + junit + 4.12 + test + + + + + target/${project.artifactId}/WEB-INF/classes + src/main/java + src/test/java + + + + org.codehaus.mojo + versions-maven-plugin + 2.5 + + + validate + + display-dependency-updates + display-plugin-updates + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + download + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.0 + + + + + \ No newline at end of file diff --git a/src/fr/klemek/betterlists/BetterArrayList.java b/src/main/java/fr/klemek/betterlists/BetterArrayList.java similarity index 97% rename from src/fr/klemek/betterlists/BetterArrayList.java rename to src/main/java/fr/klemek/betterlists/BetterArrayList.java index b8f4789..36aec18 100644 --- a/src/fr/klemek/betterlists/BetterArrayList.java +++ b/src/main/java/fr/klemek/betterlists/BetterArrayList.java @@ -1,94 +1,94 @@ -package fr.klemek.betterlists; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; - -/** - * An extension of the java.util.ArrayList class which include some of the C# - * LINQ useful functions. - * - * @author Klemek - * @see java.util.ArrayList - */ -public class BetterArrayList extends ArrayList implements BetterList { - - private static final long serialVersionUID = 4772544470059394618L; - - /** - * Constructs a list containing the elements of the specified collection, in the - * order they are returned by the collection's iterator. - * - * @param c - the collection whose elements are to be placed into this list - */ - public static BetterArrayList fromList(Collection c) { - return new BetterArrayList<>(c); - } - - /** - * Constructs a list containing the elements given in argument. - * - * @param a - the elements to be placed into this list - */ - public static BetterArrayList asList(T... a) { - return new BetterArrayList<>(a); - } - - /** - * Constructs an empty list with an initial capacity of ten. - */ - public BetterArrayList() { - super(); - } - - /** - * Constructs a list containing the elements of the specified collection, in the - * order they are returned by the collection's iterator. - * - * @param c - the collection whose elements are to be placed into this list - */ - public BetterArrayList(Collection c) { - super(c); - } - - /** - * Constructs a list containing the elements given in argument. - * - * @param a - the elements to be placed into this list - */ - public BetterArrayList(T... a) { - super(Arrays.asList(a)); - } - - /** - * Constructs an empty list with the specified initial capacity. - * - * @param initialCapacity - the initial capacity of the list - */ - public BetterArrayList(int initialCapacity) { - super(initialCapacity); - } - - /** - * Returns a view of the portion of this list between the specified fromIndex, - * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the - * returned list is empty.) The returned list is backed by this list, so - * non-structural changes in the returned list are reflected in this list, and - * vice-versa. The returned list supports all of the optional list operations - * supported by this list. This method eliminates the need for explicit range - * operations (of the sort that commonly exist for arrays). Any operation that - * expects a list can be used as a range operation by passing a subList view - * instead of a whole list. (see List.subList) - * - * @param fromIndex - low endpoint (inclusive) of the subList - * @param toIndex - high endpoint (exclusive) of the subList - * @return a view of the specified range within this list - * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > - * size || fromIndex > toIndex) - * @see java.util.List - */ - @Override - public BetterArrayList subList(int fromIndex, int toIndex) { - return (BetterArrayList) super.subList(fromIndex, toIndex); - } -} +package fr.klemek.betterlists; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +/** + * An extension of the java.util.ArrayList class which include some of the C# + * LINQ useful functions. + * + * @author Klemek + * @see java.util.ArrayList + */ +public class BetterArrayList extends ArrayList implements BetterList { + + private static final long serialVersionUID = 4772544470059394618L; + + /** + * Constructs a list containing the elements of the specified collection, in the + * order they are returned by the collection's iterator. + * + * @param c - the collection whose elements are to be placed into this list + */ + public static BetterArrayList fromList(Collection c) { + return new BetterArrayList<>(c); + } + + /** + * Constructs a list containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public static BetterArrayList asList(T... a) { + return new BetterArrayList<>(a); + } + + /** + * Constructs an empty list with an initial capacity of ten. + */ + public BetterArrayList() { + super(); + } + + /** + * Constructs a list containing the elements of the specified collection, in the + * order they are returned by the collection's iterator. + * + * @param c - the collection whose elements are to be placed into this list + */ + public BetterArrayList(Collection c) { + super(c); + } + + /** + * Constructs a list containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public BetterArrayList(T... a) { + super(Arrays.asList(a)); + } + + /** + * Constructs an empty list with the specified initial capacity. + * + * @param initialCapacity - the initial capacity of the list + */ + public BetterArrayList(int initialCapacity) { + super(initialCapacity); + } + + /** + * Returns a view of the portion of this list between the specified fromIndex, + * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the + * returned list is empty.) The returned list is backed by this list, so + * non-structural changes in the returned list are reflected in this list, and + * vice-versa. The returned list supports all of the optional list operations + * supported by this list. This method eliminates the need for explicit range + * operations (of the sort that commonly exist for arrays). Any operation that + * expects a list can be used as a range operation by passing a subList view + * instead of a whole list. (see List.subList) + * + * @param fromIndex - low endpoint (inclusive) of the subList + * @param toIndex - high endpoint (exclusive) of the subList + * @return a view of the specified range within this list + * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > + * size || fromIndex > toIndex) + * @see java.util.List + */ + @Override + public BetterArrayList subList(int fromIndex, int toIndex) { + return (BetterArrayList) super.subList(fromIndex, toIndex); + } +} diff --git a/src/fr/klemek/betterlists/BetterCopyOnWriteArrayList.java b/src/main/java/fr/klemek/betterlists/BetterCopyOnWriteArrayList.java similarity index 100% rename from src/fr/klemek/betterlists/BetterCopyOnWriteArrayList.java rename to src/main/java/fr/klemek/betterlists/BetterCopyOnWriteArrayList.java diff --git a/src/fr/klemek/betterlists/BetterLinkedList.java b/src/main/java/fr/klemek/betterlists/BetterLinkedList.java similarity index 97% rename from src/fr/klemek/betterlists/BetterLinkedList.java rename to src/main/java/fr/klemek/betterlists/BetterLinkedList.java index cc3c6be..8a0e5cf 100644 --- a/src/fr/klemek/betterlists/BetterLinkedList.java +++ b/src/main/java/fr/klemek/betterlists/BetterLinkedList.java @@ -1,86 +1,86 @@ -package fr.klemek.betterlists; - -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedList; - -/** - * An extension of the java.util.LinkedList class which include some of the C# - * LINQ useful functions. - * - * @author Klemek - * @see java.util.LinkedList - */ -public class BetterLinkedList extends LinkedList implements BetterList { - - private static final long serialVersionUID = 4837198308074701770L; - - /** - * Constructs a list containing the elements of the specified collection, in the - * order they are returned by the collection's iterator. - * - * @param c - the collection whose elements are to be placed into this list - */ - public static BetterLinkedList fromList(Collection c) { - return new BetterLinkedList<>(c); - } - - /** - * Constructs a list containing the elements given in argument. - * - * @param a - the elements to be placed into this list - */ - public static BetterLinkedList asList(T... a) { - return new BetterLinkedList<>(a); - } - - /** - * Constructs an empty list. - */ - public BetterLinkedList() { - super(); - } - - /** - * Constructs a list containing the elements of the specified collection, in the - * order they are returned by the collection's iterator. - * - * @param c - the collection whose elements are to be placed into this list - */ - public BetterLinkedList(Collection c) { - super(c); - } - - /** - * Constructs a list containing the elements given in argument. - * - * @param a - the elements to be placed into this list - */ - public BetterLinkedList(T... a) { - super(Arrays.asList(a)); - } - - /** - * Returns a view of the portion of this list between the specified fromIndex, - * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the - * returned list is empty.) The returned list is backed by this list, so - * non-structural changes in the returned list are reflected in this list, and - * vice-versa. The returned list supports all of the optional list operations - * supported by this list. This method eliminates the need for explicit range - * operations (of the sort that commonly exist for arrays). Any operation that - * expects a list can be used as a range operation by passing a subList view - * instead of a whole list. (see List.subList) - * - * @param fromIndex - low endpoint (inclusive) of the subList - * @param toIndex - high endpoint (exclusive) of the subList - * @return a view of the specified range within this list - * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > - * size || fromIndex > toIndex) - * @see java.util.List - */ - @Override - public BetterLinkedList subList(int fromIndex, int toIndex) { - return (BetterLinkedList) super.subList(fromIndex, toIndex); - } - -} +package fr.klemek.betterlists; + +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedList; + +/** + * An extension of the java.util.LinkedList class which include some of the C# + * LINQ useful functions. + * + * @author Klemek + * @see java.util.LinkedList + */ +public class BetterLinkedList extends LinkedList implements BetterList { + + private static final long serialVersionUID = 4837198308074701770L; + + /** + * Constructs a list containing the elements of the specified collection, in the + * order they are returned by the collection's iterator. + * + * @param c - the collection whose elements are to be placed into this list + */ + public static BetterLinkedList fromList(Collection c) { + return new BetterLinkedList<>(c); + } + + /** + * Constructs a list containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public static BetterLinkedList asList(T... a) { + return new BetterLinkedList<>(a); + } + + /** + * Constructs an empty list. + */ + public BetterLinkedList() { + super(); + } + + /** + * Constructs a list containing the elements of the specified collection, in the + * order they are returned by the collection's iterator. + * + * @param c - the collection whose elements are to be placed into this list + */ + public BetterLinkedList(Collection c) { + super(c); + } + + /** + * Constructs a list containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public BetterLinkedList(T... a) { + super(Arrays.asList(a)); + } + + /** + * Returns a view of the portion of this list between the specified fromIndex, + * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the + * returned list is empty.) The returned list is backed by this list, so + * non-structural changes in the returned list are reflected in this list, and + * vice-versa. The returned list supports all of the optional list operations + * supported by this list. This method eliminates the need for explicit range + * operations (of the sort that commonly exist for arrays). Any operation that + * expects a list can be used as a range operation by passing a subList view + * instead of a whole list. (see List.subList) + * + * @param fromIndex - low endpoint (inclusive) of the subList + * @param toIndex - high endpoint (exclusive) of the subList + * @return a view of the specified range within this list + * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > + * size || fromIndex > toIndex) + * @see java.util.List + */ + @Override + public BetterLinkedList subList(int fromIndex, int toIndex) { + return (BetterLinkedList) super.subList(fromIndex, toIndex); + } + +} diff --git a/src/fr/klemek/betterlists/BetterList.java b/src/main/java/fr/klemek/betterlists/BetterList.java similarity index 97% rename from src/fr/klemek/betterlists/BetterList.java rename to src/main/java/fr/klemek/betterlists/BetterList.java index 3da76dc..70306bd 100644 --- a/src/fr/klemek/betterlists/BetterList.java +++ b/src/main/java/fr/klemek/betterlists/BetterList.java @@ -1,457 +1,457 @@ -package fr.klemek.betterlists; - -import java.util.*; -import java.util.function.Function; - -/** - * An extension of the java.util.List interface which include some of the C# - * LINQ useful functions. - * - * @author Klemek - * @see java.util.List - */ -public interface BetterList extends List { - - /** - * Determines whether all elements of the sequence satisfy a condition. - * - * @param predicate - A function to test each element for a condition. - * @return true if every element of the source sequence passes the test in the - * specified predicate, or if the sequence is empty; otherwise, false. - */ - default boolean all(Function predicate) { - for (T element : this) - if (!predicate.apply(element)) - return false; - return true; - } - - /** - * Determines whether any element of the sequence satisfies a condition. - * - * @param predicate - A function to test each element for a condition. - * @return true if any elements in the source sequence pass the test in the - * specified predicate; otherwise, false. - */ - default boolean any(Function predicate) { - for (T element : this) - if (predicate.apply(element)) - return true; - return false; - } - - /** - * Returns the number of elements in the sequence. - * - * @return The number of elements in the input sequence. - */ - default int count() { - return count(e -> true); - } - - /** - * Returns a number that represents how many elements in the specified sequence - * satisfy a condition. - * - * @param predicate - A function to test each element for a condition. - * @return A number that represents how many elements in the sequence satisfy - * the condition in the predicate function. - */ - default int count(Function predicate) { - int out = 0; - for (T element : this) - if (predicate.apply(element)) - out++; - return out; - } - - /** - * Produces the set exclusion of two sequences. - * - * @param other - Another List whose distinct elements form the second set for the - * exclusion. - * @return A List that contains the elements from the first sequence not present - * in the other. - */ - default BetterList exclusion(List other) { - BetterList out = new BetterArrayList<>(); - for (T element : this) - if (!other.contains(element)) - out.add(element); - return out; - } - - /** - * Returns the first element in the sequence. - * - * @return The first element in the sequence. - * @throws NoSuchElementException If the sequence is empty. - */ - default T first() { - return first(e -> true); - } - - /** - * Returns the first element in the sequence that satisfies a specified - * condition. - * - * @param predicate - A function to test each element for a condition. - * @return The first element in the sequence that passes the test in the - * specified predicate function. - * @throws NoSuchElementException No element satisfies the condition in predicate or the sequence - * is empty. - */ - default T first(Function predicate) { - for (T element : this) - if (predicate.apply(element)) - return element; - throw new NoSuchElementException(); - } - - /** - * Returns the first element of the sequence that satisfies a condition or the - * default value if no such element is found. - * - * @param predicate - A function to test each element for a condition. - * @param defaultValue - A default value to be returned if no element passes the test - * @return defaultValue if the sequence is empty or if no element passes the - * test specified by predicate; otherwise, the first element in the - * sequence that passes the test specified by predicate. - */ - default T firstOrDefault(Function predicate, T defaultValue) { - for (T element : this) - if (predicate.apply(element)) - return element; - return defaultValue; - } - - /** - * Returns the first element of the sequence or a default value if the sequence - * is empty. - * - * @param defaultValue - A default value to be returned if the sequence is empty - * @return defaultValue if the sequence is empty otherwise, the first element in - * the sequence. - */ - default T firstOrDefault(T defaultValue) { - return firstOrDefault(e -> true, defaultValue); - } - - /** - * Returns the last element of the sequence. - * - * @return the last element of the sequence. - * @throws NoSuchElementException If the sequence is empty. - */ - default T last() { - return last(e -> true); - } - - /** - * Returns the last element of the sequence that satisfies a specified - * condition. - * - * @param predicate - A function to test each element for a condition. - * @return the last element of the sequence that satisfies a specified - * condition. - * @throws NoSuchElementException No element satisfies the condition in predicate or the sequence - * is empty. - */ - default T last(Function predicate) { - T value = null; - for (T element : this) - if (predicate.apply(element)) - value = element; - if (value == null) - throw new NoSuchElementException(); - return value; - } - - /** - * Returns the last element of the sequence that satisfies a condition or the - * default value if no such element is found. - * - * @param predicate - A function to test each element for a condition. - * @param defaultValue - A default value to be returned if no element passes the test - * @return defaultValue if the sequence is empty or if no element passes the - * test specified by predicate; otherwise, the last element in the - * sequence that passes the test specified by predicate. - */ - default T lastOrDefault(Function predicate, T defaultValue) { - T value = null; - for (T element : this) - if (predicate.apply(element)) - value = element; - return value == null ? defaultValue : value; - } - - /** - * Returns the last element of the sequence or a default value if the sequence - * is empty. - * - * @param defaultValue - A default value to be returned if the sequence is empty - * @return defaultValue if the sequence is empty otherwise, the last element in - * the sequence. - */ - default T lastOrDefault(T defaultValue) { - return lastOrDefault(e -> true, defaultValue); - } - - /** - * Invokes a transform function on each element of the sequence and returns the - * maximum nullable Double value. - * - * @param selector - A transform function to apply to each element. - * @return The value of type Double that corresponds to the maximum value in the - * sequence or null if the sequence is empty. - */ - default Double max(Function selector) { - Double max = null; - for (T element : this) - if (max == null || selector.apply(element) > max) - max = selector.apply(element); - return max; - } - - /** - * Computes the mean of the sequence of Double values that are obtained by - * invoking a transform function on each element of the input sequence. - * - * @param selector - A transform function to apply to each element. - * @return The mean of the projected values. Null if the sequence contains no - * elements. - */ - default Double mean(Function selector) { - int count = this.count(); - if (count == 0) - return null; - return this.sum(selector) / this.count(); - } - - /** - * Invokes a transform function on each element of the sequence and returns the - * minimum nullable Double value. - * - * @param selector - A transform function to apply to each element. - * @return The value of type Double that corresponds to the minimum value in the - * sequence or null if the sequence is empty. - */ - default Double min(Function selector) { - Double min = null; - for (T element : this) - if (min == null || selector.apply(element) < min) - min = selector.apply(element); - return min; - } - - /** - * Sorts the elements of a sequence in ascending order by using a specified comparer. - * - * @param selector - A transform function to apply to each element. - * @return a List whose elements are sorted according to a key. - */ - default > BetterList orderBy(Function selector) { - BetterList out = new BetterArrayList<>(); - out.addAll(this); - Collections.sort(out, (o1, o2) -> selector.apply(o1).compareTo(selector.apply(o2))); - return out; - } - - /** - * Sorts the elements of a sequence in descending order by using a specified comparer. - * - * @param selector - A transform function to apply to each element. - * @return a List whose elements are sorted according to a key. - */ - default > BetterList orderByDescending(Function selector) { - BetterList out = new BetterArrayList<>(); - out.addAll(this); - Collections.sort(out, (o1, o2) -> selector.apply(o2).compareTo(selector.apply(o1))); - return out; - } - - /** - * Inverts the order of the elements in the sequence. - * - * @return A sequence whose elements correspond to those of the sequence in - * reverse order. - */ - default BetterList reverse() { - BetterList out = new BetterArrayList<>(this.size()); - for (T element : this) - out.add(0, element); - return out; - } - - /** - * Projects each element of a sequence into a new form. - * - * @param The type of the projected values - * @param selector - A transform function to apply to each element. - * @return A List whose elements are the result of invoking the transform - * function on each element of the sequence. - */ - default BetterList select(Function selector) { - BetterList out = new BetterArrayList<>(); - for (T element : this) - out.add(selector.apply(element)); - return out; - } - - /** - * Projects each element of a sequence into a new list and flattens the - * resulting sequences into one sequence. - * - * @param The type of the projected values lists - * @param selector - A transform function to apply to each element. - * @return A List whose elements are the result of invoking the one-to-many - * transform function on each element of the input sequence. - */ - default BetterList selectMany(Function> selector) { - BetterList out = new BetterArrayList<>(); - for (T element : this) - out.addAll(selector.apply(element)); - return out; - } - - /** - * Projects each element of a sequence into a new list and flattens the - * resulting sequences into one sequence. - * - * @param The type of the projected values lists - * @param selector - A transform function to apply to each element. - * @return A List whose elements are the result of invoking the one-to-many - * transform function on each element of the input sequence. - */ - default BetterList selectManyArrays(Function selector) { - BetterList out = new BetterArrayList<>(); - for (T element : this) - out.addAll(Arrays.asList(selector.apply(element))); - return out; - } - - /** - * Bypasses a specified number of elements in the sequence and then returns the - * remaining elements. - * - * @param count - The number of elements to skip before returning the remaining - * elements. - * @return a List that contains the elements that occur after the specified - * index in the sequence. - */ - default BetterList skip(int count) { - BetterList out = new BetterArrayList<>(); - int n = 0; - for (T element : this) { - if (n >= count) - out.add(element); - n++; - } - return out; - } - - /** - * Bypasses elements in the sequence as long as a specified condition is true - * and then returns the remaining elements. - * - * @param predicate - A function to test each element for a condition. - * @return a List that contains the elements from the sequence starting at the - * first element in the linear series that does not pass the test - * specified by predicate. - */ - default BetterList skipWhile(Function predicate) { - BetterList out = new BetterArrayList<>(); - boolean match = true; - for (T element : this) - if (!match || !predicate.apply(element)) { - match = false; - out.add(element); - } - return out; - } - - /** - * Computes the sum of the sequence of Double values that are obtained by - * invoking a transform function on each element of the input sequence. - * - * @param selector - A transform function to apply to each element. - * @return The sum of the projected values. Zero if the sequence contains no - * elements. - */ - default Double sum(Function selector) { - double sum = 0d; - for (T element : this) - sum += selector.apply(element); - return sum; - } - - /** - * Returns a specified number of contiguous elements from the start of the - * sequence. - * - * @param count - The number of elements to return. - * @return a List that contains the specified number of elements from the start - * of the input sequence. - */ - default BetterList take(int count) { - BetterList out = new BetterArrayList<>(count); - int n = 0; - for (T element : this) { - if (n < count) - out.add(element); - else - break; - n++; - } - return out; - } - - /** - * Returns elements from the sequence as long as a specified condition is true. - * - * @param predicate - A function to test each element for a condition. - * @return a List that contains the elements from the sequence that occur before - * the element at which the test no longer passes. - */ - default BetterList takeWhile(Function predicate) { - BetterList out = new BetterArrayList<>(); - for (T element : this) - if (predicate.apply(element)) - out.add(element); - else - break; - return out; - } - - /** - * Produces the set union of two sequences. - * - * @param other - Another List whose distinct elements form the second set for the - * union. - * @return A List that contains the elements from both sequences, excluding - * duplicates. - */ - default BetterList union(List other) { - BetterList out = new BetterArrayList<>(); - for (T element : this) - if (other.contains(element)) - out.add(element); - return out; - } - - /** - * Filters a sequence of values based on a predicate. - * - * @param predicate - A function to test each element for a condition. - * @return a List that contains elements from the sequence that satisfy the - * condition. - */ - default BetterList where(Function predicate) { - BetterList out = new BetterArrayList<>(); - for (T element : this) - if (predicate.apply(element)) - out.add(element); - return out; - } - -} +package fr.klemek.betterlists; + +import java.util.*; +import java.util.function.Function; + +/** + * An extension of the java.util.List interface which include some of the C# + * LINQ useful functions. + * + * @author Klemek + * @see java.util.List + */ +public interface BetterList extends List { + + /** + * Determines whether all elements of the sequence satisfy a condition. + * + * @param predicate - A function to test each element for a condition. + * @return true if every element of the source sequence passes the test in the + * specified predicate, or if the sequence is empty; otherwise, false. + */ + default boolean all(Function predicate) { + for (T element : this) + if (!predicate.apply(element)) + return false; + return true; + } + + /** + * Determines whether any element of the sequence satisfies a condition. + * + * @param predicate - A function to test each element for a condition. + * @return true if any elements in the source sequence pass the test in the + * specified predicate; otherwise, false. + */ + default boolean any(Function predicate) { + for (T element : this) + if (predicate.apply(element)) + return true; + return false; + } + + /** + * Returns the number of elements in the sequence. + * + * @return The number of elements in the input sequence. + */ + default int count() { + return count(e -> true); + } + + /** + * Returns a number that represents how many elements in the specified sequence + * satisfy a condition. + * + * @param predicate - A function to test each element for a condition. + * @return A number that represents how many elements in the sequence satisfy + * the condition in the predicate function. + */ + default int count(Function predicate) { + int out = 0; + for (T element : this) + if (predicate.apply(element)) + out++; + return out; + } + + /** + * Produces the set exclusion of two sequences. + * + * @param other - Another List whose distinct elements form the second set for the + * exclusion. + * @return A List that contains the elements from the first sequence not present + * in the other. + */ + default BetterList exclusion(List other) { + BetterList out = new BetterArrayList<>(); + for (T element : this) + if (!other.contains(element)) + out.add(element); + return out; + } + + /** + * Returns the first element in the sequence. + * + * @return The first element in the sequence. + * @throws NoSuchElementException If the sequence is empty. + */ + default T first() { + return first(e -> true); + } + + /** + * Returns the first element in the sequence that satisfies a specified + * condition. + * + * @param predicate - A function to test each element for a condition. + * @return The first element in the sequence that passes the test in the + * specified predicate function. + * @throws NoSuchElementException No element satisfies the condition in predicate or the sequence + * is empty. + */ + default T first(Function predicate) { + for (T element : this) + if (predicate.apply(element)) + return element; + throw new NoSuchElementException(); + } + + /** + * Returns the first element of the sequence that satisfies a condition or the + * default value if no such element is found. + * + * @param predicate - A function to test each element for a condition. + * @param defaultValue - A default value to be returned if no element passes the test + * @return defaultValue if the sequence is empty or if no element passes the + * test specified by predicate; otherwise, the first element in the + * sequence that passes the test specified by predicate. + */ + default T firstOrDefault(Function predicate, T defaultValue) { + for (T element : this) + if (predicate.apply(element)) + return element; + return defaultValue; + } + + /** + * Returns the first element of the sequence or a default value if the sequence + * is empty. + * + * @param defaultValue - A default value to be returned if the sequence is empty + * @return defaultValue if the sequence is empty otherwise, the first element in + * the sequence. + */ + default T firstOrDefault(T defaultValue) { + return firstOrDefault(e -> true, defaultValue); + } + + /** + * Returns the last element of the sequence. + * + * @return the last element of the sequence. + * @throws NoSuchElementException If the sequence is empty. + */ + default T last() { + return last(e -> true); + } + + /** + * Returns the last element of the sequence that satisfies a specified + * condition. + * + * @param predicate - A function to test each element for a condition. + * @return the last element of the sequence that satisfies a specified + * condition. + * @throws NoSuchElementException No element satisfies the condition in predicate or the sequence + * is empty. + */ + default T last(Function predicate) { + T value = null; + for (T element : this) + if (predicate.apply(element)) + value = element; + if (value == null) + throw new NoSuchElementException(); + return value; + } + + /** + * Returns the last element of the sequence that satisfies a condition or the + * default value if no such element is found. + * + * @param predicate - A function to test each element for a condition. + * @param defaultValue - A default value to be returned if no element passes the test + * @return defaultValue if the sequence is empty or if no element passes the + * test specified by predicate; otherwise, the last element in the + * sequence that passes the test specified by predicate. + */ + default T lastOrDefault(Function predicate, T defaultValue) { + T value = null; + for (T element : this) + if (predicate.apply(element)) + value = element; + return value == null ? defaultValue : value; + } + + /** + * Returns the last element of the sequence or a default value if the sequence + * is empty. + * + * @param defaultValue - A default value to be returned if the sequence is empty + * @return defaultValue if the sequence is empty otherwise, the last element in + * the sequence. + */ + default T lastOrDefault(T defaultValue) { + return lastOrDefault(e -> true, defaultValue); + } + + /** + * Invokes a transform function on each element of the sequence and returns the + * maximum nullable Double value. + * + * @param selector - A transform function to apply to each element. + * @return The value of type Double that corresponds to the maximum value in the + * sequence or null if the sequence is empty. + */ + default Double max(Function selector) { + Double max = null; + for (T element : this) + if (max == null || selector.apply(element) > max) + max = selector.apply(element); + return max; + } + + /** + * Computes the mean of the sequence of Double values that are obtained by + * invoking a transform function on each element of the input sequence. + * + * @param selector - A transform function to apply to each element. + * @return The mean of the projected values. Null if the sequence contains no + * elements. + */ + default Double mean(Function selector) { + int count = this.count(); + if (count == 0) + return null; + return this.sum(selector) / this.count(); + } + + /** + * Invokes a transform function on each element of the sequence and returns the + * minimum nullable Double value. + * + * @param selector - A transform function to apply to each element. + * @return The value of type Double that corresponds to the minimum value in the + * sequence or null if the sequence is empty. + */ + default Double min(Function selector) { + Double min = null; + for (T element : this) + if (min == null || selector.apply(element) < min) + min = selector.apply(element); + return min; + } + + /** + * Sorts the elements of a sequence in ascending order by using a specified comparer. + * + * @param selector - A transform function to apply to each element. + * @return a List whose elements are sorted according to a key. + */ + default > BetterList orderBy(Function selector) { + BetterList out = new BetterArrayList<>(); + out.addAll(this); + Collections.sort(out, (o1, o2) -> selector.apply(o1).compareTo(selector.apply(o2))); + return out; + } + + /** + * Sorts the elements of a sequence in descending order by using a specified comparer. + * + * @param selector - A transform function to apply to each element. + * @return a List whose elements are sorted according to a key. + */ + default > BetterList orderByDescending(Function selector) { + BetterList out = new BetterArrayList<>(); + out.addAll(this); + Collections.sort(out, (o1, o2) -> selector.apply(o2).compareTo(selector.apply(o1))); + return out; + } + + /** + * Inverts the order of the elements in the sequence. + * + * @return A sequence whose elements correspond to those of the sequence in + * reverse order. + */ + default BetterList reverse() { + BetterList out = new BetterArrayList<>(this.size()); + for (T element : this) + out.add(0, element); + return out; + } + + /** + * Projects each element of a sequence into a new form. + * + * @param The type of the projected values + * @param selector - A transform function to apply to each element. + * @return A List whose elements are the result of invoking the transform + * function on each element of the sequence. + */ + default BetterList select(Function selector) { + BetterList out = new BetterArrayList<>(); + for (T element : this) + out.add(selector.apply(element)); + return out; + } + + /** + * Projects each element of a sequence into a new list and flattens the + * resulting sequences into one sequence. + * + * @param The type of the projected values lists + * @param selector - A transform function to apply to each element. + * @return A List whose elements are the result of invoking the one-to-many + * transform function on each element of the input sequence. + */ + default BetterList selectMany(Function> selector) { + BetterList out = new BetterArrayList<>(); + for (T element : this) + out.addAll(selector.apply(element)); + return out; + } + + /** + * Projects each element of a sequence into a new list and flattens the + * resulting sequences into one sequence. + * + * @param The type of the projected values lists + * @param selector - A transform function to apply to each element. + * @return A List whose elements are the result of invoking the one-to-many + * transform function on each element of the input sequence. + */ + default BetterList selectManyArrays(Function selector) { + BetterList out = new BetterArrayList<>(); + for (T element : this) + out.addAll(Arrays.asList(selector.apply(element))); + return out; + } + + /** + * Bypasses a specified number of elements in the sequence and then returns the + * remaining elements. + * + * @param count - The number of elements to skip before returning the remaining + * elements. + * @return a List that contains the elements that occur after the specified + * index in the sequence. + */ + default BetterList skip(int count) { + BetterList out = new BetterArrayList<>(); + int n = 0; + for (T element : this) { + if (n >= count) + out.add(element); + n++; + } + return out; + } + + /** + * Bypasses elements in the sequence as long as a specified condition is true + * and then returns the remaining elements. + * + * @param predicate - A function to test each element for a condition. + * @return a List that contains the elements from the sequence starting at the + * first element in the linear series that does not pass the test + * specified by predicate. + */ + default BetterList skipWhile(Function predicate) { + BetterList out = new BetterArrayList<>(); + boolean match = true; + for (T element : this) + if (!match || !predicate.apply(element)) { + match = false; + out.add(element); + } + return out; + } + + /** + * Computes the sum of the sequence of Double values that are obtained by + * invoking a transform function on each element of the input sequence. + * + * @param selector - A transform function to apply to each element. + * @return The sum of the projected values. Zero if the sequence contains no + * elements. + */ + default Double sum(Function selector) { + double sum = 0d; + for (T element : this) + sum += selector.apply(element); + return sum; + } + + /** + * Returns a specified number of contiguous elements from the start of the + * sequence. + * + * @param count - The number of elements to return. + * @return a List that contains the specified number of elements from the start + * of the input sequence. + */ + default BetterList take(int count) { + BetterList out = new BetterArrayList<>(count); + int n = 0; + for (T element : this) { + if (n < count) + out.add(element); + else + break; + n++; + } + return out; + } + + /** + * Returns elements from the sequence as long as a specified condition is true. + * + * @param predicate - A function to test each element for a condition. + * @return a List that contains the elements from the sequence that occur before + * the element at which the test no longer passes. + */ + default BetterList takeWhile(Function predicate) { + BetterList out = new BetterArrayList<>(); + for (T element : this) + if (predicate.apply(element)) + out.add(element); + else + break; + return out; + } + + /** + * Produces the set union of two sequences. + * + * @param other - Another List whose distinct elements form the second set for the + * union. + * @return A List that contains the elements from both sequences, excluding + * duplicates. + */ + default BetterList union(List other) { + BetterList out = new BetterArrayList<>(); + for (T element : this) + if (other.contains(element)) + out.add(element); + return out; + } + + /** + * Filters a sequence of values based on a predicate. + * + * @param predicate - A function to test each element for a condition. + * @return a List that contains elements from the sequence that satisfy the + * condition. + */ + default BetterList where(Function predicate) { + BetterList out = new BetterArrayList<>(); + for (T element : this) + if (predicate.apply(element)) + out.add(element); + return out; + } + +} diff --git a/src/fr/klemek/betterlists/BetterStack.java b/src/main/java/fr/klemek/betterlists/BetterStack.java similarity index 97% rename from src/fr/klemek/betterlists/BetterStack.java rename to src/main/java/fr/klemek/betterlists/BetterStack.java index 9650aa2..6cf2ade 100644 --- a/src/fr/klemek/betterlists/BetterStack.java +++ b/src/main/java/fr/klemek/betterlists/BetterStack.java @@ -1,46 +1,46 @@ -package fr.klemek.betterlists; - -import java.util.Stack; - -/** - * An extension of the java.util.Stack class which include some of the C# LINQ - * useful functions. - * - * @author Klemek - * @see java.util.Stack - */ -public class BetterStack extends Stack implements BetterList { - - private static final long serialVersionUID = 5642889973315247461L; - - /** - * Creates an empty Stack. - */ - public BetterStack() { - super(); - } - - /** - * Returns a view of the portion of this list between the specified fromIndex, - * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the - * returned list is empty.) The returned list is backed by this list, so - * non-structural changes in the returned list are reflected in this list, and - * vice-versa. The returned list supports all of the optional list operations - * supported by this list. This method eliminates the need for explicit range - * operations (of the sort that commonly exist for arrays). Any operation that - * expects a list can be used as a range operation by passing a subList view - * instead of a whole list. (see List.subList) - * - * @param fromIndex - low endpoint (inclusive) of the subList - * @param toIndex - high endpoint (exclusive) of the subList - * @return a view of the specified range within this list - * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > - * size || fromIndex > toIndex) - * @see java.util.List - */ - @Override - public BetterStack subList(int fromIndex, int toIndex) { - return (BetterStack) super.subList(fromIndex, toIndex); - } - -} +package fr.klemek.betterlists; + +import java.util.Stack; + +/** + * An extension of the java.util.Stack class which include some of the C# LINQ + * useful functions. + * + * @author Klemek + * @see java.util.Stack + */ +public class BetterStack extends Stack implements BetterList { + + private static final long serialVersionUID = 5642889973315247461L; + + /** + * Creates an empty Stack. + */ + public BetterStack() { + super(); + } + + /** + * Returns a view of the portion of this list between the specified fromIndex, + * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the + * returned list is empty.) The returned list is backed by this list, so + * non-structural changes in the returned list are reflected in this list, and + * vice-versa. The returned list supports all of the optional list operations + * supported by this list. This method eliminates the need for explicit range + * operations (of the sort that commonly exist for arrays). Any operation that + * expects a list can be used as a range operation by passing a subList view + * instead of a whole list. (see List.subList) + * + * @param fromIndex - low endpoint (inclusive) of the subList + * @param toIndex - high endpoint (exclusive) of the subList + * @return a view of the specified range within this list + * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > + * size || fromIndex > toIndex) + * @see java.util.List + */ + @Override + public BetterStack subList(int fromIndex, int toIndex) { + return (BetterStack) super.subList(fromIndex, toIndex); + } + +} diff --git a/src/fr/klemek/betterlists/BetterVector.java b/src/main/java/fr/klemek/betterlists/BetterVector.java similarity index 97% rename from src/fr/klemek/betterlists/BetterVector.java rename to src/main/java/fr/klemek/betterlists/BetterVector.java index 5a32d9d..6027c81 100644 --- a/src/fr/klemek/betterlists/BetterVector.java +++ b/src/main/java/fr/klemek/betterlists/BetterVector.java @@ -1,109 +1,109 @@ -package fr.klemek.betterlists; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Vector; - -/** - * An extension of the java.util.Vector class which include some of the C# LINQ - * useful functions. - * - * @author Klemek - * @see java.util.Vector - */ -public class BetterVector extends Vector implements BetterList { - - private static final long serialVersionUID = -704157461726911759L; - - /** - * Constructs a vector containing the elements of the specified collection, in - * the order they are returned by the collection's iterator. - * - * @param c - the collection whose elements are to be placed into this vector - */ - public static BetterVector fromList(Collection c) { - return new BetterVector<>(c); - } - - /** - * Constructs a vector containing the elements given in argument. - * - * @param a - the elements to be placed into this vector - */ - public static BetterVector asVector(T... a) { - return new BetterVector<>(a); - } - - /** - * Constructs an empty vector so that its internal data array has size 10 and - * its standard capacity increment is zero. - */ - public BetterVector() { - super(); - } - - /** - * Constructs a vector containing the elements of the specified collection, in - * the order they are returned by the collection's iterator. - * - * @param c - the collection whose elements are to be placed into this vector - */ - public BetterVector(Collection c) { - super(c); - } - - /** - * Constructs a vector containing the elements given in argument. - * - * @param a - the elements to be placed into this list - */ - public BetterVector(T... a) { - super(Arrays.asList(a)); - } - - /** - * Constructs an empty vector with the specified initial capacity and with its - * capacity increment equal to zero. - * - * @param initialCapacity - the initial capacity of the vector - */ - public BetterVector(int initialCapacity) { - super(initialCapacity); - } - - /** - * Constructs an empty vector with the specified initial capacity and capacity - * increment. - * - * @param initialCapacity - the initial capacity of the vector - * @param capacityIncrement - the amount by which the capacity is increased when the vector - * overflows - */ - public BetterVector(int initialCapacity, int capacityIncrement) { - super(initialCapacity, capacityIncrement); - } - - /** - * Returns a view of the portion of this list between the specified fromIndex, - * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the - * returned list is empty.) The returned list is backed by this list, so - * non-structural changes in the returned list are reflected in this list, and - * vice-versa. The returned list supports all of the optional list operations - * supported by this list. This method eliminates the need for explicit range - * operations (of the sort that commonly exist for arrays). Any operation that - * expects a list can be used as a range operation by passing a subList view - * instead of a whole list. (see List.subList) - * - * @param fromIndex - low endpoint (inclusive) of the subList - * @param toIndex - high endpoint (exclusive) of the subList - * @return a view of the specified range within this list - * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > - * size || fromIndex > toIndex) - * @see java.util.List - */ - @Override - public BetterVector subList(int fromIndex, int toIndex) { - return (BetterVector) super.subList(fromIndex, toIndex); - } - -} +package fr.klemek.betterlists; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Vector; + +/** + * An extension of the java.util.Vector class which include some of the C# LINQ + * useful functions. + * + * @author Klemek + * @see java.util.Vector + */ +public class BetterVector extends Vector implements BetterList { + + private static final long serialVersionUID = -704157461726911759L; + + /** + * Constructs a vector containing the elements of the specified collection, in + * the order they are returned by the collection's iterator. + * + * @param c - the collection whose elements are to be placed into this vector + */ + public static BetterVector fromList(Collection c) { + return new BetterVector<>(c); + } + + /** + * Constructs a vector containing the elements given in argument. + * + * @param a - the elements to be placed into this vector + */ + public static BetterVector asVector(T... a) { + return new BetterVector<>(a); + } + + /** + * Constructs an empty vector so that its internal data array has size 10 and + * its standard capacity increment is zero. + */ + public BetterVector() { + super(); + } + + /** + * Constructs a vector containing the elements of the specified collection, in + * the order they are returned by the collection's iterator. + * + * @param c - the collection whose elements are to be placed into this vector + */ + public BetterVector(Collection c) { + super(c); + } + + /** + * Constructs a vector containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public BetterVector(T... a) { + super(Arrays.asList(a)); + } + + /** + * Constructs an empty vector with the specified initial capacity and with its + * capacity increment equal to zero. + * + * @param initialCapacity - the initial capacity of the vector + */ + public BetterVector(int initialCapacity) { + super(initialCapacity); + } + + /** + * Constructs an empty vector with the specified initial capacity and capacity + * increment. + * + * @param initialCapacity - the initial capacity of the vector + * @param capacityIncrement - the amount by which the capacity is increased when the vector + * overflows + */ + public BetterVector(int initialCapacity, int capacityIncrement) { + super(initialCapacity, capacityIncrement); + } + + /** + * Returns a view of the portion of this list between the specified fromIndex, + * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the + * returned list is empty.) The returned list is backed by this list, so + * non-structural changes in the returned list are reflected in this list, and + * vice-versa. The returned list supports all of the optional list operations + * supported by this list. This method eliminates the need for explicit range + * operations (of the sort that commonly exist for arrays). Any operation that + * expects a list can be used as a range operation by passing a subList view + * instead of a whole list. (see List.subList) + * + * @param fromIndex - low endpoint (inclusive) of the subList + * @param toIndex - high endpoint (exclusive) of the subList + * @return a view of the specified range within this list + * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > + * size || fromIndex > toIndex) + * @see java.util.List + */ + @Override + public BetterVector subList(int fromIndex, int toIndex) { + return (BetterVector) super.subList(fromIndex, toIndex); + } + +} diff --git a/test/fr/klemek/betterlists/test/BetterListsTests.java b/src/test/java/fr/klemek/betterlists/BetterListsTests.java similarity index 99% rename from test/fr/klemek/betterlists/test/BetterListsTests.java rename to src/test/java/fr/klemek/betterlists/BetterListsTests.java index 72e1f01..528ca57 100644 --- a/test/fr/klemek/betterlists/test/BetterListsTests.java +++ b/src/test/java/fr/klemek/betterlists/BetterListsTests.java @@ -1,6 +1,5 @@ -package fr.klemek.betterlists.test; +package fr.klemek.betterlists; -import fr.klemek.betterlists.BetterArrayList; import org.junit.Assert; import org.junit.Test;