From 2473f256f7c6cba1ad004293cf86cbb13dc9793c Mon Sep 17 00:00:00 2001 From: Klemek Date: Thu, 19 Apr 2018 13:46:35 +0200 Subject: [PATCH] BetterLists 1.2 : Code improvement --- .gitignore | 3 + README.md | 25 +- download/betterlists-1.2.jar | Bin 0 -> 7530 bytes .../klemek/betterlists/BetterArrayList.java | 39 +- .../klemek/betterlists/BetterLinkedList.java | 65 +- src/fr/klemek/betterlists/BetterList.java | 52 +- src/fr/klemek/betterlists/BetterStack.java | 2 +- src/fr/klemek/betterlists/BetterVector.java | 28 +- .../{ => test}/BetterListsTests.java | 666 +++++++++--------- 9 files changed, 470 insertions(+), 410 deletions(-) create mode 100644 download/betterlists-1.2.jar rename test/fr/klemek/betterlists/{ => test}/BetterListsTests.java (82%) diff --git a/.gitignore b/.gitignore index 9c0a459..b88b55f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /bin/ /.classpath /.settings/ +/target/ +/maven-repo.bat +/.idea/ diff --git a/README.md b/README.md index 03ede3b..84180e6 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ An extension of the java.util.List interface which include some of the C# LINQ u List classes are extended as well. (ArrayList -> BetterArrayList) -Current version v1.1 +Current version v1.2 Before BetterLists : ```Java @@ -30,7 +30,7 @@ NOTE : Please note that, unlike C# LINQ, these functions are not optimized at lo ## Download -* [betterlists-1.1.jar](../../raw/master/download/betterlists-1.0.jar) +* [betterlists-1.2.jar](../../raw/master/download/betterlists-1.2.jar) ## Maven @@ -49,7 +49,7 @@ You can use this project as a maven dependency with this : klemek betterlists - 1.1 + 1.2 ``` @@ -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. | | [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. | -| [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. | | [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. | @@ -111,19 +111,21 @@ BetterList invalidFrenchContacts = frenchContacts.exclusion(validContac ``` ### 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 BetterArrayList 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 -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 BetterArrayList 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 @@ -156,6 +158,7 @@ Sorts the elements of a sequence in ascending order by using a specified compare BetterArrayList contacts = BetterArrayList.fromList(someFunction()); BetterList orderedContacts = contacts.orderBy(c -> c.getName); +BetterList orderedContacts2 = contacts.orderByDescending(c -> c.getAge()); //oldest first ``` ### reverse @@ -179,7 +182,8 @@ Bypasses elements in the sequence as long as a specified condition is true and t ```Java BetterArrayList contacts = BetterArrayList.fromList(someFunction()); -BetterList contacts2 = contacts.skipWhile(c -> c.getEmail().startsWith("society")); +BetterList contacts2 = contacts.skip(3); +BetterList contacts3 = contacts.skipWhile(c -> c.getEmail().startsWith("society")); ``` ### sum @@ -195,7 +199,8 @@ Returns a specified number of contiguous elements from the start of the sequence ```Java BetterArrayList contacts = BetterArrayList.fromList(someFunction()); -BetterList contacts2 = contacts.takeWhile(c -> c.getEmail().startsWith("society")); +BetterList contacts2 = contacts.take(5); +BetterList contacts3 = contacts.takeWhile(c -> c.getEmail().startsWith("society")); ``` ### union diff --git a/download/betterlists-1.2.jar b/download/betterlists-1.2.jar new file mode 100644 index 0000000000000000000000000000000000000000..9ec4741125db5d8055362ae31bf09251f6247004 GIT binary patch literal 7530 zcmaiZ1yEeuwk;kYK!P_O+}+)2q;Yp`w2=V88VDhH(BL7sy9I)~LvRT0uE8B1=f3}b z@=o3J?%Y+o)~=d!)U4WT%vEa)6$N+%R2Ud!7?@kzSP;w~0S|)!W9iKLH~<5q@{dW+ zrGF*aLBRH4JJx^B!~0)(W?)xWurtKQ#nt6sH8{@=ApffYwC_|}Oz;A2=j}XKm#s1gl++g{_Y2InT~UejdYY zRWg{c!vyIsFd@Dc3nRJ(t@EUV>vXr41mUXSoY8#V$;C^94b}FYOK)>3I@*pn&$sNR z8fdg?-l^KiE*b0{9vhRS$nJ{PHY3nx!(>uTb@;u8zY6f~ZX14u~MGYCTt;;Cz>5^(i)S*IDk(Z|%v zEN@X1vjr^D*RRX=wEHqLu6>DNiB}_#KP&H_*HpDDz`5Zt)Z_5St!zm3)>z7%(dcQG zhtKF#(}##uI>;tQIuKj!8Sv6@pw+#J&LWcPb}Cn-Ux1Da=WiC3fB`~dyYDDQjr!jO z6-Hit;Uid7X_S;+pgc+qNhvh97XV-Li_K`dIU@&gG__cNctvi+wfL#2j#bOz(2t8% zMW3tJM-v=b7d_-Q-sYx&6ste46BIvMV zN9t%mK+$tWy_SL{IHL}(rISvx(`L4t`^szlYXiFK$-=J@tMBLD-H;>^DhmhBW8s3zNNpt$z{q zH|ZA1{NnH7U|^7*Nyq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q_`yB^utad&eJiEhdEa9hnGf^>U)X$ z+}RCGYB64y+JajyZ2^Sg&BP~~JvGiu6o?`nEq7w5oJnOTfcPQZmy`@I@cz=zR z35`P6UJ-LQe0()lrW72a6CB%}jw}xpXd))C@@tnWq|cBQ_o(s?O!Os9JQCN1=5eDr z#^;nF=bYh+Hq>L*F=};YRx$}Zgw2->LZi1!j^g@H4-0i(o#P{{%(gpJDflH=&fnjQ zR=W^wlu@_d$(rG8rF)0Zn5jF?D&}Vzb6iWYvp^nv+FtB*lq^8ak@lq*&H&ABYhhyF;lD$ z^GEDnAw}o5GSA50*>C7dg_((6Vt1u>5PB_*OQO#ATi&<5M(W?Pz;ZL8Ep40=YJ6Zq zBI~g`XkWq@E#y0I=h?vw@5umKN8+6WZe$&+u=K9^ZTt^?@H>j)kU?4a>XR**7SPu9izVpZ`XbeYPgOBlf~PRH(|A%9RUu#D5T_NrSKWN)j!K#gjN~9 zS$k1Tu&OFa8vocBEySSI?J?;G9B}h8iFAC+ro+!M11)%4_1LGX{wQEu3hM>8Q|%h= zk@|?Pq5unr3iHpF*E3!Ji2rYZ{d3V@7TE8geqR1*i2ZZjUzXVKP<&qguKROL{M9fH z%x|^+?-to_j1T{a@ox>YzXSi>2KWtp`8>=1eRKZ}{C88}@5q0bMt>s extends ArrayList implements BetterList { return new BetterArrayList<>(c); } + /** + * Constructs a list containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public static BetterArrayList asList(T... a) { + return new BetterArrayList<>(a); + } + /** * Constructs an empty list with an initial capacity of ten. */ @@ -45,14 +55,23 @@ public class BetterArrayList extends ArrayList implements BetterList { } /** - * Constructs an empty list with the specified initial capacity. - * - * @param initialCapacity - * - the initial capacity of the list - */ - public BetterArrayList(int initialCapacity) { - super(initialCapacity); - } + * Constructs a list containing the elements given in argument. + * + * @param a + * - the elements to be placed into this list + */ + public BetterArrayList(T... a) { + super(Arrays.asList(a)); + } + + /** + * Constructs an empty list with the specified initial capacity. + * + * @param initialCapacity - the initial capacity of the list + */ + public BetterArrayList(int initialCapacity) { + super(initialCapacity); + } /** * Returns a view of the portion of this list between the specified fromIndex, @@ -77,6 +96,6 @@ public class BetterArrayList extends ArrayList implements BetterList { */ @Override public BetterArrayList subList(int fromIndex, int toIndex) { - return (BetterArrayList) this.subList(fromIndex, toIndex); - } + return (BetterArrayList) super.subList(fromIndex, toIndex); + } } diff --git a/src/fr/klemek/betterlists/BetterLinkedList.java b/src/fr/klemek/betterlists/BetterLinkedList.java index 553624b..c572800 100644 --- a/src/fr/klemek/betterlists/BetterLinkedList.java +++ b/src/fr/klemek/betterlists/BetterLinkedList.java @@ -1,5 +1,6 @@ package fr.klemek.betterlists; +import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; @@ -26,6 +27,15 @@ public class BetterLinkedList extends LinkedList implements BetterList return new BetterLinkedList<>(c); } + /** + * Constructs a list containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public static BetterLinkedList asList(T... a) { + return new BetterLinkedList<>(a); + } + /** * Constructs an empty list. */ @@ -45,29 +55,36 @@ public class BetterLinkedList extends LinkedList implements BetterList } /** - * Returns a view of the portion of this list between the specified fromIndex, - * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the - * returned list is empty.) The returned list is backed by this list, so - * non-structural changes in the returned list are reflected in this list, and - * vice-versa. The returned list supports all of the optional list operations - * supported by this list. This method eliminates the need for explicit range - * operations (of the sort that commonly exist for arrays). Any operation that - * expects a list can be used as a range operation by passing a subList view - * instead of a whole list. (see List.subList) - * - * @param fromIndex - * - low endpoint (inclusive) of the subList - * @param toIndex - * - high endpoint (exclusive) of the subList - * @return a view of the specified range within this list - * @throws IndexOutOfBoundsException - * for an illegal endpoint index value (fromIndex < 0 || toIndex > - * size || fromIndex > toIndex) - * @see java.util.List - */ - @Override - public BetterLinkedList subList(int fromIndex, int toIndex) { - return (BetterLinkedList) this.subList(fromIndex, toIndex); - } + * Constructs a list containing the elements given in argument. + * + * @param a + * - the elements to be placed into this list + */ + public BetterLinkedList(T... a) { + super(Arrays.asList(a)); + } + + /** + * Returns a view of the portion of this list between the specified fromIndex, + * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the + * returned list is empty.) The returned list is backed by this list, so + * non-structural changes in the returned list are reflected in this list, and + * vice-versa. The returned list supports all of the optional list operations + * supported by this list. This method eliminates the need for explicit range + * operations (of the sort that commonly exist for arrays). Any operation that + * expects a list can be used as a range operation by passing a subList view + * instead of a whole list. (see List.subList) + * + * @param fromIndex - low endpoint (inclusive) of the subList + * @param toIndex - high endpoint (exclusive) of the subList + * @return a view of the specified range within this list + * @throws IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > + * size || fromIndex > toIndex) + * @see java.util.List + */ + @Override + public BetterLinkedList subList(int fromIndex, int toIndex) { + return (BetterLinkedList) super.subList(fromIndex, toIndex); + } } diff --git a/src/fr/klemek/betterlists/BetterList.java b/src/fr/klemek/betterlists/BetterList.java index 9c991d5..9ed74c7 100644 --- a/src/fr/klemek/betterlists/BetterList.java +++ b/src/fr/klemek/betterlists/BetterList.java @@ -38,7 +38,7 @@ public interface BetterList extends List { * @return true if any elements in the source sequence pass the test in the * specified predicate; otherwise, false. */ - public default boolean any(Function predicate) { + default boolean any(Function predicate) { for (T element : this) if (predicate.apply(element)) return true; @@ -50,7 +50,7 @@ public interface BetterList extends List { * * @return The number of elements in the input sequence. */ - public default int count() { + default int count() { return count(e -> true); } @@ -63,7 +63,7 @@ public interface BetterList extends List { * @return A number that represents how many elements in the sequence satisfy * the condition in the predicate function. */ - public default int count(Function predicate) { + default int count(Function predicate) { int out = 0; for (T element : this) if (predicate.apply(element)) @@ -80,7 +80,7 @@ public interface BetterList extends List { * @return A List that contains the elements from the first sequence not present * in the other. */ - public default BetterList exclusion(List other) { + default BetterList exclusion(List other) { BetterList out = new BetterArrayList<>(); for (T element : this) if (!other.contains(element)) @@ -95,7 +95,7 @@ public interface BetterList extends List { * If the sequence is empty. * @return The first element in the sequence. */ - public default T first() { + default T first() { return first(e -> true); } @@ -111,7 +111,7 @@ public interface BetterList extends List { * @return The first element in the sequence that passes the test in the * specified predicate function. */ - public default T first(Function predicate) { + default T first(Function predicate) { for (T element : this) if (predicate.apply(element)) return element; @@ -130,7 +130,7 @@ public interface BetterList extends List { * test specified by predicate; otherwise, the first element in the * sequence that passes the test specified by predicate. */ - public default T firstOrDefault(Function predicate, T defaultValue) { + default T firstOrDefault(Function predicate, T defaultValue) { for (T element : this) if (predicate.apply(element)) return element; @@ -146,7 +146,7 @@ public interface BetterList extends List { * @return defaultValue if the sequence is empty otherwise, the first element in * the sequence. */ - public default T firstOrDefault(T defaultValue) { + default T firstOrDefault(T defaultValue) { return firstOrDefault(e -> true, defaultValue); } @@ -157,7 +157,7 @@ public interface BetterList extends List { * If the sequence is empty. * @return the last element of the sequence. */ - public default T last() { + default T last() { return last(e -> true); } @@ -173,7 +173,7 @@ public interface BetterList extends List { * @return the last element of the sequence that satisfies a specified * condition. */ - public default T last(Function predicate) { + default T last(Function predicate) { T value = null; for (T element : this) if (predicate.apply(element)) @@ -195,7 +195,7 @@ public interface BetterList extends List { * test specified by predicate; otherwise, the last element in the * sequence that passes the test specified by predicate. */ - public default T lastOrDefault(Function predicate, T defaultValue) { + default T lastOrDefault(Function predicate, T defaultValue) { T value = null; for (T element : this) if (predicate.apply(element)) @@ -212,7 +212,7 @@ public interface BetterList extends List { * @return defaultValue if the sequence is empty otherwise, the last element in * the sequence. */ - public default T lastOrDefault(T defaultValue) { + default T lastOrDefault(T defaultValue) { return lastOrDefault(e -> true, defaultValue); } @@ -225,7 +225,7 @@ public interface BetterList extends List { * @return The value of type Double that corresponds to the maximum value in the * sequence or null if the sequence is empty. */ - public default Double max(Function selector) { + default Double max(Function selector) { Double max = null; for (T element : this) if (max == null || selector.apply(element) > max) @@ -242,7 +242,7 @@ public interface BetterList extends List { * @return The mean of the projected values. Null if the sequence contains no * elements. */ - public default Double mean(Function selector) { + default Double mean(Function selector) { int count = this.count(); if (count == 0) return null; @@ -258,7 +258,7 @@ public interface BetterList extends List { * @return The value of type Double that corresponds to the minimum value in the * sequence or null if the sequence is empty. */ - public default Double min(Function selector) { + default Double min(Function selector) { Double min = null; for (T element : this) if (min == null || selector.apply(element) < min) @@ -272,7 +272,7 @@ public interface BetterList extends List { * - A transform function to apply to each element. * @return a List whose elements are sorted according to a key. */ - public default > BetterList orderBy(Function selector){ + default > BetterList orderBy(Function selector) { BetterList out = new BetterArrayList<>(); out.addAll(this); Collections.sort(out, (o1,o2) -> selector.apply(o1).compareTo(selector.apply(o2))); @@ -285,7 +285,7 @@ public interface BetterList extends List { * - A transform function to apply to each element. * @return a List whose elements are sorted according to a key. */ - public default > BetterList orderByDescending(Function selector){ + default > BetterList orderByDescending(Function selector) { BetterList out = new BetterArrayList<>(); out.addAll(this); Collections.sort(out, (o1,o2) -> selector.apply(o2).compareTo(selector.apply(o1))); @@ -298,7 +298,7 @@ public interface BetterList extends List { * @return A sequence whose elements correspond to those of the sequence in * reverse order. */ - public default BetterList reverse() { + default BetterList reverse() { BetterList out = new BetterArrayList<>(this.size()); for (T element : this) out.add(0, element); @@ -315,7 +315,7 @@ public interface BetterList extends List { * @return A List whose elements are the result of invoking the transform * function on each element of the sequence. */ - public default BetterList select(Function selector) { + default BetterList select(Function selector) { BetterList out = new BetterArrayList<>(); for (T element : this) out.add(selector.apply(element)); @@ -332,7 +332,7 @@ public interface BetterList extends List { * @return a List that contains the elements that occur after the specified * index in the sequence. */ - public default BetterList skip(int count) { + default BetterList skip(int count) { BetterList out = new BetterArrayList<>(); int n = 0; for (T element : this) { @@ -353,7 +353,7 @@ public interface BetterList extends List { * first element in the linear series that does not pass the test * specified by predicate. */ - public default BetterList skipWhile(Function predicate) { + default BetterList skipWhile(Function predicate) { BetterList out = new BetterArrayList<>(); boolean match = true; for (T element : this) @@ -373,7 +373,7 @@ public interface BetterList extends List { * @return The sum of the projected values. Zero if the sequence contains no * elements. */ - public default Double sum(Function selector) { + default Double sum(Function selector) { double sum = 0d; for (T element : this) sum += selector.apply(element); @@ -389,7 +389,7 @@ public interface BetterList extends List { * @return a List that contains the specified number of elements from the start * of the input sequence. */ - public default BetterList take(int count) { + default BetterList take(int count) { BetterList out = new BetterArrayList<>(count); int n = 0; for (T element : this) { @@ -410,7 +410,7 @@ public interface BetterList extends List { * @return a List that contains the elements from the sequence that occur before * the element at which the test no longer passes. */ - public default BetterList takeWhile(Function predicate) { + default BetterList takeWhile(Function predicate) { BetterList out = new BetterArrayList<>(); for (T element : this) if (predicate.apply(element)) @@ -429,7 +429,7 @@ public interface BetterList extends List { * @return A List that contains the elements from both sequences, excluding * duplicates. */ - public default BetterList union(List other) { + default BetterList union(List other) { BetterList out = new BetterArrayList<>(); for (T element : this) if (other.contains(element)) @@ -445,7 +445,7 @@ public interface BetterList extends List { * @return a List that contains elements from the sequence that satisfy the * condition. */ - public default BetterList where(Function predicate) { + default BetterList where(Function predicate) { BetterList out = new BetterArrayList<>(); for (T element : this) if (predicate.apply(element)) diff --git a/src/fr/klemek/betterlists/BetterStack.java b/src/fr/klemek/betterlists/BetterStack.java index 16904f0..82f32e1 100644 --- a/src/fr/klemek/betterlists/BetterStack.java +++ b/src/fr/klemek/betterlists/BetterStack.java @@ -44,7 +44,7 @@ public class BetterStack extends Stack implements BetterList { */ @Override public BetterStack subList(int fromIndex, int toIndex) { - return (BetterStack) this.subList(fromIndex, toIndex); + return (BetterStack) super.subList(fromIndex, toIndex); } } diff --git a/src/fr/klemek/betterlists/BetterVector.java b/src/fr/klemek/betterlists/BetterVector.java index 9ee200e..71f372e 100644 --- a/src/fr/klemek/betterlists/BetterVector.java +++ b/src/fr/klemek/betterlists/BetterVector.java @@ -1,5 +1,6 @@ package fr.klemek.betterlists; +import java.util.Arrays; import java.util.Collection; import java.util.Vector; @@ -25,7 +26,16 @@ public class BetterVector extends Vector implements BetterList { public static BetterVector fromList(Collection c) { return new BetterVector<>(c); } - + + /** + * Constructs a vector containing the elements given in argument. + * + * @param a - the elements to be placed into this vector + */ + public static BetterVector asVector(T... a) { + return new BetterVector<>(a); + } + /** * Constructs an empty vector so that its internal data array has size 10 and * its standard capacity increment is zero. @@ -45,12 +55,20 @@ public class BetterVector extends Vector implements BetterList { super(c); } + /** + * Constructs a vector containing the elements given in argument. + * + * @param a - the elements to be placed into this list + */ + public BetterVector(T... a) { + super(Arrays.asList(a)); + } + /** * Constructs an empty vector with the specified initial capacity and with its * capacity increment equal to zero. - * - * @param initialCapacity - * - the initial capacity of the vector + * + * @param initialCapacity - the initial capacity of the vector */ public BetterVector(int initialCapacity) { super(initialCapacity); @@ -93,7 +111,7 @@ public class BetterVector extends Vector implements BetterList { */ @Override public BetterVector subList(int fromIndex, int toIndex) { - return (BetterVector) this.subList(fromIndex, toIndex); + return (BetterVector) super.subList(fromIndex, toIndex); } } diff --git a/test/fr/klemek/betterlists/BetterListsTests.java b/test/fr/klemek/betterlists/test/BetterListsTests.java similarity index 82% rename from test/fr/klemek/betterlists/BetterListsTests.java rename to test/fr/klemek/betterlists/test/BetterListsTests.java index 8f9e90a..581519c 100644 --- a/test/fr/klemek/betterlists/BetterListsTests.java +++ b/test/fr/klemek/betterlists/test/BetterListsTests.java @@ -1,334 +1,332 @@ -package fr.klemek.betterlists; - -import java.util.ArrayList; -import java.util.NoSuchElementException; - -import org.junit.Assert; -import org.junit.Test; - -public class BetterListsTests { - - protected class Dummy { - double d; - String s; - - public Dummy(double d, String s) { - this.d = d; - this.s = s; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Dummy other = (Dummy) obj; - if (!getOuterType().equals(other.getOuterType())) - return false; - if (Double.doubleToLongBits(d) != Double.doubleToLongBits(other.d)) - return false; - if (s == null) { - if (other.s != null) - return false; - } else if (!s.equals(other.s)) - return false; - return true; - } - - private BetterListsTests getOuterType() { - return BetterListsTests.this; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + getOuterType().hashCode(); - long temp; - temp = Double.doubleToLongBits(d); - result = prime * result + (int) (temp ^ (temp >>> 32)); - result = prime * result + ((s == null) ? 0 : s.hashCode()); - return result; - } - } - - @Test - public void testAll() { - ArrayList al = new ArrayList(); - al.add(new Dummy(1d,"hello")); - al.add(new Dummy(2d,"test")); - al.add(new Dummy(2d,"hello")); - - BetterArrayList bal = BetterArrayList.fromList(al); - - Assert.assertTrue(bal.all(du -> du.d > 0)); - Assert.assertFalse(bal.all(du -> du.d > 1)); - } - - @Test - public void testAny() { - BetterArrayList bal = new BetterArrayList(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(2d,"hello")); - - Assert.assertTrue(bal.any(du -> du.s.startsWith("t"))); - Assert.assertFalse(bal.any(du -> du.s.startsWith("b"))); - } - - @Test - public void testCount() { - BetterArrayList bal = new BetterArrayList(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(2d,"hello")); - - Assert.assertEquals(3, bal.count()); - Assert.assertEquals(2, bal.count(du -> du.s.length() > 4)); - } - - @Test - public void testExclude() { - BetterArrayList bal1 = new BetterArrayList(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"test")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - - BetterArrayList bal2 = new BetterArrayList(); - bal2.add(new Dummy(2d,"test")); - bal2.add(new Dummy(3d,"hello")); - bal2.add(new Dummy(5d,"test")); - - BetterArrayList bal3 = (BetterArrayList) bal1.exclusion(bal2); - Assert.assertEquals(2, bal3.size()); - Assert.assertEquals(bal1.get(0), bal3.get(0)); - Assert.assertEquals(bal1.get(3), bal3.get(1)); - } - - @Test - public void testFirst() { - BetterArrayList bal = new BetterArrayList(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(2d,"hello")); - - Assert.assertEquals(bal.get(0), bal.first(du -> du.s.startsWith("h"))); - - try { - bal.first(du -> du.s.startsWith("d")); - Assert.fail("no error"); - } catch (NoSuchElementException e) { - } - - 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"))); - } - - @Test - public void testLast() { - BetterArrayList bal = new BetterArrayList(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(2d,"hello")); - - Assert.assertEquals(bal.get(2), bal.last(du -> du.s.startsWith("h"))); - - try { - bal.last(du -> du.s.startsWith("d")); - Assert.fail("no error"); - } catch (NoSuchElementException e) { - } - - Assert.assertEquals(bal.get(2), bal.lastOrDefault(du -> du.s.startsWith("h"), new Dummy(3d,"default"))); - - Assert.assertEquals(new Dummy(3d,"default"), bal.lastOrDefault(du -> du.s.startsWith("d"), new Dummy(3d,"default"))); - } - - @Test - public void testMax() { - BetterArrayList bal = new BetterArrayList(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello2")); - - Assert.assertEquals(6d, bal.max(du -> (double)du.s.length()), 0.001d); - } - - @Test - public void testMean() { - BetterArrayList bal = new BetterArrayList(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello2")); - - Assert.assertEquals(5d, bal.mean(du -> (double)du.s.length()), 0.001d); - } - - @Test - public void testMin() { - BetterArrayList bal = new BetterArrayList(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello2")); - - Assert.assertEquals(4d, bal.min(du -> (double)du.s.length()), 0.001d); - } - - @Test - public void testOrderBy() { - BetterArrayList bal1 = new BetterArrayList(); - bal1.add(new Dummy(1d,"hello1")); - bal1.add(new Dummy(2d,"hello2")); - bal1.add(new Dummy(3d,"hello0")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello4")); - - BetterArrayList bal2 = (BetterArrayList) bal1.orderBy(c -> c.s); - Assert.assertNotEquals(bal1, bal2); - Assert.assertEquals(bal1.get(2), bal2.get(0)); - Assert.assertEquals(bal1.get(0), bal2.get(1)); - Assert.assertEquals(bal1.get(1), bal2.get(2)); - Assert.assertEquals(bal1.get(4), bal2.get(3)); - Assert.assertEquals(bal1.get(3), bal2.get(4)); - } - - @Test - public void testOrderByDescending() { - BetterArrayList bal1 = new BetterArrayList(); - bal1.add(new Dummy(1d,"hello1")); - bal1.add(new Dummy(2d,"hello2")); - bal1.add(new Dummy(3d,"hello0")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello4")); - - BetterArrayList bal2 = (BetterArrayList) bal1.orderByDescending(c -> c.d); - Assert.assertNotEquals(bal1, bal2); - for(int i = 0; i < 5; i++) - Assert.assertEquals(bal1.get(4-i), bal2.get(i)); - } - - @Test - public void testReverse() { - BetterArrayList bal1 = new BetterArrayList(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"hello")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello")); - - BetterArrayList bal2 = (BetterArrayList) bal1.reverse(); - Assert.assertEquals(5, bal2.size()); - for(int i = 0; i < 5; i++) - Assert.assertEquals(bal1.get(i), bal2.get(4-i)); - } - - @Test - public void testSelect() { - BetterArrayList bal1 = new BetterArrayList(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"hello")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello")); - - BetterArrayList bal2 = (BetterArrayList) bal1.select(du -> du.d); - Assert.assertEquals(5, bal2.size()); - for(int i = 0; i < 5; i++) - Assert.assertEquals(bal1.get(i).d, bal2.get(i), 0.0001); - } - - @Test - public void testSkip() { - BetterArrayList bal1 = new BetterArrayList(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"hello")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello")); - - Assert.assertEquals(bal1, bal1.skip(0)); - Assert.assertEquals(0, bal1.skip(10).size()); - - BetterArrayList bal2 = (BetterArrayList) bal1.skip(2); - Assert.assertEquals(3, bal2.size()); - for(int i = 0; i < 3; i++) - Assert.assertEquals(bal1.get(i+2), bal2.get(i)); - - BetterArrayList bal3 = (BetterArrayList) bal1.skipWhile(du -> du.s.startsWith("h")); - Assert.assertEquals(2, bal3.size()); - for(int i = 0; i < 2; i++) - Assert.assertEquals(bal1.get(i+3), bal3.get(i)); - } - - @Test - public void testSum() { - BetterArrayList bal = new BetterArrayList(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello2")); - - Assert.assertEquals(6d, bal.sum(du -> du.d), 0.001d); - } - - @Test - public void testTake() { - BetterArrayList bal1 = new BetterArrayList(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"hello")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - bal1.add(new Dummy(5d,"hello")); - - Assert.assertEquals(bal1, bal1.take(10)); - - Assert.assertEquals(0, bal1.take(0).size()); - - BetterArrayList bal2 = (BetterArrayList) bal1.take(2); - Assert.assertEquals(2, bal2.size()); - for(int i = 0; i < 2; i++) - Assert.assertEquals(bal1.get(i), bal2.get(i)); - - BetterArrayList bal3 = (BetterArrayList) bal1.takeWhile(du -> du.s.startsWith("h")); - Assert.assertEquals(3, bal3.size()); - for(int i = 0; i < 3; i++) - Assert.assertEquals(bal1.get(i), bal3.get(i)); - } - - @Test - public void testUnion() { - BetterArrayList bal1 = new BetterArrayList(); - bal1.add(new Dummy(1d,"hello")); - bal1.add(new Dummy(2d,"test")); - bal1.add(new Dummy(3d,"hello")); - bal1.add(new Dummy(4d,"test")); - - BetterArrayList bal2 = new BetterArrayList(); - bal2.add(new Dummy(2d,"test")); - bal2.add(new Dummy(3d,"hello")); - bal2.add(new Dummy(5d,"test")); - - BetterArrayList bal3 = (BetterArrayList) bal1.union(bal2); - Assert.assertEquals(2, bal3.size()); - Assert.assertEquals(bal1.get(1), bal3.get(0)); - Assert.assertEquals(bal1.get(2), bal3.get(1)); - } - - @Test - public void testWhere() { - BetterArrayList bal = new BetterArrayList(); - bal.add(new Dummy(1d,"hello")); - bal.add(new Dummy(2d,"test")); - bal.add(new Dummy(3d,"hello")); - - BetterArrayList bal2 = (BetterArrayList) bal.where(du -> du.s.startsWith("h")); - - Assert.assertEquals(2, bal2.size()); - Assert.assertEquals(new Dummy(1d,"hello"), bal2.get(0)); - Assert.assertEquals(new Dummy(3d,"hello"), bal2.get(1)); - } -} +package fr.klemek.betterlists.test; + +import java.util.ArrayList; +import java.util.NoSuchElementException; + +import fr.klemek.betterlists.BetterArrayList; +import org.junit.Assert; +import org.junit.Test; + +public class BetterListsTests { + + protected class Dummy { + final double d; + final String s; + + public Dummy(double d, String s) { + this.d = d; + this.s = s; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Dummy other = (Dummy) obj; + if (!getOuterType().equals(other.getOuterType())) + return false; + if (Double.doubleToLongBits(d) != Double.doubleToLongBits(other.d)) + return false; + if (s == null && other.s != null) { + return false; + } else return s.equals(other.s); + } + + private BetterListsTests getOuterType() { + return BetterListsTests.this; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + getOuterType().hashCode(); + long temp; + temp = Double.doubleToLongBits(d); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + ((s == null) ? 0 : s.hashCode()); + return result; + } + } + + @Test + public void testAll() { + ArrayList al = new ArrayList<>(); + al.add(new Dummy(1d,"hello")); + al.add(new Dummy(2d,"test")); + al.add(new Dummy(2d,"hello")); + + BetterArrayList bal = BetterArrayList.fromList(al); + + Assert.assertTrue(bal.all(du -> du.d > 0)); + Assert.assertFalse(bal.all(du -> du.d > 1)); + } + + @Test + public void testAny() { + BetterArrayList bal = new BetterArrayList<>(); + bal.add(new Dummy(1d,"hello")); + bal.add(new Dummy(2d,"test")); + bal.add(new Dummy(2d,"hello")); + + Assert.assertTrue(bal.any(du -> du.s.startsWith("t"))); + Assert.assertFalse(bal.any(du -> du.s.startsWith("b"))); + } + + @Test + public void testCount() { + BetterArrayList bal = new BetterArrayList<>(); + bal.add(new Dummy(1d,"hello")); + bal.add(new Dummy(2d,"test")); + bal.add(new Dummy(2d,"hello")); + + Assert.assertEquals(3, bal.count()); + Assert.assertEquals(2, bal.count(du -> du.s.length() > 4)); + } + + @Test + public void testExclude() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d,"hello")); + bal1.add(new Dummy(2d,"test")); + bal1.add(new Dummy(3d,"hello")); + bal1.add(new Dummy(4d,"test")); + + BetterArrayList bal2 = new BetterArrayList<>(); + bal2.add(new Dummy(2d,"test")); + bal2.add(new Dummy(3d,"hello")); + bal2.add(new Dummy(5d,"test")); + + BetterArrayList bal3 = (BetterArrayList) bal1.exclusion(bal2); + Assert.assertEquals(2, bal3.size()); + Assert.assertEquals(bal1.get(0), bal3.get(0)); + Assert.assertEquals(bal1.get(3), bal3.get(1)); + } + + @Test + public void testFirst() { + BetterArrayList bal = new BetterArrayList<>(); + bal.add(new Dummy(1d,"hello")); + bal.add(new Dummy(2d,"test")); + bal.add(new Dummy(2d,"hello")); + + Assert.assertEquals(bal.get(0), bal.first(du -> du.s.startsWith("h"))); + + try { + bal.first(du -> du.s.startsWith("d")); + Assert.fail("no error"); + } catch (NoSuchElementException e) { + } + + 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"))); + } + + @Test + public void testLast() { + BetterArrayList bal = new BetterArrayList<>(); + bal.add(new Dummy(1d,"hello")); + bal.add(new Dummy(2d,"test")); + bal.add(new Dummy(2d,"hello")); + + Assert.assertEquals(bal.get(2), bal.last(du -> du.s.startsWith("h"))); + + try { + bal.last(du -> du.s.startsWith("d")); + Assert.fail("no error"); + } catch (NoSuchElementException e) { + } + + Assert.assertEquals(bal.get(2), bal.lastOrDefault(du -> du.s.startsWith("h"), new Dummy(3d,"default"))); + + Assert.assertEquals(new Dummy(3d,"default"), bal.lastOrDefault(du -> du.s.startsWith("d"), new Dummy(3d,"default"))); + } + + @Test + public void testMax() { + BetterArrayList bal = new BetterArrayList<>(); + bal.add(new Dummy(1d,"hello")); + bal.add(new Dummy(2d,"test")); + bal.add(new Dummy(3d,"hello2")); + + Assert.assertEquals(6d, bal.max(du -> (double)du.s.length()), 0.001d); + } + + @Test + public void testMean() { + BetterArrayList bal = new BetterArrayList<>(); + bal.add(new Dummy(1d,"hello")); + bal.add(new Dummy(2d,"test")); + bal.add(new Dummy(3d,"hello2")); + + Assert.assertEquals(5d, bal.mean(du -> (double)du.s.length()), 0.001d); + } + + @Test + public void testMin() { + BetterArrayList bal = new BetterArrayList<>(); + bal.add(new Dummy(1d,"hello")); + bal.add(new Dummy(2d,"test")); + bal.add(new Dummy(3d,"hello2")); + + Assert.assertEquals(4d, bal.min(du -> (double)du.s.length()), 0.001d); + } + + @Test + public void testOrderBy() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d,"hello1")); + bal1.add(new Dummy(2d,"hello2")); + bal1.add(new Dummy(3d,"hello0")); + bal1.add(new Dummy(4d,"test")); + bal1.add(new Dummy(5d,"hello4")); + + BetterArrayList bal2 = (BetterArrayList) bal1.orderBy(c -> c.s); + Assert.assertNotEquals(bal1, bal2); + Assert.assertEquals(bal1.get(2), bal2.get(0)); + Assert.assertEquals(bal1.get(0), bal2.get(1)); + Assert.assertEquals(bal1.get(1), bal2.get(2)); + Assert.assertEquals(bal1.get(4), bal2.get(3)); + Assert.assertEquals(bal1.get(3), bal2.get(4)); + } + + @Test + public void testOrderByDescending() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d,"hello1")); + bal1.add(new Dummy(2d,"hello2")); + bal1.add(new Dummy(3d,"hello0")); + bal1.add(new Dummy(4d,"test")); + bal1.add(new Dummy(5d,"hello4")); + + BetterArrayList bal2 = (BetterArrayList) bal1.orderByDescending(c -> c.d); + Assert.assertNotEquals(bal1, bal2); + for(int i = 0; i < 5; i++) + Assert.assertEquals(bal1.get(4-i), bal2.get(i)); + } + + @Test + public void testReverse() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d,"hello")); + bal1.add(new Dummy(2d,"hello")); + bal1.add(new Dummy(3d,"hello")); + bal1.add(new Dummy(4d,"test")); + bal1.add(new Dummy(5d,"hello")); + + BetterArrayList bal2 = (BetterArrayList) bal1.reverse(); + Assert.assertEquals(5, bal2.size()); + for(int i = 0; i < 5; i++) + Assert.assertEquals(bal1.get(i), bal2.get(4-i)); + } + + @Test + public void testSelect() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d,"hello")); + bal1.add(new Dummy(2d,"hello")); + bal1.add(new Dummy(3d,"hello")); + bal1.add(new Dummy(4d,"test")); + bal1.add(new Dummy(5d,"hello")); + + BetterArrayList bal2 = (BetterArrayList) bal1.select(du -> du.d); + Assert.assertEquals(5, bal2.size()); + for(int i = 0; i < 5; i++) + Assert.assertEquals(bal1.get(i).d, bal2.get(i), 0.0001); + } + + @Test + public void testSkip() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d,"hello")); + bal1.add(new Dummy(2d,"hello")); + bal1.add(new Dummy(3d,"hello")); + bal1.add(new Dummy(4d,"test")); + bal1.add(new Dummy(5d,"hello")); + + Assert.assertEquals(bal1, bal1.skip(0)); + Assert.assertEquals(0, bal1.skip(10).size()); + + BetterArrayList bal2 = (BetterArrayList) bal1.skip(2); + Assert.assertEquals(3, bal2.size()); + for(int i = 0; i < 3; i++) + Assert.assertEquals(bal1.get(i+2), bal2.get(i)); + + BetterArrayList bal3 = (BetterArrayList) bal1.skipWhile(du -> du.s.startsWith("h")); + Assert.assertEquals(2, bal3.size()); + for(int i = 0; i < 2; i++) + Assert.assertEquals(bal1.get(i+3), bal3.get(i)); + } + + @Test + public void testSum() { + BetterArrayList bal = new BetterArrayList<>(); + bal.add(new Dummy(1d,"hello")); + bal.add(new Dummy(2d,"test")); + bal.add(new Dummy(3d,"hello2")); + + Assert.assertEquals(6d, bal.sum(du -> du.d), 0.001d); + } + + @Test + public void testTake() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d,"hello")); + bal1.add(new Dummy(2d,"hello")); + bal1.add(new Dummy(3d,"hello")); + bal1.add(new Dummy(4d,"test")); + bal1.add(new Dummy(5d,"hello")); + + Assert.assertEquals(bal1, bal1.take(10)); + + Assert.assertEquals(0, bal1.take(0).size()); + + BetterArrayList bal2 = (BetterArrayList) bal1.take(2); + Assert.assertEquals(2, bal2.size()); + for(int i = 0; i < 2; i++) + Assert.assertEquals(bal1.get(i), bal2.get(i)); + + BetterArrayList bal3 = (BetterArrayList) bal1.takeWhile(du -> du.s.startsWith("h")); + Assert.assertEquals(3, bal3.size()); + for(int i = 0; i < 3; i++) + Assert.assertEquals(bal1.get(i), bal3.get(i)); + } + + @Test + public void testUnion() { + BetterArrayList bal1 = new BetterArrayList<>(); + bal1.add(new Dummy(1d,"hello")); + bal1.add(new Dummy(2d,"test")); + bal1.add(new Dummy(3d,"hello")); + bal1.add(new Dummy(4d,"test")); + + BetterArrayList bal2 = new BetterArrayList<>(); + bal2.add(new Dummy(2d,"test")); + bal2.add(new Dummy(3d,"hello")); + bal2.add(new Dummy(5d,"test")); + + BetterArrayList bal3 = (BetterArrayList) bal1.union(bal2); + Assert.assertEquals(2, bal3.size()); + Assert.assertEquals(bal1.get(1), bal3.get(0)); + Assert.assertEquals(bal1.get(2), bal3.get(1)); + } + + @Test + public void testWhere() { + BetterArrayList bal = new BetterArrayList<>(); + bal.add(new Dummy(1d,"hello")); + bal.add(new Dummy(2d,"test")); + bal.add(new Dummy(3d,"hello")); + + BetterArrayList bal2 = (BetterArrayList) bal.where(du -> du.s.startsWith("h")); + + Assert.assertEquals(2, bal2.size()); + Assert.assertEquals(new Dummy(1d,"hello"), bal2.get(0)); + Assert.assertEquals(new Dummy(3d,"hello"), bal2.get(1)); + } +}