Skip to content

Commit 5284b9d

Browse files
committed
Add support for the method $.chain(iterable, size).
1 parent b4a530d commit 5284b9d

File tree

8 files changed

+56
-1
lines changed

8 files changed

+56
-1
lines changed

lodash-plugin/src/main/java/com/github/underscore/lodash/$.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ public static <T> Chain<T> chain(final Iterable<T> iterable) {
632632
return new $.Chain<T>(newArrayList(iterable));
633633
}
634634

635+
public static <T> Chain<T> chain(final Iterable<T> iterable, int size) {
636+
return new $.Chain<T>(newArrayList(iterable, size));
637+
}
638+
635639
public static <T> Chain<T> chain(final T ... list) {
636640
return new $.Chain<T>(Arrays.asList(list));
637641
}

lodash-plugin/src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ public void main() {
575575
new $("");
576576
new $(asList()).chain();
577577
$.chain(new ArrayList<String>());
578+
$.chain(new ArrayList<String>(), 1);
578579
$.chain(new HashSet<String>());
579580
$.chain(new String[] {});
580581
$.chain("");

math-plugin/src/main/java/com/github/underscore/math/$.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ public static <T> Chain<T> chain(final Iterable<T> iterable) {
362362
return new $.Chain<T>(newArrayList(iterable));
363363
}
364364

365+
public static <T> Chain<T> chain(final Iterable<T> iterable, int size) {
366+
return new $.Chain<T>(newArrayList(iterable, size));
367+
}
368+
365369
public static <T> Chain<T> chain(final T ... array) {
366370
return new $.Chain<T>(Arrays.asList(array));
367371
}

math-plugin/src/test/java/com/github/underscore/math/MathTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ public void main() {
235235
$.main(new String[] {});
236236
new $("");
237237
new $(asList()).chain();
238+
$.chain(new ArrayList<String>(), 1);
238239
$.chain(new HashSet<String>());
239240
$.chain(new String[] {});
240241
}

src/main/java/com/github/underscore/$.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,10 @@ public static <T> Chain<T> chain(final Iterable<T> iterable) {
18871887
return new $.Chain<T>(newArrayList(iterable));
18881888
}
18891889

1890+
public static <T> Chain<T> chain(final Iterable<T> iterable, int size) {
1891+
return new $.Chain<T>(newArrayList(iterable, size));
1892+
}
1893+
18901894
@SuppressWarnings("unchecked")
18911895
public static <T> Chain<T> chain(final T ... array) {
18921896
return new $.Chain<T>(Arrays.asList(array));
@@ -2567,7 +2571,7 @@ protected static <T> List<T> newArrayList() {
25672571
}
25682572

25692573
@SuppressWarnings("unchecked")
2570-
protected static <T> List<T> newArrayList(Iterable<T> iterable) {
2574+
protected static <T> List<T> newArrayList(final Iterable<T> iterable) {
25712575
final List<T> result;
25722576
if (iterable instanceof Collection) {
25732577
result = new ArrayList<T>((Collection) iterable);
@@ -2580,6 +2584,14 @@ protected static <T> List<T> newArrayList(Iterable<T> iterable) {
25802584
return result;
25812585
}
25822586

2587+
protected static <T> List<T> newArrayList(final Iterable<T> iterable, final int size) {
2588+
final List<T> result = new ArrayList<T>();
2589+
for (int index = 0; iterable.iterator().hasNext() && index < size; index += 1) {
2590+
result.add(iterable.iterator().next());
2591+
}
2592+
return result;
2593+
}
2594+
25832595
protected static List<Integer> newIntegerList(int ... array) {
25842596
final List<Integer> result = new ArrayList<Integer>(array.length);
25852597
for (final int item : array) {

src/test/java/com/github/underscore/UnderscoreTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,34 @@ public long[] apply(long[] arg) {
299299
assertArrayEquals(new long[] {1, 2}, iterable.iterator().next());
300300
}
301301

302+
@Test
303+
public void iterateChain() {
304+
Iterable<long[]> iterable = $.<long[]>iterate(new long[] {1, 1}, new UnaryOperator<long[]>() {
305+
public long[] apply(long[] arg) {
306+
return new long[] {arg[1], arg[0] + arg[1]};
307+
}
308+
});
309+
assertEquals(1L, $.chain(iterable, 5).first().item()[0]);
310+
class MyIterable<T> implements Iterable<T> {
311+
public Iterator<T> iterator() {
312+
return new Iterator<T>() {
313+
@Override
314+
public boolean hasNext() {
315+
return false;
316+
}
317+
@Override
318+
public T next() {
319+
return null;
320+
}
321+
@Override
322+
public void remove() {
323+
}
324+
};
325+
}
326+
}
327+
assertTrue($.chain(new MyIterable<Integer>(), 5).isEmpty());
328+
}
329+
302330
@Test
303331
public void optional() {
304332
assertTrue(Optional.absent().equals(Optional.absent()));

string-plugin/src/main/java/com/github/underscore/string/$.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,10 @@ public static <T> Chain<T> chain(final Iterable<T> iterable) {
519519
return new $.Chain<T>(newArrayList(iterable));
520520
}
521521

522+
public static <T> Chain<T> chain(final Iterable<T> iterable, int size) {
523+
return new $.Chain<T>(newArrayList(iterable, size));
524+
}
525+
522526
@SuppressWarnings("unchecked")
523527
public static <T> Chain<T> chain(final T ... list) {
524528
return new $.Chain<T>(Arrays.asList(list));

string-plugin/src/test/java/com/github/underscore/string/StringTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,7 @@ public void main() throws Exception {
19501950
new $.JsonJavaValue();
19511951
new $.JsonJavaObject();
19521952
$.chain(new ArrayList<String>());
1953+
$.chain(new ArrayList<String>(), 1);
19531954
$.chain(new HashSet<String>());
19541955
$.chain(new String[] {});
19551956
}

0 commit comments

Comments
 (0)