Skip to content

Commit 07736ea

Browse files
terhesbjavadev
authored andcommitted
Add U.replace(iterable, predicate, value) and U.replaceIndexed() methods.
1 parent 229b813 commit 07736ea

File tree

2 files changed

+128
-5
lines changed

2 files changed

+128
-5
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,44 @@ public static <T, E> List<T> mapIndexed(final List<E> list, final BiFunction<Int
324324
return transformed;
325325
}
326326

327+
public static <T> List<T> replace(final Iterable<T> iter, final Predicate<T> pred, final T value) {
328+
List<T> list = newArrayList(iter);
329+
if (pred == null) {
330+
return list;
331+
}
332+
ListIterator<T> itera = list.listIterator();
333+
while (itera.hasNext()) {
334+
if (pred.test(itera.next())) {
335+
itera.set(value);
336+
}
337+
}
338+
return list;
339+
}
340+
341+
public List<T> replace(final Predicate<T> pred, final T value) {
342+
return replace(value(), pred, value);
343+
}
344+
345+
public static <T> List<T> replaceIndexed(final Iterable<T> iter, final PredicateIndexed<T> pred, final T value) {
346+
List<T> list = newArrayList(iter);
347+
if (pred == null) {
348+
return list;
349+
}
350+
ListIterator<T> itera = list.listIterator();
351+
int index = 0;
352+
while (itera.hasNext()) {
353+
if (pred.test(index, itera.next())) {
354+
itera.set(value);
355+
}
356+
index++;
357+
}
358+
return list;
359+
}
360+
361+
public List<T> replaceIndexed(final PredicateIndexed<T> pred, final T value) {
362+
return replaceIndexed(value(), pred, value);
363+
}
364+
327365
public <F> List<F> mapIndexed(final BiFunction<Integer, ? super T, F> func) {
328366
return mapIndexed(newArrayList(iterable), func);
329367
}
@@ -2709,6 +2747,14 @@ public <F> Chain<F> mapIndexed(final BiFunction<Integer, ? super T, F> func) {
27092747
return new Chain<F>(U.mapIndexed(list, func));
27102748
}
27112749

2750+
public Chain<T> replace(final Predicate<T> pred, final T value) {
2751+
return new Chain<T>(U.replace(list, pred, value));
2752+
}
2753+
2754+
public Chain<T> replaceIndexed(final PredicateIndexed<T> pred, final T value) {
2755+
return new Chain<T>(U.replaceIndexed(list, pred, value));
2756+
}
2757+
27122758
public Chain<T> filter(final Predicate<T> pred) {
27132759
return new Chain<T>(U.filter(list, pred));
27142760
}

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

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import org.junit.Test;
26+
import static java.util.Arrays.asList;
27+
import static org.junit.Assert.assertArrayEquals;
28+
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertNull;
2730

2831
import java.util.ArrayList;
2932
import java.util.Arrays;
3033
import java.util.Collection;
3134
import java.util.Collections;
35+
import java.util.HashSet;
3236
import java.util.List;
3337
import java.util.NoSuchElementException;
38+
import java.util.Set;
3439

35-
import static java.util.Arrays.asList;
36-
import static org.junit.Assert.assertArrayEquals;
37-
import static org.junit.Assert.assertEquals;
38-
import static org.junit.Assert.assertNull;
40+
import org.junit.Test;
3941

4042
/**
4143
* Underscore library unit test.
@@ -464,6 +466,81 @@ public void drop() {
464466
assertEquals("[3, 2, 1]", asList(resultArray2).toString());
465467
}
466468

469+
/*
470+
_.replace([1, 2, 3, 4], predicate(a) { return a > 2; }, 100);
471+
=> [1, 2, 100, 100]
472+
_.replace([1, 2, 3, 4], null, 100);
473+
=> [1, 2, 3, 4]
474+
*/
475+
@SuppressWarnings("serial")
476+
@Test
477+
public void replace() {
478+
assertEquals("[100, 1, 100, 3, 100, 5]", U.replace(U.newIntegerList(U.range(6)),
479+
new Predicate<Integer>() {
480+
@Override
481+
public boolean test(Integer arg) {
482+
return arg % 2 == 0;
483+
}
484+
}, 100).toString());
485+
assertEquals("[0, 1, 2, 3, 4]", U.replace(U.newIntegerList(U.range(5)), null, 100).toString());
486+
assertEquals("[a, aa, b, b]", new U<String>(asList("a", "aa", "aaa", "aaaa")).replace(
487+
new Predicate<String>() {
488+
@Override
489+
public boolean test(String arg) {
490+
return arg.length() > 2;
491+
}
492+
}, "b").toString());
493+
assertEquals("[a, aa, cc, ccc]", new U<String>(asList("a", "aa", "cc", "ccc")).replace(
494+
null, "b").toString());
495+
Set<Integer> set = new HashSet<Integer>() { {
496+
addAll(U.newIntegerList(U.range(7)));
497+
} };
498+
assertEquals("[0, 1, 2, 100, 100, 100, 100]", U.chain(set).replace(
499+
new Predicate<Integer>() {
500+
@Override
501+
public boolean test(Integer arg) {
502+
return arg > 2;
503+
}
504+
}, 100).toString());
505+
}
506+
507+
/*
508+
_.replaceIndexed([a, b, c, d], predicateIndexed(a, b) { return a > 2; }, z);
509+
=> [a, b, z, z]
510+
_.replaceIndexed([a, b, c, d], null, z);
511+
=> [a, b, c, d]
512+
*/
513+
@SuppressWarnings("serial")
514+
@Test
515+
public void replaceIndexed() {
516+
assertEquals("[0, 1, 2, 3, 100, 100]", U.replaceIndexed(U.newIntegerList(U.range(6)),
517+
new PredicateIndexed<Integer>() {
518+
@Override
519+
public boolean test(int i, Integer arg) {
520+
return i > 2 && arg > 3;
521+
}
522+
}, 100).toString());
523+
assertEquals("[0, 1, 2, 3, 4]", U.replaceIndexed(U.newIntegerList(U.range(5)), null, 100).toString());
524+
assertEquals("[a, bc, ddd, f]", new U<String>(asList("a", "bc", "ddd", "eeee")).replaceIndexed(
525+
new PredicateIndexed<String>() {
526+
@Override
527+
public boolean test(int i, String arg) {
528+
return arg.length() > 2 && i > 2;
529+
}
530+
}, "f").toString());
531+
assertEquals("[a, aa, cc, ccc]", new U<String>(asList("a", "aa", "cc", "ccc")).replaceIndexed(
532+
null, "b").toString());
533+
List<Integer> list = new ArrayList<Integer>() { {
534+
add(100); add(22); add(88); add(6530); add(-25); add(-1000);
535+
} };
536+
assertEquals("[100, 0, 88, 6530, 0, -1000]", U.chain(list).replaceIndexed(
537+
new PredicateIndexed<Integer>() {
538+
@Override
539+
public boolean test(int i, Integer arg) {
540+
return arg < 23 && i < 5;
541+
}
542+
}, 0).toString());
543+
}
467544

468545
/*
469546
_.initial([5, 4, 3, 2, 1]);

0 commit comments

Comments
 (0)