BetterLists 1.2 : Code improvement
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
/bin/
|
/bin/
|
||||||
/.classpath
|
/.classpath
|
||||||
/.settings/
|
/.settings/
|
||||||
|
/target/
|
||||||
|
/maven-repo.bat
|
||||||
|
/.idea/
|
||||||
|
|||||||
@@ -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.1
|
Current version v1.2
|
||||||
|
|
||||||
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.1.jar](../../raw/master/download/betterlists-1.0.jar)
|
* [betterlists-1.2.jar](../../raw/master/download/betterlists-1.2.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.1</version>
|
<version>1.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
```
|
```
|
||||||
@@ -68,7 +68,7 @@ You can use this project as a maven dependency with this :
|
|||||||
| [max](#max) | Invokes a transform function on each element of the sequence and returns the maximum nullable Double value. |
|
| [max](#max) | Invokes a transform function on each element of the sequence and returns the maximum nullable Double value. |
|
||||||
| [mean](#mean) | Computes the mean of the sequence of Double values that are obtained by invoking a transform function on each element of the input sequence. |
|
| [mean](#mean) | Computes the mean of the sequence of Double values that are obtained by invoking a transform function on each element of the input sequence. |
|
||||||
| [min](#min) | Invokes a transform function on each element of the sequence and returns the minimum nullable Double value. |
|
| [min](#min) | Invokes a transform function on each element of the sequence and returns the minimum nullable Double value. |
|
||||||
| [orderBy / orderByDescending](#orderby-orderbydescending) | Sorts the elements of a sequence in ascending order by using a specified comparer. (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. |
|
||||||
| [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. |
|
||||||
@@ -111,19 +111,21 @@ BetterList<Contact> invalidFrenchContacts = frenchContacts.exclusion(validContac
|
|||||||
```
|
```
|
||||||
|
|
||||||
### first / firstOrDefault
|
### first / firstOrDefault
|
||||||
Returns the first element in the sequence that satisfies a specified condition. (Returns an error if no elements match the condition unless you use the `firstOrDefault` function)
|
Returns the first element in the sequence that satisfies a specified condition. (Throws an error if no elements match the condition unless you use the `firstOrDefault` function)
|
||||||
```Java
|
```Java
|
||||||
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
||||||
|
|
||||||
Contact firstManager = contacts.first(c -> c.isManager());
|
Contact firstManager = contacts.first(c -> c.isManager()); //can throw NoSuchElementException
|
||||||
|
Contact firstContact = contacts.firstOrDefault(null); //return null if the list is empty
|
||||||
```
|
```
|
||||||
|
|
||||||
### last / lastOrDefault
|
### last / lastOrDefault
|
||||||
Returns the last element in the sequence that satisfies a specified condition. (Returns an error if no elements match the condition unless you use the `lastOrDefault` function)
|
Returns the last element in the sequence that satisfies a specified condition. (Throws an error if no elements match the condition unless you use the `lastOrDefault` function)
|
||||||
```Java
|
```Java
|
||||||
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
||||||
|
|
||||||
Contact lastRegular = contacts.last(c -> !c.isManager());
|
Contact lastRegular = contacts.last(c -> !c.isManager()); //can throw NoSuchElementException
|
||||||
|
Contact lastManager = contacts.lastOrDefault(c -> c.isManager(), null); //return null there is no manager
|
||||||
```
|
```
|
||||||
|
|
||||||
### max
|
### max
|
||||||
@@ -156,6 +158,7 @@ Sorts the elements of a sequence in ascending order by using a specified compare
|
|||||||
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
||||||
|
|
||||||
BetterList<Contact> orderedContacts = contacts.orderBy(c -> c.getName);
|
BetterList<Contact> orderedContacts = contacts.orderBy(c -> c.getName);
|
||||||
|
BetterList<Contact> orderedContacts2 = contacts.orderByDescending(c -> c.getAge()); //oldest first
|
||||||
```
|
```
|
||||||
|
|
||||||
### reverse
|
### reverse
|
||||||
@@ -179,7 +182,8 @@ Bypasses elements in the sequence as long as a specified condition is true and t
|
|||||||
```Java
|
```Java
|
||||||
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
||||||
|
|
||||||
BetterList<Contact> contacts2 = contacts.skipWhile(c -> c.getEmail().startsWith("society"));
|
BetterList<Contact> contacts2 = contacts.skip(3);
|
||||||
|
BetterList<Contact> contacts3 = contacts.skipWhile(c -> c.getEmail().startsWith("society"));
|
||||||
```
|
```
|
||||||
|
|
||||||
### sum
|
### sum
|
||||||
@@ -195,7 +199,8 @@ Returns a specified number of contiguous elements from the start of the sequence
|
|||||||
```Java
|
```Java
|
||||||
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
BetterArrayList<Contact> contacts = BetterArrayList.fromList(someFunction());
|
||||||
|
|
||||||
BetterList<Contact> contacts2 = contacts.takeWhile(c -> c.getEmail().startsWith("society"));
|
BetterList<Contact> contacts2 = contacts.take(5);
|
||||||
|
BetterList<Contact> contacts3 = contacts.takeWhile(c -> c.getEmail().startsWith("society"));
|
||||||
```
|
```
|
||||||
|
|
||||||
### union
|
### union
|
||||||
|
|||||||
Binary file not shown.
@@ -1,6 +1,7 @@
|
|||||||
package fr.klemek.betterlists;
|
package fr.klemek.betterlists;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -26,6 +27,15 @@ public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
|
|||||||
return new BetterArrayList<>(c);
|
return new BetterArrayList<>(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a list containing the elements given in argument.
|
||||||
|
*
|
||||||
|
* @param a - the elements to be placed into this list
|
||||||
|
*/
|
||||||
|
public static <T> BetterArrayList<T> asList(T... a) {
|
||||||
|
return new BetterArrayList<>(a);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an empty list with an initial capacity of ten.
|
* Constructs an empty list with an initial capacity of ten.
|
||||||
*/
|
*/
|
||||||
@@ -45,14 +55,23 @@ public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an empty list with the specified initial capacity.
|
* Constructs a list containing the elements given in argument.
|
||||||
*
|
*
|
||||||
* @param initialCapacity
|
* @param a
|
||||||
* - the initial capacity of the list
|
* - the elements to be placed into this list
|
||||||
*/
|
*/
|
||||||
public BetterArrayList(int initialCapacity) {
|
public BetterArrayList(T... a) {
|
||||||
super(initialCapacity);
|
super(Arrays.asList(a));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an empty list with the specified initial capacity.
|
||||||
|
*
|
||||||
|
* @param initialCapacity - the initial capacity of the list
|
||||||
|
*/
|
||||||
|
public BetterArrayList(int initialCapacity) {
|
||||||
|
super(initialCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a view of the portion of this list between the specified fromIndex,
|
* Returns a view of the portion of this list between the specified fromIndex,
|
||||||
@@ -77,6 +96,6 @@ public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public BetterArrayList<T> subList(int fromIndex, int toIndex) {
|
public BetterArrayList<T> subList(int fromIndex, int toIndex) {
|
||||||
return (BetterArrayList<T>) this.subList(fromIndex, toIndex);
|
return (BetterArrayList<T>) super.subList(fromIndex, toIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package fr.klemek.betterlists;
|
package fr.klemek.betterlists;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
@@ -26,6 +27,15 @@ public class BetterLinkedList<T> extends LinkedList<T> implements BetterList<T>
|
|||||||
return new BetterLinkedList<>(c);
|
return new BetterLinkedList<>(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a list containing the elements given in argument.
|
||||||
|
*
|
||||||
|
* @param a - the elements to be placed into this list
|
||||||
|
*/
|
||||||
|
public static <T> BetterLinkedList<T> asList(T... a) {
|
||||||
|
return new BetterLinkedList<>(a);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an empty list.
|
* Constructs an empty list.
|
||||||
*/
|
*/
|
||||||
@@ -45,29 +55,36 @@ public class BetterLinkedList<T> extends LinkedList<T> implements BetterList<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a view of the portion of this list between the specified fromIndex,
|
* Constructs a list containing the elements given in argument.
|
||||||
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the
|
*
|
||||||
* returned list is empty.) The returned list is backed by this list, so
|
* @param a
|
||||||
* non-structural changes in the returned list are reflected in this list, and
|
* - the elements to be placed into this list
|
||||||
* vice-versa. The returned list supports all of the optional list operations
|
*/
|
||||||
* supported by this list. This method eliminates the need for explicit range
|
public BetterLinkedList(T... a) {
|
||||||
* operations (of the sort that commonly exist for arrays). Any operation that
|
super(Arrays.asList(a));
|
||||||
* 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
|
* Returns a view of the portion of this list between the specified fromIndex,
|
||||||
* - low endpoint (inclusive) of the subList
|
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the
|
||||||
* @param toIndex
|
* returned list is empty.) The returned list is backed by this list, so
|
||||||
* - high endpoint (exclusive) of the subList
|
* non-structural changes in the returned list are reflected in this list, and
|
||||||
* @return a view of the specified range within this list
|
* vice-versa. The returned list supports all of the optional list operations
|
||||||
* @throws IndexOutOfBoundsException
|
* supported by this list. This method eliminates the need for explicit range
|
||||||
* for an illegal endpoint index value (fromIndex < 0 || toIndex >
|
* operations (of the sort that commonly exist for arrays). Any operation that
|
||||||
* size || fromIndex > toIndex)
|
* expects a list can be used as a range operation by passing a subList view
|
||||||
* @see java.util.List
|
* instead of a whole list. (see List.subList)
|
||||||
*/
|
*
|
||||||
@Override
|
* @param fromIndex - low endpoint (inclusive) of the subList
|
||||||
public BetterLinkedList<T> subList(int fromIndex, int toIndex) {
|
* @param toIndex - high endpoint (exclusive) of the subList
|
||||||
return (BetterLinkedList<T>) this.subList(fromIndex, toIndex);
|
* @return a view of the specified range within this list
|
||||||
}
|
* @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex >
|
||||||
|
* size || fromIndex > toIndex)
|
||||||
|
* @see java.util.List
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public BetterLinkedList<T> subList(int fromIndex, int toIndex) {
|
||||||
|
return (BetterLinkedList<T>) super.subList(fromIndex, toIndex);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default boolean any(Function<T, Boolean> predicate) {
|
default boolean any(Function<T, Boolean> predicate) {
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (predicate.apply(element))
|
if (predicate.apply(element))
|
||||||
return true;
|
return true;
|
||||||
@@ -50,7 +50,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
*
|
*
|
||||||
* @return The number of elements in the input sequence.
|
* @return The number of elements in the input sequence.
|
||||||
*/
|
*/
|
||||||
public default int count() {
|
default int count() {
|
||||||
return count(e -> true);
|
return count(e -> true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default int count(Function<T, Boolean> predicate) {
|
default int count(Function<T, Boolean> predicate) {
|
||||||
int out = 0;
|
int out = 0;
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (predicate.apply(element))
|
if (predicate.apply(element))
|
||||||
@@ -80,7 +80,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default BetterList<T> exclusion(List<T> other) {
|
default BetterList<T> exclusion(List<T> other) {
|
||||||
BetterList<T> out = new BetterArrayList<>();
|
BetterList<T> out = new BetterArrayList<>();
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (!other.contains(element))
|
if (!other.contains(element))
|
||||||
@@ -95,7 +95,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* If the sequence is empty.
|
* If the sequence is empty.
|
||||||
* @return The first element in the sequence.
|
* @return The first element in the sequence.
|
||||||
*/
|
*/
|
||||||
public default T first() {
|
default T first() {
|
||||||
return first(e -> true);
|
return first(e -> true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default T first(Function<T, Boolean> predicate) {
|
default T first(Function<T, Boolean> predicate) {
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (predicate.apply(element))
|
if (predicate.apply(element))
|
||||||
return element;
|
return element;
|
||||||
@@ -130,7 +130,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* 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.
|
||||||
*/
|
*/
|
||||||
public default T firstOrDefault(Function<T, Boolean> predicate, T defaultValue) {
|
default T firstOrDefault(Function<T, Boolean> predicate, T defaultValue) {
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (predicate.apply(element))
|
if (predicate.apply(element))
|
||||||
return element;
|
return element;
|
||||||
@@ -146,7 +146,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default T firstOrDefault(T defaultValue) {
|
default T firstOrDefault(T defaultValue) {
|
||||||
return firstOrDefault(e -> true, defaultValue);
|
return firstOrDefault(e -> true, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +157,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* If the sequence is empty.
|
* If the sequence is empty.
|
||||||
* @return the last element of the sequence.
|
* @return the last element of the sequence.
|
||||||
*/
|
*/
|
||||||
public default T last() {
|
default T last() {
|
||||||
return last(e -> true);
|
return last(e -> true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +173,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @return the last element of the sequence that satisfies a specified
|
* @return the last element of the sequence that satisfies a specified
|
||||||
* condition.
|
* condition.
|
||||||
*/
|
*/
|
||||||
public default T last(Function<T, Boolean> predicate) {
|
default T last(Function<T, Boolean> predicate) {
|
||||||
T value = null;
|
T value = null;
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (predicate.apply(element))
|
if (predicate.apply(element))
|
||||||
@@ -195,7 +195,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* 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.
|
||||||
*/
|
*/
|
||||||
public default T lastOrDefault(Function<T, Boolean> predicate, T defaultValue) {
|
default T lastOrDefault(Function<T, Boolean> predicate, T defaultValue) {
|
||||||
T value = null;
|
T value = null;
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (predicate.apply(element))
|
if (predicate.apply(element))
|
||||||
@@ -212,7 +212,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default T lastOrDefault(T defaultValue) {
|
default T lastOrDefault(T defaultValue) {
|
||||||
return lastOrDefault(e -> true, defaultValue);
|
return lastOrDefault(e -> true, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,7 +225,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default Double max(Function<T, Double> selector) {
|
default Double max(Function<T, Double> selector) {
|
||||||
Double max = null;
|
Double max = null;
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (max == null || selector.apply(element) > max)
|
if (max == null || selector.apply(element) > max)
|
||||||
@@ -242,7 +242,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default Double mean(Function<T, Double> selector) {
|
default Double mean(Function<T, Double> selector) {
|
||||||
int count = this.count();
|
int count = this.count();
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
return null;
|
return null;
|
||||||
@@ -258,7 +258,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default Double min(Function<T, Double> selector) {
|
default Double min(Function<T, Double> selector) {
|
||||||
Double min = null;
|
Double min = null;
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (min == null || selector.apply(element) < min)
|
if (min == null || selector.apply(element) < min)
|
||||||
@@ -272,7 +272,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* - A transform function to apply to each element.
|
* - 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.
|
||||||
*/
|
*/
|
||||||
public 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)));
|
||||||
@@ -285,7 +285,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* - A transform function to apply to each element.
|
* - 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.
|
||||||
*/
|
*/
|
||||||
public 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)));
|
||||||
@@ -298,7 +298,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @return A sequence whose elements correspond to those of the sequence in
|
* @return A sequence whose elements correspond to those of the sequence in
|
||||||
* reverse order.
|
* reverse order.
|
||||||
*/
|
*/
|
||||||
public default BetterList<T> reverse() {
|
default BetterList<T> reverse() {
|
||||||
BetterList<T> out = new BetterArrayList<>(this.size());
|
BetterList<T> out = new BetterArrayList<>(this.size());
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
out.add(0, element);
|
out.add(0, element);
|
||||||
@@ -315,7 +315,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default <E> BetterList<E> select(Function<T, E> selector) {
|
default <E> BetterList<E> select(Function<T, E> selector) {
|
||||||
BetterList<E> out = new BetterArrayList<>();
|
BetterList<E> out = new BetterArrayList<>();
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
out.add(selector.apply(element));
|
out.add(selector.apply(element));
|
||||||
@@ -332,7 +332,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default BetterList<T> skip(int count) {
|
default BetterList<T> skip(int count) {
|
||||||
BetterList<T> out = new BetterArrayList<>();
|
BetterList<T> out = new BetterArrayList<>();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (T element : this) {
|
for (T element : this) {
|
||||||
@@ -353,7 +353,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* 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.
|
||||||
*/
|
*/
|
||||||
public default BetterList<T> skipWhile(Function<T, Boolean> predicate) {
|
default BetterList<T> skipWhile(Function<T, Boolean> predicate) {
|
||||||
BetterList<T> out = new BetterArrayList<>();
|
BetterList<T> out = new BetterArrayList<>();
|
||||||
boolean match = true;
|
boolean match = true;
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
@@ -373,7 +373,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default Double sum(Function<T, Double> selector) {
|
default Double sum(Function<T, Double> selector) {
|
||||||
double sum = 0d;
|
double sum = 0d;
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
sum += selector.apply(element);
|
sum += selector.apply(element);
|
||||||
@@ -389,7 +389,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default BetterList<T> take(int count) {
|
default BetterList<T> take(int count) {
|
||||||
BetterList<T> out = new BetterArrayList<>(count);
|
BetterList<T> out = new BetterArrayList<>(count);
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (T element : this) {
|
for (T element : this) {
|
||||||
@@ -410,7 +410,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default BetterList<T> takeWhile(Function<T, Boolean> predicate) {
|
default BetterList<T> takeWhile(Function<T, Boolean> predicate) {
|
||||||
BetterList<T> out = new BetterArrayList<>();
|
BetterList<T> out = new BetterArrayList<>();
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (predicate.apply(element))
|
if (predicate.apply(element))
|
||||||
@@ -429,7 +429,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @return A List that contains the elements from both sequences, excluding
|
* @return A List that contains the elements from both sequences, excluding
|
||||||
* duplicates.
|
* duplicates.
|
||||||
*/
|
*/
|
||||||
public default BetterList<T> union(List<T> other) {
|
default BetterList<T> union(List<T> other) {
|
||||||
BetterList<T> out = new BetterArrayList<>();
|
BetterList<T> out = new BetterArrayList<>();
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (other.contains(element))
|
if (other.contains(element))
|
||||||
@@ -445,7 +445,7 @@ public interface BetterList<T> extends List<T> {
|
|||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public default BetterList<T> where(Function<T, Boolean> predicate) {
|
default BetterList<T> where(Function<T, Boolean> predicate) {
|
||||||
BetterList<T> out = new BetterArrayList<>();
|
BetterList<T> out = new BetterArrayList<>();
|
||||||
for (T element : this)
|
for (T element : this)
|
||||||
if (predicate.apply(element))
|
if (predicate.apply(element))
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public class BetterStack<T> extends Stack<T> implements BetterList<T> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public BetterStack<T> subList(int fromIndex, int toIndex) {
|
public BetterStack<T> subList(int fromIndex, int toIndex) {
|
||||||
return (BetterStack<T>) this.subList(fromIndex, toIndex);
|
return (BetterStack<T>) super.subList(fromIndex, toIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package fr.klemek.betterlists;
|
package fr.klemek.betterlists;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
@@ -25,7 +26,16 @@ public class BetterVector<T> extends Vector<T> implements BetterList<T> {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a vector containing the elements given in argument.
|
||||||
|
*
|
||||||
|
* @param a - the elements to be placed into this vector
|
||||||
|
*/
|
||||||
|
public static <T> BetterVector<T> asVector(T... a) {
|
||||||
|
return new BetterVector<>(a);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an empty vector so that its internal data array has size 10 and
|
* Constructs an empty vector so that its internal data array has size 10 and
|
||||||
* its standard capacity increment is zero.
|
* its standard capacity increment is zero.
|
||||||
@@ -45,12 +55,20 @@ public class BetterVector<T> extends Vector<T> implements BetterList<T> {
|
|||||||
super(c);
|
super(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a vector containing the elements given in argument.
|
||||||
|
*
|
||||||
|
* @param a - the elements to be placed into this list
|
||||||
|
*/
|
||||||
|
public BetterVector(T... a) {
|
||||||
|
super(Arrays.asList(a));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an empty vector with the specified initial capacity and with its
|
* Constructs an empty vector with the specified initial capacity and with its
|
||||||
* capacity increment equal to zero.
|
* capacity increment equal to zero.
|
||||||
*
|
*
|
||||||
* @param initialCapacity
|
* @param initialCapacity - the initial capacity of the vector
|
||||||
* - the initial capacity of the vector
|
|
||||||
*/
|
*/
|
||||||
public BetterVector(int initialCapacity) {
|
public BetterVector(int initialCapacity) {
|
||||||
super(initialCapacity);
|
super(initialCapacity);
|
||||||
@@ -93,7 +111,7 @@ public class BetterVector<T> extends Vector<T> implements BetterList<T> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public BetterVector<T> subList(int fromIndex, int toIndex) {
|
public BetterVector<T> subList(int fromIndex, int toIndex) {
|
||||||
return (BetterVector<T>) this.subList(fromIndex, toIndex);
|
return (BetterVector<T>) super.subList(fromIndex, toIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+332
-334
@@ -1,334 +1,332 @@
|
|||||||
package fr.klemek.betterlists;
|
package fr.klemek.betterlists.test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import fr.klemek.betterlists.BetterArrayList;
|
||||||
import org.junit.Test;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
public class BetterListsTests {
|
|
||||||
|
public class BetterListsTests {
|
||||||
protected class Dummy {
|
|
||||||
double d;
|
protected class Dummy {
|
||||||
String s;
|
final double d;
|
||||||
|
final String s;
|
||||||
public Dummy(double d, String s) {
|
|
||||||
this.d = d;
|
public Dummy(double d, String s) {
|
||||||
this.s = s;
|
this.d = d;
|
||||||
}
|
this.s = s;
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
@Override
|
||||||
if (this == obj)
|
public boolean equals(Object obj) {
|
||||||
return true;
|
if (this == obj)
|
||||||
if (obj == null)
|
return true;
|
||||||
return false;
|
if (obj == null)
|
||||||
if (getClass() != obj.getClass())
|
return false;
|
||||||
return false;
|
if (getClass() != obj.getClass())
|
||||||
Dummy other = (Dummy) obj;
|
return false;
|
||||||
if (!getOuterType().equals(other.getOuterType()))
|
Dummy other = (Dummy) obj;
|
||||||
return false;
|
if (!getOuterType().equals(other.getOuterType()))
|
||||||
if (Double.doubleToLongBits(d) != Double.doubleToLongBits(other.d))
|
return false;
|
||||||
return false;
|
if (Double.doubleToLongBits(d) != Double.doubleToLongBits(other.d))
|
||||||
if (s == null) {
|
return false;
|
||||||
if (other.s != null)
|
if (s == null && other.s != null) {
|
||||||
return false;
|
return false;
|
||||||
} else if (!s.equals(other.s))
|
} else return s.equals(other.s);
|
||||||
return false;
|
}
|
||||||
return true;
|
|
||||||
}
|
private BetterListsTests getOuterType() {
|
||||||
|
return BetterListsTests.this;
|
||||||
private BetterListsTests getOuterType() {
|
}
|
||||||
return BetterListsTests.this;
|
|
||||||
}
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
@Override
|
final int prime = 31;
|
||||||
public int hashCode() {
|
int result = 1;
|
||||||
final int prime = 31;
|
result = prime * result + getOuterType().hashCode();
|
||||||
int result = 1;
|
long temp;
|
||||||
result = prime * result + getOuterType().hashCode();
|
temp = Double.doubleToLongBits(d);
|
||||||
long temp;
|
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||||
temp = Double.doubleToLongBits(d);
|
result = prime * result + ((s == null) ? 0 : s.hashCode());
|
||||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
return result;
|
||||||
result = prime * result + ((s == null) ? 0 : s.hashCode());
|
}
|
||||||
return result;
|
}
|
||||||
}
|
|
||||||
}
|
@Test
|
||||||
|
public void testAll() {
|
||||||
@Test
|
ArrayList<Dummy> al = new ArrayList<>();
|
||||||
public void testAll() {
|
al.add(new Dummy(1d,"hello"));
|
||||||
ArrayList<Dummy> al = new ArrayList<Dummy>();
|
al.add(new Dummy(2d,"test"));
|
||||||
al.add(new Dummy(1d,"hello"));
|
al.add(new Dummy(2d,"hello"));
|
||||||
al.add(new Dummy(2d,"test"));
|
|
||||||
al.add(new Dummy(2d,"hello"));
|
BetterArrayList<Dummy> bal = BetterArrayList.fromList(al);
|
||||||
|
|
||||||
BetterArrayList<Dummy> bal = BetterArrayList.fromList(al);
|
Assert.assertTrue(bal.all(du -> du.d > 0));
|
||||||
|
Assert.assertFalse(bal.all(du -> du.d > 1));
|
||||||
Assert.assertTrue(bal.all(du -> du.d > 0));
|
}
|
||||||
Assert.assertFalse(bal.all(du -> du.d > 1));
|
|
||||||
}
|
@Test
|
||||||
|
public void testAny() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal = new BetterArrayList<>();
|
||||||
public void testAny() {
|
bal.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal = new BetterArrayList<Dummy>();
|
bal.add(new Dummy(2d,"test"));
|
||||||
bal.add(new Dummy(1d,"hello"));
|
bal.add(new Dummy(2d,"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")));
|
||||||
Assert.assertTrue(bal.any(du -> du.s.startsWith("t")));
|
}
|
||||||
Assert.assertFalse(bal.any(du -> du.s.startsWith("b")));
|
|
||||||
}
|
@Test
|
||||||
|
public void testCount() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal = new BetterArrayList<>();
|
||||||
public void testCount() {
|
bal.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal = new BetterArrayList<Dummy>();
|
bal.add(new Dummy(2d,"test"));
|
||||||
bal.add(new Dummy(1d,"hello"));
|
bal.add(new Dummy(2d,"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));
|
||||||
Assert.assertEquals(3, bal.count());
|
}
|
||||||
Assert.assertEquals(2, bal.count(du -> du.s.length() > 4));
|
|
||||||
}
|
@Test
|
||||||
|
public void testExclude() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
|
||||||
public void testExclude() {
|
bal1.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal1 = new BetterArrayList<Dummy>();
|
bal1.add(new Dummy(2d,"test"));
|
||||||
bal1.add(new Dummy(1d,"hello"));
|
bal1.add(new Dummy(3d,"hello"));
|
||||||
bal1.add(new Dummy(2d,"test"));
|
bal1.add(new Dummy(4d,"test"));
|
||||||
bal1.add(new Dummy(3d,"hello"));
|
|
||||||
bal1.add(new Dummy(4d,"test"));
|
BetterArrayList<Dummy> bal2 = new BetterArrayList<>();
|
||||||
|
bal2.add(new Dummy(2d,"test"));
|
||||||
BetterArrayList<Dummy> bal2 = new BetterArrayList<Dummy>();
|
bal2.add(new Dummy(3d,"hello"));
|
||||||
bal2.add(new Dummy(2d,"test"));
|
bal2.add(new Dummy(5d,"test"));
|
||||||
bal2.add(new Dummy(3d,"hello"));
|
|
||||||
bal2.add(new Dummy(5d,"test"));
|
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.exclusion(bal2);
|
||||||
|
Assert.assertEquals(2, bal3.size());
|
||||||
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.exclusion(bal2);
|
Assert.assertEquals(bal1.get(0), bal3.get(0));
|
||||||
Assert.assertEquals(2, bal3.size());
|
Assert.assertEquals(bal1.get(3), bal3.get(1));
|
||||||
Assert.assertEquals(bal1.get(0), bal3.get(0));
|
}
|
||||||
Assert.assertEquals(bal1.get(3), bal3.get(1));
|
|
||||||
}
|
@Test
|
||||||
|
public void testFirst() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal = new BetterArrayList<>();
|
||||||
public void testFirst() {
|
bal.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal = new BetterArrayList<Dummy>();
|
bal.add(new Dummy(2d,"test"));
|
||||||
bal.add(new Dummy(1d,"hello"));
|
bal.add(new Dummy(2d,"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"));
|
||||||
try {
|
Assert.fail("no error");
|
||||||
bal.first(du -> du.s.startsWith("d"));
|
} catch (NoSuchElementException e) {
|
||||||
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(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
|
||||||
|
public void testLast() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal = new BetterArrayList<>();
|
||||||
public void testLast() {
|
bal.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal = new BetterArrayList<Dummy>();
|
bal.add(new Dummy(2d,"test"));
|
||||||
bal.add(new Dummy(1d,"hello"));
|
bal.add(new Dummy(2d,"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")));
|
||||||
|
|
||||||
Assert.assertEquals(bal.get(2), bal.last(du -> du.s.startsWith("h")));
|
try {
|
||||||
|
bal.last(du -> du.s.startsWith("d"));
|
||||||
try {
|
Assert.fail("no error");
|
||||||
bal.last(du -> du.s.startsWith("d"));
|
} catch (NoSuchElementException e) {
|
||||||
Assert.fail("no error");
|
}
|
||||||
} 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
|
||||||
|
public void testMax() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal = new BetterArrayList<>();
|
||||||
public void testMax() {
|
bal.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal = new BetterArrayList<Dummy>();
|
bal.add(new Dummy(2d,"test"));
|
||||||
bal.add(new Dummy(1d,"hello"));
|
bal.add(new Dummy(3d,"hello2"));
|
||||||
bal.add(new Dummy(2d,"test"));
|
|
||||||
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
|
||||||
|
public void testMean() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal = new BetterArrayList<>();
|
||||||
public void testMean() {
|
bal.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal = new BetterArrayList<Dummy>();
|
bal.add(new Dummy(2d,"test"));
|
||||||
bal.add(new Dummy(1d,"hello"));
|
bal.add(new Dummy(3d,"hello2"));
|
||||||
bal.add(new Dummy(2d,"test"));
|
|
||||||
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
|
||||||
|
public void testMin() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal = new BetterArrayList<>();
|
||||||
public void testMin() {
|
bal.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal = new BetterArrayList<Dummy>();
|
bal.add(new Dummy(2d,"test"));
|
||||||
bal.add(new Dummy(1d,"hello"));
|
bal.add(new Dummy(3d,"hello2"));
|
||||||
bal.add(new Dummy(2d,"test"));
|
|
||||||
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
|
||||||
|
public void testOrderBy() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
|
||||||
public void testOrderBy() {
|
bal1.add(new Dummy(1d,"hello1"));
|
||||||
BetterArrayList<Dummy> bal1 = new BetterArrayList<Dummy>();
|
bal1.add(new Dummy(2d,"hello2"));
|
||||||
bal1.add(new Dummy(1d,"hello1"));
|
bal1.add(new Dummy(3d,"hello0"));
|
||||||
bal1.add(new Dummy(2d,"hello2"));
|
bal1.add(new Dummy(4d,"test"));
|
||||||
bal1.add(new Dummy(3d,"hello0"));
|
bal1.add(new Dummy(5d,"hello4"));
|
||||||
bal1.add(new Dummy(4d,"test"));
|
|
||||||
bal1.add(new Dummy(5d,"hello4"));
|
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.orderBy(c -> c.s);
|
||||||
|
Assert.assertNotEquals(bal1, bal2);
|
||||||
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.orderBy(c -> c.s);
|
Assert.assertEquals(bal1.get(2), bal2.get(0));
|
||||||
Assert.assertNotEquals(bal1, bal2);
|
Assert.assertEquals(bal1.get(0), bal2.get(1));
|
||||||
Assert.assertEquals(bal1.get(2), bal2.get(0));
|
Assert.assertEquals(bal1.get(1), bal2.get(2));
|
||||||
Assert.assertEquals(bal1.get(0), bal2.get(1));
|
Assert.assertEquals(bal1.get(4), bal2.get(3));
|
||||||
Assert.assertEquals(bal1.get(1), bal2.get(2));
|
Assert.assertEquals(bal1.get(3), bal2.get(4));
|
||||||
Assert.assertEquals(bal1.get(4), bal2.get(3));
|
}
|
||||||
Assert.assertEquals(bal1.get(3), bal2.get(4));
|
|
||||||
}
|
@Test
|
||||||
|
public void testOrderByDescending() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
|
||||||
public void testOrderByDescending() {
|
bal1.add(new Dummy(1d,"hello1"));
|
||||||
BetterArrayList<Dummy> bal1 = new BetterArrayList<Dummy>();
|
bal1.add(new Dummy(2d,"hello2"));
|
||||||
bal1.add(new Dummy(1d,"hello1"));
|
bal1.add(new Dummy(3d,"hello0"));
|
||||||
bal1.add(new Dummy(2d,"hello2"));
|
bal1.add(new Dummy(4d,"test"));
|
||||||
bal1.add(new Dummy(3d,"hello0"));
|
bal1.add(new Dummy(5d,"hello4"));
|
||||||
bal1.add(new Dummy(4d,"test"));
|
|
||||||
bal1.add(new Dummy(5d,"hello4"));
|
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.orderByDescending(c -> c.d);
|
||||||
|
Assert.assertNotEquals(bal1, bal2);
|
||||||
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.orderByDescending(c -> c.d);
|
for(int i = 0; i < 5; i++)
|
||||||
Assert.assertNotEquals(bal1, bal2);
|
Assert.assertEquals(bal1.get(4-i), bal2.get(i));
|
||||||
for(int i = 0; i < 5; i++)
|
}
|
||||||
Assert.assertEquals(bal1.get(4-i), bal2.get(i));
|
|
||||||
}
|
@Test
|
||||||
|
public void testReverse() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
|
||||||
public void testReverse() {
|
bal1.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal1 = new BetterArrayList<Dummy>();
|
bal1.add(new Dummy(2d,"hello"));
|
||||||
bal1.add(new Dummy(1d,"hello"));
|
bal1.add(new Dummy(3d,"hello"));
|
||||||
bal1.add(new Dummy(2d,"hello"));
|
bal1.add(new Dummy(4d,"test"));
|
||||||
bal1.add(new Dummy(3d,"hello"));
|
bal1.add(new Dummy(5d,"hello"));
|
||||||
bal1.add(new Dummy(4d,"test"));
|
|
||||||
bal1.add(new Dummy(5d,"hello"));
|
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.reverse();
|
||||||
|
Assert.assertEquals(5, bal2.size());
|
||||||
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.reverse();
|
for(int i = 0; i < 5; i++)
|
||||||
Assert.assertEquals(5, bal2.size());
|
Assert.assertEquals(bal1.get(i), bal2.get(4-i));
|
||||||
for(int i = 0; i < 5; i++)
|
}
|
||||||
Assert.assertEquals(bal1.get(i), bal2.get(4-i));
|
|
||||||
}
|
@Test
|
||||||
|
public void testSelect() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
|
||||||
public void testSelect() {
|
bal1.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal1 = new BetterArrayList<Dummy>();
|
bal1.add(new Dummy(2d,"hello"));
|
||||||
bal1.add(new Dummy(1d,"hello"));
|
bal1.add(new Dummy(3d,"hello"));
|
||||||
bal1.add(new Dummy(2d,"hello"));
|
bal1.add(new Dummy(4d,"test"));
|
||||||
bal1.add(new Dummy(3d,"hello"));
|
bal1.add(new Dummy(5d,"hello"));
|
||||||
bal1.add(new Dummy(4d,"test"));
|
|
||||||
bal1.add(new Dummy(5d,"hello"));
|
BetterArrayList<Double> bal2 = (BetterArrayList<Double>) bal1.select(du -> du.d);
|
||||||
|
Assert.assertEquals(5, bal2.size());
|
||||||
BetterArrayList<Double> bal2 = (BetterArrayList<Double>) bal1.<Double>select(du -> du.d);
|
for(int i = 0; i < 5; i++)
|
||||||
Assert.assertEquals(5, bal2.size());
|
Assert.assertEquals(bal1.get(i).d, bal2.get(i), 0.0001);
|
||||||
for(int i = 0; i < 5; i++)
|
}
|
||||||
Assert.assertEquals(bal1.get(i).d, bal2.get(i), 0.0001);
|
|
||||||
}
|
@Test
|
||||||
|
public void testSkip() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
|
||||||
public void testSkip() {
|
bal1.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal1 = new BetterArrayList<Dummy>();
|
bal1.add(new Dummy(2d,"hello"));
|
||||||
bal1.add(new Dummy(1d,"hello"));
|
bal1.add(new Dummy(3d,"hello"));
|
||||||
bal1.add(new Dummy(2d,"hello"));
|
bal1.add(new Dummy(4d,"test"));
|
||||||
bal1.add(new Dummy(3d,"hello"));
|
bal1.add(new Dummy(5d,"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());
|
||||||
Assert.assertEquals(bal1, bal1.skip(0));
|
|
||||||
Assert.assertEquals(0, bal1.skip(10).size());
|
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.skip(2);
|
||||||
|
Assert.assertEquals(3, bal2.size());
|
||||||
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.skip(2);
|
for(int i = 0; i < 3; i++)
|
||||||
Assert.assertEquals(3, bal2.size());
|
Assert.assertEquals(bal1.get(i+2), bal2.get(i));
|
||||||
for(int i = 0; i < 3; i++)
|
|
||||||
Assert.assertEquals(bal1.get(i+2), bal2.get(i));
|
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.skipWhile(du -> du.s.startsWith("h"));
|
||||||
|
Assert.assertEquals(2, bal3.size());
|
||||||
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.skipWhile(du -> du.s.startsWith("h"));
|
for(int i = 0; i < 2; i++)
|
||||||
Assert.assertEquals(2, bal3.size());
|
Assert.assertEquals(bal1.get(i+3), bal3.get(i));
|
||||||
for(int i = 0; i < 2; i++)
|
}
|
||||||
Assert.assertEquals(bal1.get(i+3), bal3.get(i));
|
|
||||||
}
|
@Test
|
||||||
|
public void testSum() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal = new BetterArrayList<>();
|
||||||
public void testSum() {
|
bal.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal = new BetterArrayList<Dummy>();
|
bal.add(new Dummy(2d,"test"));
|
||||||
bal.add(new Dummy(1d,"hello"));
|
bal.add(new Dummy(3d,"hello2"));
|
||||||
bal.add(new Dummy(2d,"test"));
|
|
||||||
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);
|
|
||||||
}
|
@Test
|
||||||
|
public void testTake() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
|
||||||
public void testTake() {
|
bal1.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal1 = new BetterArrayList<Dummy>();
|
bal1.add(new Dummy(2d,"hello"));
|
||||||
bal1.add(new Dummy(1d,"hello"));
|
bal1.add(new Dummy(3d,"hello"));
|
||||||
bal1.add(new Dummy(2d,"hello"));
|
bal1.add(new Dummy(4d,"test"));
|
||||||
bal1.add(new Dummy(3d,"hello"));
|
bal1.add(new Dummy(5d,"hello"));
|
||||||
bal1.add(new Dummy(4d,"test"));
|
|
||||||
bal1.add(new Dummy(5d,"hello"));
|
Assert.assertEquals(bal1, bal1.take(10));
|
||||||
|
|
||||||
Assert.assertEquals(bal1, bal1.take(10));
|
Assert.assertEquals(0, bal1.take(0).size());
|
||||||
|
|
||||||
Assert.assertEquals(0, bal1.take(0).size());
|
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.take(2);
|
||||||
|
Assert.assertEquals(2, bal2.size());
|
||||||
BetterArrayList<Dummy> bal2 = (BetterArrayList<Dummy>) bal1.take(2);
|
for(int i = 0; i < 2; i++)
|
||||||
Assert.assertEquals(2, bal2.size());
|
Assert.assertEquals(bal1.get(i), bal2.get(i));
|
||||||
for(int i = 0; i < 2; i++)
|
|
||||||
Assert.assertEquals(bal1.get(i), bal2.get(i));
|
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.takeWhile(du -> du.s.startsWith("h"));
|
||||||
|
Assert.assertEquals(3, bal3.size());
|
||||||
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.takeWhile(du -> du.s.startsWith("h"));
|
for(int i = 0; i < 3; i++)
|
||||||
Assert.assertEquals(3, bal3.size());
|
Assert.assertEquals(bal1.get(i), bal3.get(i));
|
||||||
for(int i = 0; i < 3; i++)
|
}
|
||||||
Assert.assertEquals(bal1.get(i), bal3.get(i));
|
|
||||||
}
|
@Test
|
||||||
|
public void testUnion() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal1 = new BetterArrayList<>();
|
||||||
public void testUnion() {
|
bal1.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal1 = new BetterArrayList<Dummy>();
|
bal1.add(new Dummy(2d,"test"));
|
||||||
bal1.add(new Dummy(1d,"hello"));
|
bal1.add(new Dummy(3d,"hello"));
|
||||||
bal1.add(new Dummy(2d,"test"));
|
bal1.add(new Dummy(4d,"test"));
|
||||||
bal1.add(new Dummy(3d,"hello"));
|
|
||||||
bal1.add(new Dummy(4d,"test"));
|
BetterArrayList<Dummy> bal2 = new BetterArrayList<>();
|
||||||
|
bal2.add(new Dummy(2d,"test"));
|
||||||
BetterArrayList<Dummy> bal2 = new BetterArrayList<Dummy>();
|
bal2.add(new Dummy(3d,"hello"));
|
||||||
bal2.add(new Dummy(2d,"test"));
|
bal2.add(new Dummy(5d,"test"));
|
||||||
bal2.add(new Dummy(3d,"hello"));
|
|
||||||
bal2.add(new Dummy(5d,"test"));
|
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.union(bal2);
|
||||||
|
Assert.assertEquals(2, bal3.size());
|
||||||
BetterArrayList<Dummy> bal3 = (BetterArrayList<Dummy>) bal1.union(bal2);
|
Assert.assertEquals(bal1.get(1), bal3.get(0));
|
||||||
Assert.assertEquals(2, bal3.size());
|
Assert.assertEquals(bal1.get(2), bal3.get(1));
|
||||||
Assert.assertEquals(bal1.get(1), bal3.get(0));
|
}
|
||||||
Assert.assertEquals(bal1.get(2), bal3.get(1));
|
|
||||||
}
|
@Test
|
||||||
|
public void testWhere() {
|
||||||
@Test
|
BetterArrayList<Dummy> bal = new BetterArrayList<>();
|
||||||
public void testWhere() {
|
bal.add(new Dummy(1d,"hello"));
|
||||||
BetterArrayList<Dummy> bal = new BetterArrayList<Dummy>();
|
bal.add(new Dummy(2d,"test"));
|
||||||
bal.add(new Dummy(1d,"hello"));
|
bal.add(new Dummy(3d,"hello"));
|
||||||
bal.add(new Dummy(2d,"test"));
|
|
||||||
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(new Dummy(1d,"hello"), bal2.get(0));
|
||||||
Assert.assertEquals(2, bal2.size());
|
Assert.assertEquals(new Dummy(3d,"hello"), bal2.get(1));
|
||||||
Assert.assertEquals(new Dummy(1d,"hello"), bal2.get(0));
|
}
|
||||||
Assert.assertEquals(new Dummy(3d,"hello"), bal2.get(1));
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user