Skip to content

Commit e4dd58f

Browse files
committed
Add support for the xor().
1 parent e702ce7 commit e4dd58f

File tree

2 files changed

+30
-0
lines changed
  • lodash-plugin/src

2 files changed

+30
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public Chain<List<Object>> pull(Object ... values) {
9191
public Chain<List<Object>> pullAt(Integer ... indexes) {
9292
return new Chain<List<Object>>($.pullAt((List<Object>) value(), indexes));
9393
}
94+
95+
public Chain<List<T>> xor(final List<T> list) {
96+
return new Chain<List<T>>($.xor(value(), list));
97+
}
9498
}
9599

96100
public static Chain chain(final String item) {
@@ -236,6 +240,21 @@ public List<Object> pullAt(Integer ... indexes) {
236240
return pullAt((List<Object>) getIterable(), indexes);
237241
}
238242

243+
public static <T> List<T> xor(final List<T> ... lists) {
244+
int index = -1;
245+
int length = lists.length;
246+
List<T> result = null;
247+
while (++index < length) {
248+
final List<T> array = lists[index];
249+
result = result == null ? array : concat(difference(result, array), difference(array, result));
250+
}
251+
return uniq(result);
252+
}
253+
254+
public List<T> xor(final List<T> list) {
255+
return xor((List<T>) getIterable(), list);
256+
}
257+
239258
public static void main(String ... args) {
240259
final String message = "Underscore-java-lodash is a lodash plugin for underscore-java.\n\n"
241260
+ "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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ public void pullAt() {
247247
assertEquals("[10, 20]", events.toString());
248248
}
249249

250+
/*
251+
_.xor([1, 2], [4, 2]);
252+
// → [1, 4]
253+
*/
254+
@Test
255+
public void xor() {
256+
assertEquals("[1, 4]", $.xor(asList(1, 2), asList(4, 2)).toString());
257+
assertEquals("[1, 4]", new $(asList(1, 2)).xor(asList(4, 2)).toString());
258+
assertEquals("[1, 4]", $.chain(asList(1, 2)).xor(asList(4, 2)).toString());
259+
}
260+
250261
@Test
251262
public void main() {
252263
$.main(new String[] {});

0 commit comments

Comments
 (0)