Skip to content

Commit 0e83536

Browse files
committed
Add support for the takeWhile(), takeRightWile() and at().
1 parent 00ffd4b commit 0e83536

File tree

2 files changed

+116
-9
lines changed
  • lodash-plugin/src

2 files changed

+116
-9
lines changed

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,21 @@ public Chain<List<T>> takeRight(final Integer n) {
112112
return new Chain<List<T>>($.takeRight(value(), n));
113113
}
114114

115+
public Chain<List<T>> takeWhile(final Predicate<T> pred) {
116+
return new Chain<List<T>>($.takeWhile(value(), pred));
117+
}
118+
119+
public Chain<List<T>> takeRightWhile(final Predicate<T> pred) {
120+
return new Chain<List<T>>($.takeRightWhile(value(), pred));
121+
}
122+
115123
public Chain<List<T>> xor(final List<T> list) {
116124
return new Chain<List<T>>($.xor(value(), list));
117125
}
126+
127+
public Chain<List<T>> at(final Integer ... indexes) {
128+
return new Chain<List<T>>($.at(value(), indexes));
129+
}
118130
}
119131

120132
public static Chain chain(final String item) {
@@ -189,7 +201,7 @@ public List<T> dropWhile(final Predicate<T> pred) {
189201
}
190202

191203
public static <T> List<T> dropRightWhile(final Iterable<T> iterable, final Predicate<T> pred) {
192-
return dropWhile(reverse(iterable), pred);
204+
return reverse(dropWhile(reverse(iterable), pred));
193205
}
194206

195207
public List<T> dropRightWhile(final Predicate<T> pred) {
@@ -308,6 +320,22 @@ public List<T> takeRight(final Integer n) {
308320
return takeRight(getIterable(), n);
309321
}
310322

323+
public static <T> List<T> takeWhile(final Iterable<T> iterable, final Predicate<T> pred) {
324+
return first(newArrayList(iterable), findIndex(newArrayList(iterable), negate(pred)));
325+
}
326+
327+
public List<T> takeWhile(final Predicate<T> pred) {
328+
return takeWhile(getIterable(), pred);
329+
}
330+
331+
public static <T> List<T> takeRightWhile(final Iterable<T> iterable, final Predicate<T> pred) {
332+
return reverse(takeWhile(reverse(iterable), pred));
333+
}
334+
335+
public List<T> takeRightWhile(final Predicate<T> pred) {
336+
return takeRightWhile(getIterable(), pred);
337+
}
338+
311339
public static <T> List<T> xor(final List<T> ... lists) {
312340
int index = -1;
313341
int length = lists.length;
@@ -323,6 +351,24 @@ public List<T> xor(final List<T> list) {
323351
return xor((List<T>) getIterable(), list);
324352
}
325353

354+
public static <T> List<T> at(final List<T> list, final Integer ... indexes) {
355+
final List<T> result = newArrayList();
356+
final List<Integer> indexesList = Arrays.asList(indexes);
357+
int index = 0;
358+
for (final Iterator<T> iterator = list.iterator(); iterator.hasNext(); ) {
359+
final T object = iterator.next();
360+
if (indexesList.contains(index)) {
361+
result.add(object);
362+
}
363+
index += 1;
364+
}
365+
return result;
366+
}
367+
368+
public List<T> at(final Integer ... indexes) {
369+
return at((List<T>) getIterable(), indexes);
370+
}
371+
326372
public static void main(String ... args) {
327373
final String message = "Underscore-java-lodash is a lodash plugin for underscore-java.\n\n"
328374
+ "For docs, license, tests, and downloads, see: http://javadev.github.io/underscore-java";

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

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,25 @@ public Boolean apply(Integer n) {
128128

129129
/*
130130
_.dropRightWhile([1, 2, 3], function(n) {
131-
return n > 1;
131+
return n > 2;
132132
});
133-
// → [1]
133+
// → [1, 2]
134134
*/
135135
@Test
136136
public void dropRightWhile() {
137-
assertEquals("[1]", $.dropRightWhile(asList(1, 2, 3), new Predicate<Integer>() {
137+
assertEquals("[1, 2]", $.dropRightWhile(asList(1, 2, 3), new Predicate<Integer>() {
138138
public Boolean apply(Integer n) {
139-
return n > 1;
139+
return n > 2;
140140
}
141141
}).toString());
142-
assertEquals("[1]", new $(asList(1, 2, 3)).dropRightWhile(new Predicate<Integer>() {
142+
assertEquals("[1, 2]", new $(asList(1, 2, 3)).dropRightWhile(new Predicate<Integer>() {
143143
public Boolean apply(Integer n) {
144-
return n > 1;
144+
return n > 2;
145145
}
146146
}).toString());
147-
assertEquals("[1]", $.chain(asList(1, 2, 3)).dropRightWhile(new Predicate<Integer>() {
147+
assertEquals("[1, 2]", $.chain(asList(1, 2, 3)).dropRightWhile(new Predicate<Integer>() {
148148
public Boolean apply(Integer n) {
149-
return n > 1;
149+
return n > 2;
150150
}
151151
}).toString());
152152
}
@@ -337,6 +337,56 @@ public void takeRight() {
337337
assertEquals("[]", $.takeRight(asList(1, 2, 3), 0).toString());
338338
}
339339

340+
/*
341+
_.takeWhile([1, 2, 3], function(n) {
342+
return n < 3;
343+
});
344+
// → [1, 2]
345+
*/
346+
@Test
347+
public void takeWhile() {
348+
assertEquals("[1, 2]", $.takeWhile(asList(1, 2, 3), new Predicate<Integer>() {
349+
public Boolean apply(Integer n) {
350+
return n < 3;
351+
}
352+
}).toString());
353+
assertEquals("[1, 2]", new $(asList(1, 2, 3)).takeWhile(new Predicate<Integer>() {
354+
public Boolean apply(Integer n) {
355+
return n < 3;
356+
}
357+
}).toString());
358+
assertEquals("[1, 2]", $.chain(asList(1, 2, 3)).takeWhile(new Predicate<Integer>() {
359+
public Boolean apply(Integer n) {
360+
return n < 3;
361+
}
362+
}).toString());
363+
}
364+
365+
/*
366+
_.takeRightWhile([1, 2, 3], function(n) {
367+
return n > 1;
368+
});
369+
// → [2, 3]
370+
*/
371+
@Test
372+
public void takeRightWhile() {
373+
assertEquals("[2, 3]", $.takeRightWhile(asList(1, 2, 3), new Predicate<Integer>() {
374+
public Boolean apply(Integer n) {
375+
return n > 1;
376+
}
377+
}).toString());
378+
assertEquals("[2, 3]", new $(asList(1, 2, 3)).takeRightWhile(new Predicate<Integer>() {
379+
public Boolean apply(Integer n) {
380+
return n > 1;
381+
}
382+
}).toString());
383+
assertEquals("[2, 3]", $.chain(asList(1, 2, 3)).takeRightWhile(new Predicate<Integer>() {
384+
public Boolean apply(Integer n) {
385+
return n > 1;
386+
}
387+
}).toString());
388+
}
389+
340390
/*
341391
_.xor([1, 2], [4, 2]);
342392
// → [1, 4]
@@ -349,6 +399,17 @@ public void xor() {
349399
}
350400

351401

402+
/*
403+
_.at(['a', 'b', 'c'], 0, 2);
404+
// → ['a', 'c']
405+
*/
406+
@Test
407+
public void at() {
408+
assertEquals("[a, c]", $.at(asList("a", "b", "c"), 0, 2).toString());
409+
assertEquals("[a, c]", new $(asList("a", "b", "c")).at(0, 2).toString());
410+
assertEquals("[a, c]", $.chain(asList("a", "b", "c")).at(0, 2).value().toString());
411+
}
412+
352413
@Test
353414
public void main() {
354415
$.main(new String[] {});

0 commit comments

Comments
 (0)