From c9f641a4a4a01866819e1c30dea7ebc7cb142be7 Mon Sep 17 00:00:00 2001 From: Klemek Date: Mon, 18 Jun 2018 14:54:05 +0200 Subject: [PATCH] BetterLists 1.3 --- README.md | 16 +- download/betterlists-1.3.jar | Bin 0 -> 8726 bytes .../klemek/betterlists/BetterArrayList.java | 103 ++- .../BetterCopyOnWriteArrayList.java | 86 ++ .../klemek/betterlists/BetterLinkedList.java | 60 +- src/fr/klemek/betterlists/BetterList.java | 775 +++++++++--------- src/fr/klemek/betterlists/BetterStack.java | 64 +- src/fr/klemek/betterlists/BetterVector.java | 178 ++-- .../betterlists/test/BetterListsTests.java | 556 +++++++------ 9 files changed, 955 insertions(+), 883 deletions(-) create mode 100644 download/betterlists-1.3.jar create mode 100644 src/fr/klemek/betterlists/BetterCopyOnWriteArrayList.java diff --git a/README.md b/README.md index 84180e6..55a4322 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ An extension of the java.util.List interface which include some of the C# LINQ u List classes are extended as well. (ArrayList -> BetterArrayList) -Current version v1.2 +Current version v1.3 Before BetterLists : ```Java @@ -30,7 +30,7 @@ NOTE : Please note that, unlike C# LINQ, these functions are not optimized at lo ## Download -* [betterlists-1.2.jar](../../raw/master/download/betterlists-1.2.jar) +* [betterlists-1.3.jar](../../raw/master/download/betterlists-1.3.jar) ## Maven @@ -49,7 +49,7 @@ You can use this project as a maven dependency with this : klemek betterlists - 1.2 + 1.3 ``` @@ -71,6 +71,7 @@ You can use this project as a maven dependency with this : | [orderBy / orderByDescending](#orderby-orderbydescending) | Sorts the elements of a sequence in ascending order by using a specified comparator. (You can user orderByDescending to change the order) | | [reverse](#reverse) | Inverts the order of the elements in the sequence. | | [select](#select) | Projects each element of a sequence into a new form. | +| [selectMany](#selectmany) | Projects each element of a sequence into a new list and flattens the resulting sequences into one sequence. | | [skip / skipWhile](#skip-skipwhile) | Bypasses elements in the sequence as long as a specified condition is true and then returns the remaining elements. | | [sum](#sum) | Computes the sum of the sequence of Double values that are obtained by invoking a transform function on each element of the input sequence. | | [take / takeWhile](#take-takewhile) | Returns a specified number of contiguous elements from the start of the sequence. | @@ -177,6 +178,15 @@ BetterArrayList contacts = BetterArrayList.fromList(someFunction()); BetterList contactsMails = contacts.select(c -> c.getEmail()); ``` +### selectMany +Projects each element of a sequence into a new list and flattens the resulting sequences into one sequence. +```Java +BetterArrayList contacts = BetterArrayList.fromList(someFunction()); + +BetterList contactsMails = contacts.select(c -> c.getEmail().split(";")); +``` + + ### skip / skipWhile Bypasses elements in the sequence as long as a specified condition is true and then returns the remaining elements. ```Java diff --git a/download/betterlists-1.3.jar b/download/betterlists-1.3.jar new file mode 100644 index 0000000000000000000000000000000000000000..f891b8cc82c500838e67ea4e4bb604f3c7cb7514 GIT binary patch literal 8726 zcmaiZ1yEeuwk_@im&V=Q-CctRcXtWy(l`W{;1&q(Zo%ClSa7%CP9EG>|0nmBF(Uto7j|+o^pJZ^!eDG; z=aRuxmM0|mDQ_K%UYrWq8izgC2t9f+84@cw*``rqGa>c` zdF{FnM?Pvo;F0tRqo22Rxlp%3Yx-*Zc*ScX&FggZ>FM&C*WaTIwh14k2j31842Eb3 z(Wj_t(A?54x#_mT-xQP2P5LL(-MJbop>c()RLN zr+^#-lgp{aPEdyO6d;+Tng5_ZvG_0|eK1i?jo~mjE^61cuD64RaPToaud?)=b>%3KSWJ_FMORtWbILsGcBWEp;I6)UB~{=fTVFAtCm zy)LV=mY~U2ETWcQdG9J4uQx|^VM^0!09C4N$$4H?C0RXG=aobu^to_;@kzNakpW|o zXK1#=PR?lGO_)f$F)U(3FH*Vbw$4j|np|9w)tz)oo@7#=8Ut9;aLePv)6%UT8qpNG z%8SydEUE1>eC6!_sq}aoSWmVU00P zKi$Ss+gB9Zb1{9aAy!Glyi1C^V)~ok=8sdL5cIU5CN3;E?#@^B#03oBBq+Sr^o=_N zG}|QPUaq31a7SlNA&M}=vWwUQBGlA9v*hvmks?684CP8?ny8>o9(0^w zg!#T;Fj0%eaS_&B5P^$G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7RLL3`VKxQ!x@^}a2P#yhAR*|D(cmd!7 zR}?9IjF%B8w!W#2CNI zs}^6kaN?uo@sT89Rh%!1TatZGca%78$ME9y@|ySkj8uGW(v6IQ5%eycx6b*m?&8yq zp~*<7u%<#p{8&!D2+Yx}~!#9iJ6`-l7c z*AhHH<@Q-5vE&=K?!)y|Dk=4aWFuP&v~71^t^$@mLWGfJhcP7nDuReRMkVj`4?&tY z^OwBwF!78Glq_;v93}PQ6KR0=k$uAS3c>&nY7fPuID*AvKd8)~$Ij^q>Ifb=<@tsC z#@y{e%NLVZ)}H@IY6Jy?1NnRF{FT0c;Qt%xe;@Q_rT+%`>+nxw{qN)6to7fJe;xjg z`){Ki6bv2Yx7Gfq{r6_I|3<*;@c+*7e;V)aWq&7ra}WQtIo{mE-++er=fr>SCH{Gi zw@Ue^==)}v{DwR9KcC~T_Q_u}|Jm5zMC0EWfBlF4>9PE_Wd54-&x-%HCHM_7z(42w pV}tP5q<=2Pw`TDhez^af^v~AuUKSGSEfV(iGw`aSa}&P(`X33dievx) literal 0 HcmV?d00001 diff --git a/src/fr/klemek/betterlists/BetterArrayList.java b/src/fr/klemek/betterlists/BetterArrayList.java index a525e13..b8f4789 100644 --- a/src/fr/klemek/betterlists/BetterArrayList.java +++ b/src/fr/klemek/betterlists/BetterArrayList.java @@ -7,25 +7,23 @@ import java.util.Collection; /** * An extension of the java.util.ArrayList class which include some of the C# * LINQ useful functions. - * - * @author Klemek * + * @author Klemek * @see java.util.ArrayList */ public class BetterArrayList extends ArrayList implements BetterList { - private static final long serialVersionUID = 4772544470059394618L; + 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 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. @@ -36,29 +34,27 @@ public class BetterArrayList extends ArrayList implements BetterList { return new BetterArrayList<>(a); } - /** - * Constructs an empty list with an initial capacity of ten. - */ - public BetterArrayList() { - super(); - } + /** + * 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 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 + * @param a - the elements to be placed into this list */ public BetterArrayList(T... a) { super(Arrays.asList(a)); @@ -73,29 +69,26 @@ public class BetterArrayList extends ArrayList implements BetterList { 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) { + /** + * 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/fr/klemek/betterlists/BetterCopyOnWriteArrayList.java new file mode 100644 index 0000000..fdb7e5d --- /dev/null +++ b/src/fr/klemek/betterlists/BetterCopyOnWriteArrayList.java @@ -0,0 +1,86 @@ +package fr.klemek.betterlists; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * An extension of the java.concurrent.CopyOnWriteArrayList class which include some of the C# + * LINQ useful functions. + * + * @author Klemek + * @see ArrayList + */ +public class BetterCopyOnWriteArrayList extends CopyOnWriteArrayList implements BetterList { + + private static final long serialVersionUID = -1148672915754560195L; + + /** + * 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 BetterCopyOnWriteArrayList fromList(Collection c) { + return new BetterCopyOnWriteArrayList<>(c); + } + + /** + * Constructs a list containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public static BetterCopyOnWriteArrayList asList(T... a) { + return new BetterCopyOnWriteArrayList<>(a); + } + + /** + * Constructs an empty list with an initial capacity of ten. + */ + public BetterCopyOnWriteArrayList() { + 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 BetterCopyOnWriteArrayList(Collection c) { + super(c); + } + + /** + * Constructs a list containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public BetterCopyOnWriteArrayList(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 BetterCopyOnWriteArrayList subList(int fromIndex, int toIndex) { + return (BetterCopyOnWriteArrayList) super.subList(fromIndex, toIndex); + } +} diff --git a/src/fr/klemek/betterlists/BetterLinkedList.java b/src/fr/klemek/betterlists/BetterLinkedList.java index c572800..cc3c6be 100644 --- a/src/fr/klemek/betterlists/BetterLinkedList.java +++ b/src/fr/klemek/betterlists/BetterLinkedList.java @@ -7,25 +7,23 @@ import java.util.LinkedList; /** * An extension of the java.util.LinkedList class which include some of the C# * LINQ useful functions. - * - * @author Klemek * + * @author Klemek * @see java.util.LinkedList */ public class BetterLinkedList extends LinkedList implements BetterList { - private static final long serialVersionUID = 4837198308074701770L; + 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 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. @@ -36,29 +34,27 @@ public class BetterLinkedList extends LinkedList implements BetterList return new BetterLinkedList<>(a); } - /** - * Constructs an empty list. - */ - public BetterLinkedList() { - super(); - } + /** + * 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 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 + * @param a - the elements to be placed into this list */ public BetterLinkedList(T... a) { super(Arrays.asList(a)); diff --git a/src/fr/klemek/betterlists/BetterList.java b/src/fr/klemek/betterlists/BetterList.java index 9ed74c7..6940787 100644 --- a/src/fr/klemek/betterlists/BetterList.java +++ b/src/fr/klemek/betterlists/BetterList.java @@ -1,456 +1,441 @@ package fr.klemek.betterlists; -import java.util.Collections; -import java.util.List; -import java.util.NoSuchElementException; +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 * + * @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. - */ - public default boolean all(Function predicate) { - for (T element : this) - if (!predicate.apply(element)) - return false; - return true; - } + /** + * 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. - */ + /** + * 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; - } + 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. - */ + /** + * Returns the number of elements in the sequence. + * + * @return The number of elements in the input sequence. + */ default int count() { - return count(e -> true); - } + 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. - */ + /** + * 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; - } + 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. - */ + /** + * 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; - } + BetterList out = new BetterArrayList<>(); + for (T element : this) + if (!other.contains(element)) + out.add(element); + return out; + } - /** - * Returns the first element in the sequence. - * - * @throws NoSuchElementException - * If the sequence is empty. - * @return The first element in the sequence. - */ + /** + * 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); - } + 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. - * @throws NoSuchElementException - * No element satisfies the condition in predicate or the sequence - * is empty. - * @return The first element in the sequence that passes the test in the - * specified predicate function. - */ + /** + * 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(); - } + 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. - */ + /** + * 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; - } + 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. - */ + /** + * 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); - } + return firstOrDefault(e -> true, defaultValue); + } - /** - * Returns the last element of the sequence. - * - * @throws NoSuchElementException - * If the sequence is empty. - * @return the last element of the sequence. - */ + /** + * 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); - } + 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. - * @throws NoSuchElementException - * No element satisfies the condition in predicate or the sequence - * is empty. - * @return the last element of the sequence that satisfies a specified - * condition. - */ + /** + * 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; - } + 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. - */ + /** + * 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; - } + 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. - */ + /** + * 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); - } + 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. - */ + /** + * 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; - } + 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. - */ + /** + * 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(); - } + 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. - */ + /** + * 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. - */ + 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; - } + 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. - */ + /** + * 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. - */ + 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. - */ + 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; - } + BetterList out = new BetterArrayList<>(); + for (T element : this) + out.add(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. - */ + /** + * 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(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; - } + 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. - */ + /** + * 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; - } + 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. - */ + /** + * 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; - } + 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. - */ + /** + * 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; - } + 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. - */ + /** + * 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; - } + BetterList out = new BetterArrayList<>(); + for (T element : this) + if (predicate.apply(element)) + out.add(element); + else + break; + 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. - */ + /** + * 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; - } + 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/fr/klemek/betterlists/BetterStack.java index 82f32e1..9650aa2 100644 --- a/src/fr/klemek/betterlists/BetterStack.java +++ b/src/fr/klemek/betterlists/BetterStack.java @@ -5,46 +5,42 @@ import java.util.Stack; /** * An extension of the java.util.Stack class which include some of the C# LINQ * useful functions. - * - * @author Klemek * + * @author Klemek * @see java.util.Stack */ public class BetterStack extends Stack implements BetterList { - private static final long serialVersionUID = 5642889973315247461L; + private static final long serialVersionUID = 5642889973315247461L; - /** - * Creates an empty Stack. - */ - public BetterStack() { - super(); - } + /** + * 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); - } + /** + * 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/fr/klemek/betterlists/BetterVector.java index 71f372e..5a32d9d 100644 --- a/src/fr/klemek/betterlists/BetterVector.java +++ b/src/fr/klemek/betterlists/BetterVector.java @@ -7,111 +7,103 @@ import java.util.Vector; /** * An extension of the java.util.Vector class which include some of the C# LINQ * useful functions. - * - * @author Klemek * + * @author Klemek * @see java.util.Vector */ public class BetterVector extends Vector implements BetterList { - private static final long serialVersionUID = -704157461726911759L; + 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 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 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 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 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 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 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); - } + /** + * 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); - } + /** + * 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/test/fr/klemek/betterlists/test/BetterListsTests.java index 581519c..e9b517d 100644 --- a/test/fr/klemek/betterlists/test/BetterListsTests.java +++ b/test/fr/klemek/betterlists/test/BetterListsTests.java @@ -1,332 +1,346 @@ package fr.klemek.betterlists.test; -import java.util.ArrayList; -import java.util.NoSuchElementException; - import fr.klemek.betterlists.BetterArrayList; import org.junit.Assert; import org.junit.Test; +import java.util.ArrayList; +import java.util.NoSuchElementException; + public class BetterListsTests { - protected class Dummy { + protected class Dummy { final double d; final String s; - - public Dummy(double d, String s) { - this.d = d; - this.s = s; - } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Dummy other = (Dummy) obj; - if (!getOuterType().equals(other.getOuterType())) - return false; - if (Double.doubleToLongBits(d) != Double.doubleToLongBits(other.d)) - return false; + public Dummy(double d, String s) { + this.d = d; + this.s = s; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Dummy other = (Dummy) obj; + if (!getOuterType().equals(other.getOuterType())) + return false; + if (Double.doubleToLongBits(d) != Double.doubleToLongBits(other.d)) + return false; if (s == null && other.s != null) { - return false; + return false; } else return s.equals(other.s); - } + } - private BetterListsTests getOuterType() { - return BetterListsTests.this; - } + private BetterListsTests getOuterType() { + return BetterListsTests.this; + } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + getOuterType().hashCode(); - long temp; - temp = Double.doubleToLongBits(d); - result = prime * result + (int) (temp ^ (temp >>> 32)); - result = prime * result + ((s == null) ? 0 : s.hashCode()); - return result; - } - } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + getOuterType().hashCode(); + long temp; + temp = Double.doubleToLongBits(d); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + ((s == null) ? 0 : s.hashCode()); + return result; + } + } - @Test - public void testAll() { + @Test + public void testAll() { ArrayList al = new ArrayList<>(); - al.add(new Dummy(1d,"hello")); - al.add(new Dummy(2d,"test")); - al.add(new Dummy(2d,"hello")); - - BetterArrayList bal = BetterArrayList.fromList(al); - - Assert.assertTrue(bal.all(du -> du.d > 0)); - Assert.assertFalse(bal.all(du -> du.d > 1)); - } + al.add(new Dummy(1d, "hello")); + al.add(new Dummy(2d, "test")); + al.add(new Dummy(2d, "hello")); - @Test - public void testAny() { + BetterArrayList bal = BetterArrayList.fromList(al); + + Assert.assertTrue(bal.all(du -> du.d > 0)); + Assert.assertFalse(bal.all(du -> du.d > 1)); + } + + @Test + public void testAny() { BetterArrayList bal = new BetterArrayList<>(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(2d,"hello")); - - Assert.assertTrue(bal.any(du -> du.s.startsWith("t"))); - Assert.assertFalse(bal.any(du -> du.s.startsWith("b"))); - } + bal.add(new Dummy(1d, "hello")); + bal.add(new Dummy(2d, "test")); + bal.add(new Dummy(2d, "hello")); - @Test - public void testCount() { + Assert.assertTrue(bal.any(du -> du.s.startsWith("t"))); + Assert.assertFalse(bal.any(du -> du.s.startsWith("b"))); + } + + @Test + public void testCount() { BetterArrayList bal = new BetterArrayList<>(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(2d,"hello")); - - Assert.assertEquals(3, bal.count()); - Assert.assertEquals(2, bal.count(du -> du.s.length() > 4)); - } + bal.add(new Dummy(1d, "hello")); + bal.add(new Dummy(2d, "test")); + bal.add(new Dummy(2d, "hello")); - @Test - public void testExclude() { + Assert.assertEquals(3, bal.count()); + Assert.assertEquals(2, bal.count(du -> du.s.length() > 4)); + } + + @Test + public void testExclude() { BetterArrayList bal1 = new BetterArrayList<>(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"test")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); + bal1.add(new Dummy(1d, "hello")); + bal1.add(new Dummy(2d, "test")); + bal1.add(new Dummy(3d, "hello")); + bal1.add(new Dummy(4d, "test")); BetterArrayList bal2 = new BetterArrayList<>(); - bal2.add(new Dummy(2d,"test")); - bal2.add(new Dummy(3d,"hello")); - bal2.add(new Dummy(5d,"test")); - - BetterArrayList bal3 = (BetterArrayList) bal1.exclusion(bal2); - Assert.assertEquals(2, bal3.size()); - Assert.assertEquals(bal1.get(0), bal3.get(0)); - Assert.assertEquals(bal1.get(3), bal3.get(1)); - } + bal2.add(new Dummy(2d, "test")); + bal2.add(new Dummy(3d, "hello")); + bal2.add(new Dummy(5d, "test")); - @Test - public void testFirst() { + BetterArrayList bal3 = (BetterArrayList) bal1.exclusion(bal2); + Assert.assertEquals(2, bal3.size()); + Assert.assertEquals(bal1.get(0), bal3.get(0)); + Assert.assertEquals(bal1.get(3), bal3.get(1)); + } + + @Test + public void testFirst() { BetterArrayList bal = new BetterArrayList<>(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(2d,"hello")); + bal.add(new Dummy(1d, "hello")); + bal.add(new Dummy(2d, "test")); + bal.add(new Dummy(2d, "hello")); - Assert.assertEquals(bal.get(0), bal.first(du -> du.s.startsWith("h"))); + Assert.assertEquals(bal.get(0), bal.first(du -> du.s.startsWith("h"))); - try { - bal.first(du -> du.s.startsWith("d")); - Assert.fail("no error"); - } catch (NoSuchElementException e) { - } + try { + bal.first(du -> du.s.startsWith("d")); + Assert.fail("no error"); + } catch (NoSuchElementException e) { + } - Assert.assertEquals(bal.get(0), bal.firstOrDefault(du -> du.s.startsWith("h"), new Dummy(3d,"default"))); - - Assert.assertEquals(new Dummy(3d,"default"), bal.firstOrDefault(du -> du.s.startsWith("d"), new Dummy(3d,"default"))); - } + Assert.assertEquals(bal.get(0), bal.firstOrDefault(du -> du.s.startsWith("h"), new Dummy(3d, "default"))); - @Test - public void testLast() { + Assert.assertEquals(new Dummy(3d, "default"), bal.firstOrDefault(du -> du.s.startsWith("d"), new Dummy(3d, "default"))); + } + + @Test + public void testLast() { BetterArrayList bal = new BetterArrayList<>(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(2d,"hello")); + bal.add(new Dummy(1d, "hello")); + bal.add(new Dummy(2d, "test")); + bal.add(new Dummy(2d, "hello")); - Assert.assertEquals(bal.get(2), bal.last(du -> du.s.startsWith("h"))); - - try { - bal.last(du -> du.s.startsWith("d")); - Assert.fail("no error"); - } catch (NoSuchElementException e) { - } + Assert.assertEquals(bal.get(2), bal.last(du -> du.s.startsWith("h"))); - Assert.assertEquals(bal.get(2), bal.lastOrDefault(du -> du.s.startsWith("h"), new Dummy(3d,"default"))); - - Assert.assertEquals(new Dummy(3d,"default"), bal.lastOrDefault(du -> du.s.startsWith("d"), new Dummy(3d,"default"))); - } + try { + bal.last(du -> du.s.startsWith("d")); + Assert.fail("no error"); + } catch (NoSuchElementException e) { + } - @Test - public void testMax() { + Assert.assertEquals(bal.get(2), bal.lastOrDefault(du -> du.s.startsWith("h"), new Dummy(3d, "default"))); + + Assert.assertEquals(new Dummy(3d, "default"), bal.lastOrDefault(du -> du.s.startsWith("d"), new Dummy(3d, "default"))); + } + + @Test + public void testMax() { BetterArrayList bal = new BetterArrayList<>(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello2")); - - Assert.assertEquals(6d, bal.max(du -> (double)du.s.length()), 0.001d); - } + bal.add(new Dummy(1d, "hello")); + bal.add(new Dummy(2d, "test")); + bal.add(new Dummy(3d, "hello2")); - @Test - public void testMean() { + Assert.assertEquals(6d, bal.max(du -> (double) du.s.length()), 0.001d); + } + + @Test + public void testMean() { BetterArrayList bal = new BetterArrayList<>(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello2")); - - Assert.assertEquals(5d, bal.mean(du -> (double)du.s.length()), 0.001d); - } + bal.add(new Dummy(1d, "hello")); + bal.add(new Dummy(2d, "test")); + bal.add(new Dummy(3d, "hello2")); - @Test - public void testMin() { + Assert.assertEquals(5d, bal.mean(du -> (double) du.s.length()), 0.001d); + } + + @Test + public void testMin() { BetterArrayList bal = new BetterArrayList<>(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello2")); - - Assert.assertEquals(4d, bal.min(du -> (double)du.s.length()), 0.001d); - } + bal.add(new Dummy(1d, "hello")); + bal.add(new Dummy(2d, "test")); + bal.add(new Dummy(3d, "hello2")); - @Test - public void testOrderBy() { + Assert.assertEquals(4d, bal.min(du -> (double) du.s.length()), 0.001d); + } + + @Test + public void testOrderBy() { BetterArrayList bal1 = new BetterArrayList<>(); - bal1.add(new Dummy(1d,"hello1")); - bal1.add(new Dummy(2d,"hello2")); - bal1.add(new Dummy(3d,"hello0")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello4")); - - BetterArrayList bal2 = (BetterArrayList) bal1.orderBy(c -> c.s); - Assert.assertNotEquals(bal1, bal2); - Assert.assertEquals(bal1.get(2), bal2.get(0)); - Assert.assertEquals(bal1.get(0), bal2.get(1)); - Assert.assertEquals(bal1.get(1), bal2.get(2)); - Assert.assertEquals(bal1.get(4), bal2.get(3)); - Assert.assertEquals(bal1.get(3), bal2.get(4)); - } + bal1.add(new Dummy(1d, "hello1")); + bal1.add(new Dummy(2d, "hello2")); + bal1.add(new Dummy(3d, "hello0")); + bal1.add(new Dummy(4d, "test")); + bal1.add(new Dummy(5d, "hello4")); - @Test - public void testOrderByDescending() { - BetterArrayList bal1 = new BetterArrayList<>(); - bal1.add(new Dummy(1d,"hello1")); - bal1.add(new Dummy(2d,"hello2")); - bal1.add(new Dummy(3d,"hello0")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello4")); - - BetterArrayList bal2 = (BetterArrayList) bal1.orderByDescending(c -> c.d); - Assert.assertNotEquals(bal1, bal2); - for(int i = 0; i < 5; i++) - Assert.assertEquals(bal1.get(4-i), bal2.get(i)); - } + BetterArrayList bal2 = (BetterArrayList) bal1.orderBy(c -> c.s); + Assert.assertNotEquals(bal1, bal2); + Assert.assertEquals(bal1.get(2), bal2.get(0)); + Assert.assertEquals(bal1.get(0), bal2.get(1)); + Assert.assertEquals(bal1.get(1), bal2.get(2)); + Assert.assertEquals(bal1.get(4), bal2.get(3)); + Assert.assertEquals(bal1.get(3), bal2.get(4)); + } - @Test - public void testReverse() { + @Test + public void testOrderByDescending() { BetterArrayList bal1 = new BetterArrayList<>(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"hello")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello")); - - BetterArrayList bal2 = (BetterArrayList) bal1.reverse(); - Assert.assertEquals(5, bal2.size()); - for(int i = 0; i < 5; i++) - Assert.assertEquals(bal1.get(i), bal2.get(4-i)); - } + bal1.add(new Dummy(1d, "hello1")); + bal1.add(new Dummy(2d, "hello2")); + bal1.add(new Dummy(3d, "hello0")); + bal1.add(new Dummy(4d, "test")); + bal1.add(new Dummy(5d, "hello4")); - @Test - public void testSelect() { + BetterArrayList bal2 = (BetterArrayList) bal1.orderByDescending(c -> c.d); + Assert.assertNotEquals(bal1, bal2); + for (int i = 0; i < 5; i++) + Assert.assertEquals(bal1.get(4 - i), bal2.get(i)); + } + + @Test + public void testReverse() { BetterArrayList bal1 = new BetterArrayList<>(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"hello")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello")); + bal1.add(new Dummy(1d, "hello")); + bal1.add(new Dummy(2d, "hello")); + bal1.add(new Dummy(3d, "hello")); + bal1.add(new Dummy(4d, "test")); + bal1.add(new Dummy(5d, "hello")); + + BetterArrayList bal2 = (BetterArrayList) bal1.reverse(); + Assert.assertEquals(5, bal2.size()); + for (int i = 0; i < 5; i++) + Assert.assertEquals(bal1.get(i), bal2.get(4 - i)); + } + + @Test + public void testSelect() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d, "hello")); + bal1.add(new Dummy(2d, "hello")); + bal1.add(new Dummy(3d, "hello")); + bal1.add(new Dummy(4d, "test")); + bal1.add(new Dummy(5d, "hello")); BetterArrayList bal2 = (BetterArrayList) bal1.select(du -> du.d); - Assert.assertEquals(5, bal2.size()); - for(int i = 0; i < 5; i++) - Assert.assertEquals(bal1.get(i).d, bal2.get(i), 0.0001); - } + Assert.assertEquals(5, bal2.size()); + for (int i = 0; i < 5; i++) + Assert.assertEquals(bal1.get(i).d, bal2.get(i), 0.0001); + } - @Test - public void testSkip() { + @Test + public void testSelectMany() { BetterArrayList bal1 = new BetterArrayList<>(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"hello")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello")); - - Assert.assertEquals(bal1, bal1.skip(0)); - Assert.assertEquals(0, bal1.skip(10).size()); - - BetterArrayList bal2 = (BetterArrayList) bal1.skip(2); - Assert.assertEquals(3, bal2.size()); - for(int i = 0; i < 3; i++) - Assert.assertEquals(bal1.get(i+2), bal2.get(i)); - - BetterArrayList bal3 = (BetterArrayList) bal1.skipWhile(du -> du.s.startsWith("h")); - Assert.assertEquals(2, bal3.size()); - for(int i = 0; i < 2; i++) - Assert.assertEquals(bal1.get(i+3), bal3.get(i)); - } - - @Test - public void testSum() { + bal1.add(new Dummy(1d, "hel;lo")); + bal1.add(new Dummy(2d, "hel;lo")); + + BetterArrayList bal2 = (BetterArrayList) bal1.selectMany(du -> du.s.split(";")); + Assert.assertEquals(4, bal2.size()); + Assert.assertEquals("hel", bal2.get(0)); + Assert.assertEquals("lo", bal2.get(1)); + Assert.assertEquals("hel", bal2.get(2)); + Assert.assertEquals("lo", bal2.get(3)); + } + + @Test + public void testSkip() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d, "hello")); + bal1.add(new Dummy(2d, "hello")); + bal1.add(new Dummy(3d, "hello")); + bal1.add(new Dummy(4d, "test")); + bal1.add(new Dummy(5d, "hello")); + + Assert.assertEquals(bal1, bal1.skip(0)); + Assert.assertEquals(0, bal1.skip(10).size()); + + BetterArrayList bal2 = (BetterArrayList) bal1.skip(2); + Assert.assertEquals(3, bal2.size()); + for (int i = 0; i < 3; i++) + Assert.assertEquals(bal1.get(i + 2), bal2.get(i)); + + BetterArrayList bal3 = (BetterArrayList) bal1.skipWhile(du -> du.s.startsWith("h")); + Assert.assertEquals(2, bal3.size()); + for (int i = 0; i < 2; i++) + Assert.assertEquals(bal1.get(i + 3), bal3.get(i)); + } + + @Test + public void testSum() { BetterArrayList bal = new BetterArrayList<>(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello2")); - - Assert.assertEquals(6d, bal.sum(du -> du.d), 0.001d); - } - - @Test - public void testTake() { + bal.add(new Dummy(1d, "hello")); + bal.add(new Dummy(2d, "test")); + bal.add(new Dummy(3d, "hello2")); + + Assert.assertEquals(6d, bal.sum(du -> du.d), 0.001d); + } + + @Test + public void testTake() { BetterArrayList bal1 = new BetterArrayList<>(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"hello")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello")); - - Assert.assertEquals(bal1, bal1.take(10)); - - Assert.assertEquals(0, bal1.take(0).size()); - - BetterArrayList bal2 = (BetterArrayList) bal1.take(2); - Assert.assertEquals(2, bal2.size()); - for(int i = 0; i < 2; i++) - Assert.assertEquals(bal1.get(i), bal2.get(i)); - - BetterArrayList bal3 = (BetterArrayList) bal1.takeWhile(du -> du.s.startsWith("h")); - Assert.assertEquals(3, bal3.size()); - for(int i = 0; i < 3; i++) - Assert.assertEquals(bal1.get(i), bal3.get(i)); - } - - @Test - public void testUnion() { + bal1.add(new Dummy(1d, "hello")); + bal1.add(new Dummy(2d, "hello")); + bal1.add(new Dummy(3d, "hello")); + bal1.add(new Dummy(4d, "test")); + bal1.add(new Dummy(5d, "hello")); + + Assert.assertEquals(bal1, bal1.take(10)); + + Assert.assertEquals(0, bal1.take(0).size()); + + BetterArrayList bal2 = (BetterArrayList) bal1.take(2); + Assert.assertEquals(2, bal2.size()); + for (int i = 0; i < 2; i++) + Assert.assertEquals(bal1.get(i), bal2.get(i)); + + BetterArrayList bal3 = (BetterArrayList) bal1.takeWhile(du -> du.s.startsWith("h")); + Assert.assertEquals(3, bal3.size()); + for (int i = 0; i < 3; i++) + Assert.assertEquals(bal1.get(i), bal3.get(i)); + } + + @Test + public void testUnion() { BetterArrayList bal1 = new BetterArrayList<>(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"test")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); + bal1.add(new Dummy(1d, "hello")); + bal1.add(new Dummy(2d, "test")); + bal1.add(new Dummy(3d, "hello")); + bal1.add(new Dummy(4d, "test")); BetterArrayList bal2 = new BetterArrayList<>(); - bal2.add(new Dummy(2d,"test")); - bal2.add(new Dummy(3d,"hello")); - bal2.add(new Dummy(5d,"test")); - - BetterArrayList bal3 = (BetterArrayList) bal1.union(bal2); - Assert.assertEquals(2, bal3.size()); - Assert.assertEquals(bal1.get(1), bal3.get(0)); - Assert.assertEquals(bal1.get(2), bal3.get(1)); - } + bal2.add(new Dummy(2d, "test")); + bal2.add(new Dummy(3d, "hello")); + bal2.add(new Dummy(5d, "test")); - @Test - public void testWhere() { + BetterArrayList bal3 = (BetterArrayList) bal1.union(bal2); + Assert.assertEquals(2, bal3.size()); + Assert.assertEquals(bal1.get(1), bal3.get(0)); + Assert.assertEquals(bal1.get(2), bal3.get(1)); + } + + @Test + public void testWhere() { BetterArrayList bal = new BetterArrayList<>(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello")); + bal.add(new Dummy(1d, "hello")); + bal.add(new Dummy(2d, "test")); + bal.add(new Dummy(3d, "hello")); - BetterArrayList bal2 = (BetterArrayList) bal.where(du -> du.s.startsWith("h")); + BetterArrayList bal2 = (BetterArrayList) bal.where(du -> du.s.startsWith("h")); - Assert.assertEquals(2, bal2.size()); - Assert.assertEquals(new Dummy(1d,"hello"), bal2.get(0)); - Assert.assertEquals(new Dummy(3d,"hello"), bal2.get(1)); - } + Assert.assertEquals(2, bal2.size()); + Assert.assertEquals(new Dummy(1d, "hello"), bal2.get(0)); + Assert.assertEquals(new Dummy(3d, "hello"), bal2.get(1)); + } }