BetterLists 1.3

This commit is contained in:
Klemek
2018-06-18 14:54:05 +02:00
parent c8e15e0943
commit c9f641a4a4
9 changed files with 955 additions and 883 deletions
+13 -3
View File
@@ -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) List classes are extended as well. (ArrayList -> BetterArrayList)
Current version v1.2 Current version v1.3
Before BetterLists : Before BetterLists :
```Java ```Java
@@ -30,7 +30,7 @@ NOTE : Please note that, unlike C# LINQ, these functions are not optimized at lo
## Download ## Download
* [betterlists-1.2.jar](../../raw/master/download/betterlists-1.2.jar) * [betterlists-1.3.jar](../../raw/master/download/betterlists-1.3.jar)
## Maven ## Maven
@@ -49,7 +49,7 @@ You can use this project as a maven dependency with this :
<dependency> <dependency>
<groupId>klemek</groupId> <groupId>klemek</groupId>
<artifactId>betterlists</artifactId> <artifactId>betterlists</artifactId>
<version>1.2</version> <version>1.3</version>
</dependency> </dependency>
</dependencies> </dependencies>
``` ```
@@ -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) | | [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. | | [reverse](#reverse) | Inverts the order of the elements in the sequence. |
| [select](#select) | Projects each element of a sequence into a new form. | | [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. | | [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. | | [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. | | [take / takeWhile](#take-takewhile) | Returns a specified number of contiguous elements from the start of the sequence. |
@@ -177,6 +178,15 @@ BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
BetterList<String> contactsMails = contacts.select(c -> c.getEmail()); BetterList<String> 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<Contact> contacts = BetterArrayList.fromList(someFunction());
BetterList<String> contactsMails = contacts.select(c -> c.getEmail().split(";"));
```
### skip / skipWhile ### skip / skipWhile
Bypasses elements in the sequence as long as a specified condition is true and then returns the remaining elements. Bypasses elements in the sequence as long as a specified condition is true and then returns the remaining elements.
```Java ```Java
Binary file not shown.
+6 -13
View File
@@ -9,7 +9,6 @@ import java.util.Collection;
* LINQ useful functions. * LINQ useful functions.
* *
* @author Klemek * @author Klemek
*
* @see java.util.ArrayList * @see java.util.ArrayList
*/ */
public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> { public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
@@ -20,8 +19,7 @@ public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
* Constructs a list containing the elements of the specified collection, in the * Constructs a list containing the elements of the specified collection, in the
* order they are returned by the collection's iterator. * order they are returned by the collection's iterator.
* *
* @param c * @param c - the collection whose elements are to be placed into this list
* - the collection whose elements are to be placed into this list
*/ */
public static <T> BetterArrayList<T> fromList(Collection<T> c) { public static <T> BetterArrayList<T> fromList(Collection<T> c) {
return new BetterArrayList<>(c); return new BetterArrayList<>(c);
@@ -47,8 +45,7 @@ public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
* Constructs a list containing the elements of the specified collection, in the * Constructs a list containing the elements of the specified collection, in the
* order they are returned by the collection's iterator. * order they are returned by the collection's iterator.
* *
* @param c * @param c - the collection whose elements are to be placed into this list
* - the collection whose elements are to be placed into this list
*/ */
public BetterArrayList(Collection<? extends T> c) { public BetterArrayList(Collection<? extends T> c) {
super(c); super(c);
@@ -57,8 +54,7 @@ public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
/** /**
* Constructs a list containing the elements given in argument. * Constructs a list containing the elements given in argument.
* *
* @param a * @param a - the elements to be placed into this list
* - the elements to be placed into this list
*/ */
public BetterArrayList(T... a) { public BetterArrayList(T... a) {
super(Arrays.asList(a)); super(Arrays.asList(a));
@@ -84,13 +80,10 @@ public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
* expects a list can be used as a range operation by passing a subList view * expects a list can be used as a range operation by passing a subList view
* instead of a whole list. (see List.subList) * instead of a whole list. (see List.subList)
* *
* @param fromIndex * @param fromIndex - low endpoint (inclusive) of the subList
* - low endpoint (inclusive) of the subList * @param toIndex - high endpoint (exclusive) of the subList
* @param toIndex
* - high endpoint (exclusive) of the subList
* @return a view of the specified range within this list * @return a view of the specified range within this list
* @throws IndexOutOfBoundsException * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex >
* for an illegal endpoint index value (fromIndex < 0 || toIndex >
* size || fromIndex > toIndex) * size || fromIndex > toIndex)
* @see java.util.List * @see java.util.List
*/ */
@@ -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<T> extends CopyOnWriteArrayList<T> implements BetterList<T> {
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 <T> BetterCopyOnWriteArrayList<T> fromList(Collection<T> 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 <T> BetterCopyOnWriteArrayList<T> 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<? extends T> 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<T> subList(int fromIndex, int toIndex) {
return (BetterCopyOnWriteArrayList<T>) super.subList(fromIndex, toIndex);
}
}
@@ -9,7 +9,6 @@ import java.util.LinkedList;
* LINQ useful functions. * LINQ useful functions.
* *
* @author Klemek * @author Klemek
*
* @see java.util.LinkedList * @see java.util.LinkedList
*/ */
public class BetterLinkedList<T> extends LinkedList<T> implements BetterList<T> { public class BetterLinkedList<T> extends LinkedList<T> implements BetterList<T> {
@@ -20,8 +19,7 @@ public class BetterLinkedList<T> extends LinkedList<T> implements BetterList<T>
* Constructs a list containing the elements of the specified collection, in the * Constructs a list containing the elements of the specified collection, in the
* order they are returned by the collection's iterator. * order they are returned by the collection's iterator.
* *
* @param c * @param c - the collection whose elements are to be placed into this list
* - the collection whose elements are to be placed into this list
*/ */
public static <T> BetterLinkedList<T> fromList(Collection<T> c) { public static <T> BetterLinkedList<T> fromList(Collection<T> c) {
return new BetterLinkedList<>(c); return new BetterLinkedList<>(c);
@@ -47,8 +45,7 @@ public class BetterLinkedList<T> extends LinkedList<T> implements BetterList<T>
* Constructs a list containing the elements of the specified collection, in the * Constructs a list containing the elements of the specified collection, in the
* order they are returned by the collection's iterator. * order they are returned by the collection's iterator.
* *
* @param c * @param c - the collection whose elements are to be placed into this list
* - the collection whose elements are to be placed into this list
*/ */
public BetterLinkedList(Collection<? extends T> c) { public BetterLinkedList(Collection<? extends T> c) {
super(c); super(c);
@@ -57,8 +54,7 @@ public class BetterLinkedList<T> extends LinkedList<T> implements BetterList<T>
/** /**
* Constructs a list containing the elements given in argument. * Constructs a list containing the elements given in argument.
* *
* @param a * @param a - the elements to be placed into this list
* - the elements to be placed into this list
*/ */
public BetterLinkedList(T... a) { public BetterLinkedList(T... a) {
super(Arrays.asList(a)); super(Arrays.asList(a));
+54 -69
View File
@@ -1,8 +1,6 @@
package fr.klemek.betterlists; package fr.klemek.betterlists;
import java.util.Collections; import java.util.*;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Function; import java.util.function.Function;
/** /**
@@ -10,7 +8,6 @@ import java.util.function.Function;
* LINQ useful functions. * LINQ useful functions.
* *
* @author Klemek * @author Klemek
*
* @see java.util.List * @see java.util.List
*/ */
public interface BetterList<T> extends List<T> { public interface BetterList<T> extends List<T> {
@@ -18,12 +15,11 @@ public interface BetterList<T> extends List<T> {
/** /**
* Determines whether all elements of the sequence satisfy a condition. * Determines whether all elements of the sequence satisfy a condition.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - A function to test each element for a condition.
* @return true if every element of the source sequence passes the test in the * @return true if every element of the source sequence passes the test in the
* specified predicate, or if the sequence is empty; otherwise, false. * specified predicate, or if the sequence is empty; otherwise, false.
*/ */
public default boolean all(Function<T, Boolean> predicate) { default boolean all(Function<T, Boolean> predicate) {
for (T element : this) for (T element : this)
if (!predicate.apply(element)) if (!predicate.apply(element))
return false; return false;
@@ -33,8 +29,7 @@ public interface BetterList<T> extends List<T> {
/** /**
* Determines whether any element of the sequence satisfies a condition. * Determines whether any element of the sequence satisfies a condition.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - A function to test each element for a condition.
* @return true if any elements in the source sequence pass the test in the * @return true if any elements in the source sequence pass the test in the
* specified predicate; otherwise, false. * specified predicate; otherwise, false.
*/ */
@@ -58,8 +53,7 @@ public interface BetterList<T> extends List<T> {
* Returns a number that represents how many elements in the specified sequence * Returns a number that represents how many elements in the specified sequence
* satisfy a condition. * satisfy a condition.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - A function to test each element for a condition.
* @return A number that represents how many elements in the sequence satisfy * @return A number that represents how many elements in the sequence satisfy
* the condition in the predicate function. * the condition in the predicate function.
*/ */
@@ -74,8 +68,7 @@ public interface BetterList<T> extends List<T> {
/** /**
* Produces the set exclusion of two sequences. * Produces the set exclusion of two sequences.
* *
* @param other * @param other - Another List whose distinct elements form the second set for the
* - Another List whose distinct elements form the second set for the
* exclusion. * exclusion.
* @return A List that contains the elements from the first sequence not present * @return A List that contains the elements from the first sequence not present
* in the other. * in the other.
@@ -91,9 +84,8 @@ public interface BetterList<T> extends List<T> {
/** /**
* Returns the first element in the sequence. * Returns the first element in the sequence.
* *
* @throws NoSuchElementException
* If the sequence is empty.
* @return The first element in the sequence. * @return The first element in the sequence.
* @throws NoSuchElementException If the sequence is empty.
*/ */
default T first() { default T first() {
return first(e -> true); return first(e -> true);
@@ -103,13 +95,11 @@ public interface BetterList<T> extends List<T> {
* Returns the first element in the sequence that satisfies a specified * Returns the first element in the sequence that satisfies a specified
* condition. * condition.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - 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 * @return The first element in the sequence that passes the test in the
* specified predicate function. * specified predicate function.
* @throws NoSuchElementException No element satisfies the condition in predicate or the sequence
* is empty.
*/ */
default T first(Function<T, Boolean> predicate) { default T first(Function<T, Boolean> predicate) {
for (T element : this) for (T element : this)
@@ -122,10 +112,8 @@ public interface BetterList<T> extends List<T> {
* Returns the first element of the sequence that satisfies a condition or the * Returns the first element of the sequence that satisfies a condition or the
* default value if no such element is found. * default value if no such element is found.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - A function to test each element for a condition. * @param defaultValue - A default value to be returned if no element passes the test
* @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 * @return defaultValue if the sequence is empty or if no element passes the
* test specified by predicate; otherwise, the first element in the * test specified by predicate; otherwise, the first element in the
* sequence that passes the test specified by predicate. * sequence that passes the test specified by predicate.
@@ -141,8 +129,7 @@ public interface BetterList<T> extends List<T> {
* Returns the first element of the sequence or a default value if the sequence * Returns the first element of the sequence or a default value if the sequence
* is empty. * is empty.
* *
* @param defaultValue * @param defaultValue - A default value to be returned if the sequence is empty
* - A default value to be returned if the sequence is empty
* @return defaultValue if the sequence is empty otherwise, the first element in * @return defaultValue if the sequence is empty otherwise, the first element in
* the sequence. * the sequence.
*/ */
@@ -153,9 +140,8 @@ public interface BetterList<T> extends List<T> {
/** /**
* Returns the last element of the sequence. * Returns the last element of the sequence.
* *
* @throws NoSuchElementException
* If the sequence is empty.
* @return the last element of the sequence. * @return the last element of the sequence.
* @throws NoSuchElementException If the sequence is empty.
*/ */
default T last() { default T last() {
return last(e -> true); return last(e -> true);
@@ -165,13 +151,11 @@ public interface BetterList<T> extends List<T> {
* Returns the last element of the sequence that satisfies a specified * Returns the last element of the sequence that satisfies a specified
* condition. * condition.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - 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 * @return the last element of the sequence that satisfies a specified
* condition. * condition.
* @throws NoSuchElementException No element satisfies the condition in predicate or the sequence
* is empty.
*/ */
default T last(Function<T, Boolean> predicate) { default T last(Function<T, Boolean> predicate) {
T value = null; T value = null;
@@ -187,10 +171,8 @@ public interface BetterList<T> extends List<T> {
* Returns the last element of the sequence that satisfies a condition or the * Returns the last element of the sequence that satisfies a condition or the
* default value if no such element is found. * default value if no such element is found.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - A function to test each element for a condition. * @param defaultValue - A default value to be returned if no element passes the test
* @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 * @return defaultValue if the sequence is empty or if no element passes the
* test specified by predicate; otherwise, the last element in the * test specified by predicate; otherwise, the last element in the
* sequence that passes the test specified by predicate. * sequence that passes the test specified by predicate.
@@ -207,8 +189,7 @@ public interface BetterList<T> extends List<T> {
* Returns the last element of the sequence or a default value if the sequence * Returns the last element of the sequence or a default value if the sequence
* is empty. * is empty.
* *
* @param defaultValue * @param defaultValue - A default value to be returned if the sequence is empty
* - A default value to be returned if the sequence is empty
* @return defaultValue if the sequence is empty otherwise, the last element in * @return defaultValue if the sequence is empty otherwise, the last element in
* the sequence. * the sequence.
*/ */
@@ -220,8 +201,7 @@ public interface BetterList<T> extends List<T> {
* Invokes a transform function on each element of the sequence and returns the * Invokes a transform function on each element of the sequence and returns the
* maximum nullable Double value. * maximum nullable Double value.
* *
* @param selector * @param selector - A transform function to apply to each element.
* - A transform function to apply to each element.
* @return The value of type Double that corresponds to the maximum value in the * @return The value of type Double that corresponds to the maximum value in the
* sequence or null if the sequence is empty. * sequence or null if the sequence is empty.
*/ */
@@ -237,8 +217,7 @@ public interface BetterList<T> extends List<T> {
* Computes the mean of the sequence of Double values that are obtained by * Computes the mean of the sequence of Double values that are obtained by
* invoking a transform function on each element of the input sequence. * invoking a transform function on each element of the input sequence.
* *
* @param selector * @param selector - A transform function to apply to each element.
* - A transform function to apply to each element.
* @return The mean of the projected values. Null if the sequence contains no * @return The mean of the projected values. Null if the sequence contains no
* elements. * elements.
*/ */
@@ -253,8 +232,7 @@ public interface BetterList<T> extends List<T> {
* Invokes a transform function on each element of the sequence and returns the * Invokes a transform function on each element of the sequence and returns the
* minimum nullable Double value. * minimum nullable Double value.
* *
* @param selector * @param selector - A transform function to apply to each element.
* - A transform function to apply to each element.
* @return The value of type Double that corresponds to the minimum value in the * @return The value of type Double that corresponds to the minimum value in the
* sequence or null if the sequence is empty. * sequence or null if the sequence is empty.
*/ */
@@ -268,27 +246,27 @@ public interface BetterList<T> extends List<T> {
/** /**
* Sorts the elements of a sequence in ascending order by using a specified comparer. * Sorts the elements of a sequence in ascending order by using a specified comparer.
* @param selector *
* - A transform function to apply to each element. * @param selector - A transform function to apply to each element.
* @return a List whose elements are sorted according to a key. * @return a List whose elements are sorted according to a key.
*/ */
default <E extends Comparable<E>> BetterList<T> orderBy(Function<T, E> selector) { default <E extends Comparable<E>> BetterList<T> orderBy(Function<T, E> selector) {
BetterList<T> out = new BetterArrayList<>(); BetterList<T> out = new BetterArrayList<>();
out.addAll(this); out.addAll(this);
Collections.sort(out, (o1,o2) -> selector.apply(o1).compareTo(selector.apply(o2))); Collections.sort(out, (o1, o2) -> selector.apply(o1).compareTo(selector.apply(o2)));
return out; return out;
} }
/** /**
* Sorts the elements of a sequence in descending order by using a specified comparer. * Sorts the elements of a sequence in descending order by using a specified comparer.
* @param selector *
* - A transform function to apply to each element. * @param selector - A transform function to apply to each element.
* @return a List whose elements are sorted according to a key. * @return a List whose elements are sorted according to a key.
*/ */
default <E extends Comparable<E>> BetterList<T> orderByDescending(Function<T, E> selector) { default <E extends Comparable<E>> BetterList<T> orderByDescending(Function<T, E> selector) {
BetterList<T> out = new BetterArrayList<>(); BetterList<T> out = new BetterArrayList<>();
out.addAll(this); out.addAll(this);
Collections.sort(out, (o1,o2) -> selector.apply(o2).compareTo(selector.apply(o1))); Collections.sort(out, (o1, o2) -> selector.apply(o2).compareTo(selector.apply(o1)));
return out; return out;
} }
@@ -308,10 +286,8 @@ public interface BetterList<T> extends List<T> {
/** /**
* Projects each element of a sequence into a new form. * Projects each element of a sequence into a new form.
* *
* @param <E> * @param <E> The type of the projected values
* The type of the projected values * @param selector - A transform function to apply to each element.
* @param selector
* - A transform function to apply to each element.
* @return A List whose elements are the result of invoking the transform * @return A List whose elements are the result of invoking the transform
* function on each element of the sequence. * function on each element of the sequence.
*/ */
@@ -322,12 +298,27 @@ public interface BetterList<T> extends List<T> {
return out; return out;
} }
/**
* Projects each element of a sequence into a new list and flattens the
* resulting sequences into one sequence.
*
* @param <E> 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 <E> BetterList<E> selectMany(Function<T, E[]> selector) {
BetterList<E> 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 * Bypasses a specified number of elements in the sequence and then returns the
* remaining elements. * remaining elements.
* *
* @param count * @param count - The number of elements to skip before returning the remaining
* - The number of elements to skip before returning the remaining
* elements. * elements.
* @return a List that contains the elements that occur after the specified * @return a List that contains the elements that occur after the specified
* index in the sequence. * index in the sequence.
@@ -347,8 +338,7 @@ public interface BetterList<T> extends List<T> {
* Bypasses elements in the sequence as long as a specified condition is true * Bypasses elements in the sequence as long as a specified condition is true
* and then returns the remaining elements. * and then returns the remaining elements.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - A function to test each element for a condition.
* @return a List that contains the elements from the sequence starting at the * @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 * first element in the linear series that does not pass the test
* specified by predicate. * specified by predicate.
@@ -368,8 +358,7 @@ public interface BetterList<T> extends List<T> {
* Computes the sum of the sequence of Double values that are obtained by * Computes the sum of the sequence of Double values that are obtained by
* invoking a transform function on each element of the input sequence. * invoking a transform function on each element of the input sequence.
* *
* @param selector * @param selector - A transform function to apply to each element.
* - A transform function to apply to each element.
* @return The sum of the projected values. Zero if the sequence contains no * @return The sum of the projected values. Zero if the sequence contains no
* elements. * elements.
*/ */
@@ -384,8 +373,7 @@ public interface BetterList<T> extends List<T> {
* Returns a specified number of contiguous elements from the start of the * Returns a specified number of contiguous elements from the start of the
* sequence. * sequence.
* *
* @param count * @param count - The number of elements to return.
* - The number of elements to return.
* @return a List that contains the specified number of elements from the start * @return a List that contains the specified number of elements from the start
* of the input sequence. * of the input sequence.
*/ */
@@ -405,8 +393,7 @@ public interface BetterList<T> extends List<T> {
/** /**
* Returns elements from the sequence as long as a specified condition is true. * Returns elements from the sequence as long as a specified condition is true.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - A function to test each element for a condition.
* @return a List that contains the elements from the sequence that occur before * @return a List that contains the elements from the sequence that occur before
* the element at which the test no longer passes. * the element at which the test no longer passes.
*/ */
@@ -423,8 +410,7 @@ public interface BetterList<T> extends List<T> {
/** /**
* Produces the set union of two sequences. * Produces the set union of two sequences.
* *
* @param other * @param other - Another List whose distinct elements form the second set for the
* - Another List whose distinct elements form the second set for the
* union. * union.
* @return A List that contains the elements from both sequences, excluding * @return A List that contains the elements from both sequences, excluding
* duplicates. * duplicates.
@@ -440,8 +426,7 @@ public interface BetterList<T> extends List<T> {
/** /**
* Filters a sequence of values based on a predicate. * Filters a sequence of values based on a predicate.
* *
* @param predicate * @param predicate - A function to test each element for a condition.
* - A function to test each element for a condition.
* @return a List that contains elements from the sequence that satisfy the * @return a List that contains elements from the sequence that satisfy the
* condition. * condition.
*/ */
+3 -7
View File
@@ -7,7 +7,6 @@ import java.util.Stack;
* useful functions. * useful functions.
* *
* @author Klemek * @author Klemek
*
* @see java.util.Stack * @see java.util.Stack
*/ */
public class BetterStack<T> extends Stack<T> implements BetterList<T> { public class BetterStack<T> extends Stack<T> implements BetterList<T> {
@@ -32,13 +31,10 @@ public class BetterStack<T> extends Stack<T> implements BetterList<T> {
* expects a list can be used as a range operation by passing a subList view * expects a list can be used as a range operation by passing a subList view
* instead of a whole list. (see List.subList) * instead of a whole list. (see List.subList)
* *
* @param fromIndex * @param fromIndex - low endpoint (inclusive) of the subList
* - low endpoint (inclusive) of the subList * @param toIndex - high endpoint (exclusive) of the subList
* @param toIndex
* - high endpoint (exclusive) of the subList
* @return a view of the specified range within this list * @return a view of the specified range within this list
* @throws IndexOutOfBoundsException * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex >
* for an illegal endpoint index value (fromIndex < 0 || toIndex >
* size || fromIndex > toIndex) * size || fromIndex > toIndex)
* @see java.util.List * @see java.util.List
*/ */
+7 -15
View File
@@ -9,7 +9,6 @@ import java.util.Vector;
* useful functions. * useful functions.
* *
* @author Klemek * @author Klemek
*
* @see java.util.Vector * @see java.util.Vector
*/ */
public class BetterVector<T> extends Vector<T> implements BetterList<T> { public class BetterVector<T> extends Vector<T> implements BetterList<T> {
@@ -20,8 +19,7 @@ public class BetterVector<T> extends Vector<T> implements BetterList<T> {
* Constructs a vector containing the elements of the specified collection, in * Constructs a vector containing the elements of the specified collection, in
* the order they are returned by the collection's iterator. * the order they are returned by the collection's iterator.
* *
* @param c * @param c - the collection whose elements are to be placed into this vector
* - the collection whose elements are to be placed into this vector
*/ */
public static <T> BetterVector<T> fromList(Collection<T> c) { public static <T> BetterVector<T> fromList(Collection<T> c) {
return new BetterVector<>(c); return new BetterVector<>(c);
@@ -48,8 +46,7 @@ public class BetterVector<T> extends Vector<T> implements BetterList<T> {
* Constructs a vector containing the elements of the specified collection, in * Constructs a vector containing the elements of the specified collection, in
* the order they are returned by the collection's iterator. * the order they are returned by the collection's iterator.
* *
* @param c * @param c - the collection whose elements are to be placed into this vector
* - the collection whose elements are to be placed into this vector
*/ */
public BetterVector(Collection<? extends T> c) { public BetterVector(Collection<? extends T> c) {
super(c); super(c);
@@ -78,10 +75,8 @@ public class BetterVector<T> extends Vector<T> implements BetterList<T> {
* Constructs an empty vector with the specified initial capacity and capacity * Constructs an empty vector with the specified initial capacity and capacity
* increment. * increment.
* *
* @param initialCapacity * @param initialCapacity - the initial capacity of the vector
* - the initial capacity of the vector * @param capacityIncrement - the amount by which the capacity is increased when the vector
* @param capacityIncrement
* - the amount by which the capacity is increased when the vector
* overflows * overflows
*/ */
public BetterVector(int initialCapacity, int capacityIncrement) { public BetterVector(int initialCapacity, int capacityIncrement) {
@@ -99,13 +94,10 @@ public class BetterVector<T> extends Vector<T> implements BetterList<T> {
* expects a list can be used as a range operation by passing a subList view * expects a list can be used as a range operation by passing a subList view
* instead of a whole list. (see List.subList) * instead of a whole list. (see List.subList)
* *
* @param fromIndex * @param fromIndex - low endpoint (inclusive) of the subList
* - low endpoint (inclusive) of the subList * @param toIndex - high endpoint (exclusive) of the subList
* @param toIndex
* - high endpoint (exclusive) of the subList
* @return a view of the specified range within this list * @return a view of the specified range within this list
* @throws IndexOutOfBoundsException * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex >
* for an illegal endpoint index value (fromIndex < 0 || toIndex >
* size || fromIndex > toIndex) * size || fromIndex > toIndex)
* @see java.util.List * @see java.util.List
*/ */
@@ -1,12 +1,12 @@
package fr.klemek.betterlists.test; package fr.klemek.betterlists.test;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import fr.klemek.betterlists.BetterArrayList; import fr.klemek.betterlists.BetterArrayList;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class BetterListsTests { public class BetterListsTests {
protected class Dummy { protected class Dummy {
@@ -56,9 +56,9 @@ public class BetterListsTests {
@Test @Test
public void testAll() { public void testAll() {
ArrayList<Dummy> al = new ArrayList<>(); ArrayList<Dummy> al = new ArrayList<>();
al.add(new Dummy(1d,"hello")); al.add(new Dummy(1d, "hello"));
al.add(new Dummy(2d,"test")); al.add(new Dummy(2d, "test"));
al.add(new Dummy(2d,"hello")); al.add(new Dummy(2d, "hello"));
BetterArrayList<Dummy> bal = BetterArrayList.fromList(al); BetterArrayList<Dummy> bal = BetterArrayList.fromList(al);
@@ -69,9 +69,9 @@ public class BetterListsTests {
@Test @Test
public void testAny() { public void testAny() {
BetterArrayList<Dummy> bal = new BetterArrayList<>(); BetterArrayList<Dummy> bal = new BetterArrayList<>();
bal.add(new Dummy(1d,"hello")); bal.add(new Dummy(1d, "hello"));
bal.add(new Dummy(2d,"test")); bal.add(new Dummy(2d, "test"));
bal.add(new Dummy(2d,"hello")); bal.add(new Dummy(2d, "hello"));
Assert.assertTrue(bal.any(du -> du.s.startsWith("t"))); Assert.assertTrue(bal.any(du -> du.s.startsWith("t")));
Assert.assertFalse(bal.any(du -> du.s.startsWith("b"))); Assert.assertFalse(bal.any(du -> du.s.startsWith("b")));
@@ -80,9 +80,9 @@ public class BetterListsTests {
@Test @Test
public void testCount() { public void testCount() {
BetterArrayList<Dummy> bal = new BetterArrayList<>(); BetterArrayList<Dummy> bal = new BetterArrayList<>();
bal.add(new Dummy(1d,"hello")); bal.add(new Dummy(1d, "hello"));
bal.add(new Dummy(2d,"test")); bal.add(new Dummy(2d, "test"));
bal.add(new Dummy(2d,"hello")); bal.add(new Dummy(2d, "hello"));
Assert.assertEquals(3, bal.count()); Assert.assertEquals(3, bal.count());
Assert.assertEquals(2, bal.count(du -> du.s.length() > 4)); Assert.assertEquals(2, bal.count(du -> du.s.length() > 4));
@@ -91,15 +91,15 @@ public class BetterListsTests {
@Test @Test
public void testExclude() { public void testExclude() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>(); BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d,"hello")); bal1.add(new Dummy(1d, "hello"));
bal1.add(new Dummy(2d,"test")); bal1.add(new Dummy(2d, "test"));
bal1.add(new Dummy(3d,"hello")); bal1.add(new Dummy(3d, "hello"));
bal1.add(new Dummy(4d,"test")); bal1.add(new Dummy(4d, "test"));
BetterArrayList<Dummy> bal2 = new BetterArrayList<>(); BetterArrayList<Dummy> bal2 = new BetterArrayList<>();
bal2.add(new Dummy(2d,"test")); bal2.add(new Dummy(2d, "test"));
bal2.add(new Dummy(3d,"hello")); bal2.add(new Dummy(3d, "hello"));
bal2.add(new Dummy(5d,"test")); bal2.add(new Dummy(5d, "test"));
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.exclusion(bal2); BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.exclusion(bal2);
Assert.assertEquals(2, bal3.size()); Assert.assertEquals(2, bal3.size());
@@ -110,9 +110,9 @@ public class BetterListsTests {
@Test @Test
public void testFirst() { public void testFirst() {
BetterArrayList<Dummy> bal = new BetterArrayList<>(); BetterArrayList<Dummy> bal = new BetterArrayList<>();
bal.add(new Dummy(1d,"hello")); bal.add(new Dummy(1d, "hello"));
bal.add(new Dummy(2d,"test")); bal.add(new Dummy(2d, "test"));
bal.add(new Dummy(2d,"hello")); 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")));
@@ -122,17 +122,17 @@ public class BetterListsTests {
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
} }
Assert.assertEquals(bal.get(0), bal.firstOrDefault(du -> du.s.startsWith("h"), new Dummy(3d,"default"))); 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(new Dummy(3d, "default"), bal.firstOrDefault(du -> du.s.startsWith("d"), new Dummy(3d, "default")));
} }
@Test @Test
public void testLast() { public void testLast() {
BetterArrayList<Dummy> bal = new BetterArrayList<>(); BetterArrayList<Dummy> bal = new BetterArrayList<>();
bal.add(new Dummy(1d,"hello")); bal.add(new Dummy(1d, "hello"));
bal.add(new Dummy(2d,"test")); bal.add(new Dummy(2d, "test"));
bal.add(new Dummy(2d,"hello")); bal.add(new Dummy(2d, "hello"));
Assert.assertEquals(bal.get(2), bal.last(du -> du.s.startsWith("h"))); Assert.assertEquals(bal.get(2), bal.last(du -> du.s.startsWith("h")));
@@ -142,49 +142,49 @@ public class BetterListsTests {
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
} }
Assert.assertEquals(bal.get(2), bal.lastOrDefault(du -> du.s.startsWith("h"), new Dummy(3d,"default"))); 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"))); Assert.assertEquals(new Dummy(3d, "default"), bal.lastOrDefault(du -> du.s.startsWith("d"), new Dummy(3d, "default")));
} }
@Test @Test
public void testMax() { public void testMax() {
BetterArrayList<Dummy> bal = new BetterArrayList<>(); BetterArrayList<Dummy> bal = new BetterArrayList<>();
bal.add(new Dummy(1d,"hello")); bal.add(new Dummy(1d, "hello"));
bal.add(new Dummy(2d,"test")); bal.add(new Dummy(2d, "test"));
bal.add(new Dummy(3d,"hello2")); bal.add(new Dummy(3d, "hello2"));
Assert.assertEquals(6d, bal.max(du -> (double)du.s.length()), 0.001d); Assert.assertEquals(6d, bal.max(du -> (double) du.s.length()), 0.001d);
} }
@Test @Test
public void testMean() { public void testMean() {
BetterArrayList<Dummy> bal = new BetterArrayList<>(); BetterArrayList<Dummy> bal = new BetterArrayList<>();
bal.add(new Dummy(1d,"hello")); bal.add(new Dummy(1d, "hello"));
bal.add(new Dummy(2d,"test")); bal.add(new Dummy(2d, "test"));
bal.add(new Dummy(3d,"hello2")); bal.add(new Dummy(3d, "hello2"));
Assert.assertEquals(5d, bal.mean(du -> (double)du.s.length()), 0.001d); Assert.assertEquals(5d, bal.mean(du -> (double) du.s.length()), 0.001d);
} }
@Test @Test
public void testMin() { public void testMin() {
BetterArrayList<Dummy> bal = new BetterArrayList<>(); BetterArrayList<Dummy> bal = new BetterArrayList<>();
bal.add(new Dummy(1d,"hello")); bal.add(new Dummy(1d, "hello"));
bal.add(new Dummy(2d,"test")); bal.add(new Dummy(2d, "test"));
bal.add(new Dummy(3d,"hello2")); bal.add(new Dummy(3d, "hello2"));
Assert.assertEquals(4d, bal.min(du -> (double)du.s.length()), 0.001d); Assert.assertEquals(4d, bal.min(du -> (double) du.s.length()), 0.001d);
} }
@Test @Test
public void testOrderBy() { public void testOrderBy() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>(); BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d,"hello1")); bal1.add(new Dummy(1d, "hello1"));
bal1.add(new Dummy(2d,"hello2")); bal1.add(new Dummy(2d, "hello2"));
bal1.add(new Dummy(3d,"hello0")); bal1.add(new Dummy(3d, "hello0"));
bal1.add(new Dummy(4d,"test")); bal1.add(new Dummy(4d, "test"));
bal1.add(new Dummy(5d,"hello4")); bal1.add(new Dummy(5d, "hello4"));
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.orderBy(c -> c.s); BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.orderBy(c -> c.s);
Assert.assertNotEquals(bal1, bal2); Assert.assertNotEquals(bal1, bal2);
@@ -198,77 +198,91 @@ public class BetterListsTests {
@Test @Test
public void testOrderByDescending() { public void testOrderByDescending() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>(); BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d,"hello1")); bal1.add(new Dummy(1d, "hello1"));
bal1.add(new Dummy(2d,"hello2")); bal1.add(new Dummy(2d, "hello2"));
bal1.add(new Dummy(3d,"hello0")); bal1.add(new Dummy(3d, "hello0"));
bal1.add(new Dummy(4d,"test")); bal1.add(new Dummy(4d, "test"));
bal1.add(new Dummy(5d,"hello4")); bal1.add(new Dummy(5d, "hello4"));
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.orderByDescending(c -> c.d); BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.orderByDescending(c -> c.d);
Assert.assertNotEquals(bal1, bal2); Assert.assertNotEquals(bal1, bal2);
for(int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
Assert.assertEquals(bal1.get(4-i), bal2.get(i)); Assert.assertEquals(bal1.get(4 - i), bal2.get(i));
} }
@Test @Test
public void testReverse() { public void testReverse() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>(); BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d,"hello")); bal1.add(new Dummy(1d, "hello"));
bal1.add(new Dummy(2d,"hello")); bal1.add(new Dummy(2d, "hello"));
bal1.add(new Dummy(3d,"hello")); bal1.add(new Dummy(3d, "hello"));
bal1.add(new Dummy(4d,"test")); bal1.add(new Dummy(4d, "test"));
bal1.add(new Dummy(5d,"hello")); bal1.add(new Dummy(5d, "hello"));
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.reverse(); BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.reverse();
Assert.assertEquals(5, bal2.size()); Assert.assertEquals(5, bal2.size());
for(int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
Assert.assertEquals(bal1.get(i), bal2.get(4-i)); Assert.assertEquals(bal1.get(i), bal2.get(4 - i));
} }
@Test @Test
public void testSelect() { public void testSelect() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>(); BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d,"hello")); bal1.add(new Dummy(1d, "hello"));
bal1.add(new Dummy(2d,"hello")); bal1.add(new Dummy(2d, "hello"));
bal1.add(new Dummy(3d,"hello")); bal1.add(new Dummy(3d, "hello"));
bal1.add(new Dummy(4d,"test")); bal1.add(new Dummy(4d, "test"));
bal1.add(new Dummy(5d,"hello")); bal1.add(new Dummy(5d, "hello"));
BetterArrayList<Double> bal2 = (BetterArrayList<Double>) bal1.select(du -> du.d); BetterArrayList<Double> bal2 = (BetterArrayList<Double>) bal1.select(du -> du.d);
Assert.assertEquals(5, bal2.size()); Assert.assertEquals(5, bal2.size());
for(int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
Assert.assertEquals(bal1.get(i).d, bal2.get(i), 0.0001); Assert.assertEquals(bal1.get(i).d, bal2.get(i), 0.0001);
} }
@Test
public void testSelectMany() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d, "hel;lo"));
bal1.add(new Dummy(2d, "hel;lo"));
BetterArrayList<String> bal2 = (BetterArrayList<String>) 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 @Test
public void testSkip() { public void testSkip() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>(); BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d,"hello")); bal1.add(new Dummy(1d, "hello"));
bal1.add(new Dummy(2d,"hello")); bal1.add(new Dummy(2d, "hello"));
bal1.add(new Dummy(3d,"hello")); bal1.add(new Dummy(3d, "hello"));
bal1.add(new Dummy(4d,"test")); bal1.add(new Dummy(4d, "test"));
bal1.add(new Dummy(5d,"hello")); bal1.add(new Dummy(5d, "hello"));
Assert.assertEquals(bal1, bal1.skip(0)); Assert.assertEquals(bal1, bal1.skip(0));
Assert.assertEquals(0, bal1.skip(10).size()); Assert.assertEquals(0, bal1.skip(10).size());
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.skip(2); BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.skip(2);
Assert.assertEquals(3, bal2.size()); Assert.assertEquals(3, bal2.size());
for(int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
Assert.assertEquals(bal1.get(i+2), bal2.get(i)); Assert.assertEquals(bal1.get(i + 2), bal2.get(i));
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.skipWhile(du -> du.s.startsWith("h")); BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.skipWhile(du -> du.s.startsWith("h"));
Assert.assertEquals(2, bal3.size()); Assert.assertEquals(2, bal3.size());
for(int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
Assert.assertEquals(bal1.get(i+3), bal3.get(i)); Assert.assertEquals(bal1.get(i + 3), bal3.get(i));
} }
@Test @Test
public void testSum() { public void testSum() {
BetterArrayList<Dummy> bal = new BetterArrayList<>(); BetterArrayList<Dummy> bal = new BetterArrayList<>();
bal.add(new Dummy(1d,"hello")); bal.add(new Dummy(1d, "hello"));
bal.add(new Dummy(2d,"test")); bal.add(new Dummy(2d, "test"));
bal.add(new Dummy(3d,"hello2")); bal.add(new Dummy(3d, "hello2"));
Assert.assertEquals(6d, bal.sum(du -> du.d), 0.001d); Assert.assertEquals(6d, bal.sum(du -> du.d), 0.001d);
} }
@@ -276,11 +290,11 @@ public class BetterListsTests {
@Test @Test
public void testTake() { public void testTake() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>(); BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d,"hello")); bal1.add(new Dummy(1d, "hello"));
bal1.add(new Dummy(2d,"hello")); bal1.add(new Dummy(2d, "hello"));
bal1.add(new Dummy(3d,"hello")); bal1.add(new Dummy(3d, "hello"));
bal1.add(new Dummy(4d,"test")); bal1.add(new Dummy(4d, "test"));
bal1.add(new Dummy(5d,"hello")); bal1.add(new Dummy(5d, "hello"));
Assert.assertEquals(bal1, bal1.take(10)); Assert.assertEquals(bal1, bal1.take(10));
@@ -288,27 +302,27 @@ public class BetterListsTests {
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.take(2); BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.take(2);
Assert.assertEquals(2, bal2.size()); Assert.assertEquals(2, bal2.size());
for(int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
Assert.assertEquals(bal1.get(i), bal2.get(i)); Assert.assertEquals(bal1.get(i), bal2.get(i));
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.takeWhile(du -> du.s.startsWith("h")); BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.takeWhile(du -> du.s.startsWith("h"));
Assert.assertEquals(3, bal3.size()); Assert.assertEquals(3, bal3.size());
for(int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
Assert.assertEquals(bal1.get(i), bal3.get(i)); Assert.assertEquals(bal1.get(i), bal3.get(i));
} }
@Test @Test
public void testUnion() { public void testUnion() {
BetterArrayList<Dummy> bal1 = new BetterArrayList<>(); BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
bal1.add(new Dummy(1d,"hello")); bal1.add(new Dummy(1d, "hello"));
bal1.add(new Dummy(2d,"test")); bal1.add(new Dummy(2d, "test"));
bal1.add(new Dummy(3d,"hello")); bal1.add(new Dummy(3d, "hello"));
bal1.add(new Dummy(4d,"test")); bal1.add(new Dummy(4d, "test"));
BetterArrayList<Dummy> bal2 = new BetterArrayList<>(); BetterArrayList<Dummy> bal2 = new BetterArrayList<>();
bal2.add(new Dummy(2d,"test")); bal2.add(new Dummy(2d, "test"));
bal2.add(new Dummy(3d,"hello")); bal2.add(new Dummy(3d, "hello"));
bal2.add(new Dummy(5d,"test")); bal2.add(new Dummy(5d, "test"));
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.union(bal2); BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.union(bal2);
Assert.assertEquals(2, bal3.size()); Assert.assertEquals(2, bal3.size());
@@ -319,14 +333,14 @@ public class BetterListsTests {
@Test @Test
public void testWhere() { public void testWhere() {
BetterArrayList<Dummy> bal = new BetterArrayList<>(); BetterArrayList<Dummy> bal = new BetterArrayList<>();
bal.add(new Dummy(1d,"hello")); bal.add(new Dummy(1d, "hello"));
bal.add(new Dummy(2d,"test")); bal.add(new Dummy(2d, "test"));
bal.add(new Dummy(3d,"hello")); bal.add(new Dummy(3d, "hello"));
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal.where(du -> du.s.startsWith("h")); BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal.where(du -> du.s.startsWith("h"));
Assert.assertEquals(2, bal2.size()); Assert.assertEquals(2, bal2.size());
Assert.assertEquals(new Dummy(1d,"hello"), bal2.get(0)); Assert.assertEquals(new Dummy(1d, "hello"), bal2.get(0));
Assert.assertEquals(new Dummy(3d,"hello"), bal2.get(1)); Assert.assertEquals(new Dummy(3d, "hello"), bal2.get(1));
} }
} }