Skip to content

Commit

Permalink
Add methods last, initial, rest to the chain().
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed May 1, 2015
1 parent 4fddd71 commit 3a77505
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add this dependency to your project's POM:
<dependency>
<groupId>com.github.javadev</groupId>
<artifactId>underscore</artifactId>
<version>1.1</version>
<version>1.2</version>
</dependency>

Underscore-java is a java port of [Underscore.js](http://underscorejs.org/).
Expand Down
2 changes: 1 addition & 1 deletion pom-central.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.javadev</groupId>
<artifactId>underscore</artifactId>
<packaging>jar</packaging>
<version>1.1</version>
<version>1.2</version>
<name>java port of Underscore.js</name>
<description>The java port of Underscore.js</description>
<url>https://github.com/javadev/underscore-java</url>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.javadev</groupId>
<artifactId>underscore</artifactId>
<packaging>jar</packaging>
<version>1.1</version>
<version>1.2</version>
<name>java port of Underscore.js</name>
<description>The java port of Underscore.js</description>
<url>https://github.com/javadev/underscore-java</url>
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/com/github/underscore/_.java
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,14 @@ public static <T> Chain chain(final List<T> list) {
return new _.Chain<T>(list);
}

public static <T> Chain chain(final Set<T> list) {
return new _.Chain<T>(new ArrayList<T>(list));
}

public static <T> Chain chain(final T[] list) {
return new _.Chain<T>(Arrays.asList(list));
}

public static class Chain<T> {
private final T item;
private final List<T> list;
Expand All @@ -869,6 +877,26 @@ public Chain<T> first() {
return new Chain<T>(_.first(list));
}

public Chain<T> initial() {
return new Chain<T>(_.initial(list));
}

public Chain<T> initial(int n) {
return new Chain<T>(_.initial(list, n));
}

public Chain<T> last() {
return new Chain<T>(_.last(list));
}

public Chain<T> rest() {
return new Chain<T>(_.rest(list));
}

public Chain<T> rest(int n) {
return new Chain<T>(_.rest(list, n));
}

public Chain<T> flatten() {
List<T> flattened = new ArrayList<T>();
flatten(list, flattened);
Expand Down Expand Up @@ -1042,16 +1070,6 @@ public static <T> String join(final T[] array, final String separator) {
return join(Arrays.asList(array), separator);
}

public static <T> T[] concat(final T[] first, final T[] second) {
final T[] result = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}

public static <T> List<T> concat(final List<T> first, final List<T> second) {
return (List<T>) Arrays.asList(_.concat(first.toArray(), second.toArray()));
}

public static <T> T[] concat(final T[] first, final T[] ... other) {
int length = 0;
for (T[] otherItem : other) {
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/github/underscore/_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ public String toString() {
public void first() {
final Integer result = _.first(asList(5, 4, 3, 2, 1));
assertEquals("5", result.toString());
final Object resultChain = _.chain(asList(5, 4, 3, 2, 1)).first().item();
assertEquals("5", resultChain.toString());
final List<Integer> resultList = _.first(asList(5, 4, 3, 2, 1), 2);
assertEquals("[5, 4]", resultList.toString());
final int resultInt = _.first(new Integer[] {5, 4, 3, 2, 1});
Expand Down Expand Up @@ -547,8 +549,12 @@ public void head() {
public void initial() {
final List<Integer> result = _.initial(asList(5, 4, 3, 2, 1));
assertEquals("[5, 4, 3, 2]", result.toString());
final List<Integer> resultChain = _.chain(asList(5, 4, 3, 2, 1)).initial().value();
assertEquals("[5, 4, 3, 2]", resultChain.toString());
final List<Integer> resultList = _.initial(asList(5, 4, 3, 2, 1), 2);
assertEquals("[5, 4, 3]", resultList.toString());
final List<Integer> resultListChain = _.chain(asList(5, 4, 3, 2, 1)).initial(2).value();
assertEquals("[5, 4, 3]", resultListChain.toString());
final Integer[] resultArray = _.initial(new Integer[] {5, 4, 3, 2, 1});
assertEquals("[5, 4, 3, 2]", asList(resultArray).toString());
final Integer[] resultListArray = _.initial(new Integer[] {5, 4, 3, 2, 1}, 2);
Expand All @@ -563,6 +569,8 @@ public void initial() {
public void last() {
final Integer result = _.last(asList(5, 4, 3, 2, 1));
assertEquals("1", result.toString());
final Object resultChain = _.chain(asList(5, 4, 3, 2, 1)).last().item();
assertEquals("1", resultChain.toString());
final Integer resultArray = _.last(new Integer[] {5, 4, 3, 2, 1});
assertEquals("1", resultArray.toString());
}
Expand Down Expand Up @@ -617,8 +625,12 @@ public void drop() {
public void rest() {
final List<Integer> result = _.rest(asList(5, 4, 3, 2, 1));
assertEquals("[4, 3, 2, 1]", result.toString());
final List<Integer> resultChain = _.chain(asList(5, 4, 3, 2, 1)).rest().value();
assertEquals("[4, 3, 2, 1]", resultChain.toString());
final List<Integer> result2 = _.rest(asList(5, 4, 3, 2, 1), 2);
assertEquals("[3, 2, 1]", result2.toString());
final List<Integer> result2Chain = _.chain(asList(5, 4, 3, 2, 1)).rest(2).value();
assertEquals("[3, 2, 1]", result2Chain.toString());
final Object[] resultArray = _.rest(new Integer[] {5, 4, 3, 2, 1});
assertEquals("[4, 3, 2, 1]", asList(resultArray).toString());
final Object[] resultArray2 = _.rest(new Integer[] {5, 4, 3, 2, 1}, 2);
Expand Down Expand Up @@ -1128,6 +1140,54 @@ public String apply(Map<String, Object> item) {
assertEquals("moe is 21", youngest);
}

@Test
public void chainSet() throws Exception {
final Set<Map<String, Object>> stooges = new HashSet<Map<String, Object>>() {{
add(new LinkedHashMap<String, Object>() {{ put("name", "curly"); put("age", 25); }});
add(new LinkedHashMap<String, Object>() {{ put("name", "moe"); put("age", 21); }});
add(new LinkedHashMap<String, Object>() {{ put("name", "larry"); put("age", 23); }});
}};
final String youngest = _.chain(stooges)
.sortBy(
new Function1<Map<String, Object>, String>() {
public String apply(Map<String, Object> item) {
return item.get("age").toString();
}
})
.map(
new Function1<Map<String, Object>, String>() {
public String apply(Map<String, Object> item) {
return item.get("name") + " is " + item.get("age");
}
})
.first().item().toString();
assertEquals("moe is 21", youngest);
}

@Test
public void chainArray() throws Exception {
final List<Map<String, Object>> stooges = new ArrayList<Map<String, Object>>() {{
add(new LinkedHashMap<String, Object>() {{ put("name", "curly"); put("age", 25); }});
add(new LinkedHashMap<String, Object>() {{ put("name", "moe"); put("age", 21); }});
add(new LinkedHashMap<String, Object>() {{ put("name", "larry"); put("age", 23); }});
}};
final String youngest = _.chain(stooges.toArray())
.sortBy(
new Function1<Map<String, Object>, String>() {
public String apply(Map<String, Object> item) {
return item.get("age").toString();
}
})
.map(
new Function1<Map<String, Object>, String>() {
public String apply(Map<String, Object> item) {
return item.get("name") + " is " + item.get("age");
}
})
.first().item().toString();
assertEquals("moe is 21", youngest);
}

/*
var lyrics = [
{line: 1, words: "I'm a lumberjack and I'm okay"},
Expand Down
Binary file added underscore-1.2.jar
Binary file not shown.

0 comments on commit 3a77505

Please sign in to comment.