Skip to content

Commit 715cb00

Browse files
committed
Add support for the concat()
1 parent 27ee5e9 commit 715cb00

File tree

5 files changed

+70
-12
lines changed

5 files changed

+70
-12
lines changed

pom.xml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
<plugin>
138138
<groupId>org.apache.maven.plugins</groupId>
139139
<artifactId>maven-site-plugin</artifactId>
140-
<version>3.0</version>
140+
<version>3.3</version>
141141
<configuration>
142142
<reportPlugins>
143143
<plugin>
@@ -148,17 +148,17 @@
148148
<plugin>
149149
<groupId>org.apache.maven.plugins</groupId>
150150
<artifactId>maven-surefire-report-plugin</artifactId>
151-
<version>2.3</version>
151+
<version>2.18</version>
152152
</plugin>
153153
<plugin>
154154
<groupId>org.apache.maven.plugins</groupId>
155155
<artifactId>maven-jxr-plugin</artifactId>
156-
<version>2.1</version>
156+
<version>2.5</version>
157157
</plugin>
158158
<plugin>
159159
<groupId>org.apache.maven.plugins</groupId>
160160
<artifactId>maven-checkstyle-plugin</artifactId>
161-
<version>2.8</version>
161+
<version>2.13</version>
162162
<configuration>
163163
<configLocation>${basedir}/checkstyle.xml</configLocation>
164164
<includeTestSourceDirectory>true</includeTestSourceDirectory>
@@ -185,10 +185,15 @@
185185
</rulesets>
186186
</configuration>
187187
</plugin>
188+
<plugin>
189+
<groupId>org.codehaus.mojo</groupId>
190+
<artifactId>findbugs-maven-plugin</artifactId>
191+
<version>3.0.0</version>
192+
</plugin>
188193
<plugin>
189194
<groupId>org.codehaus.mojo</groupId>
190195
<artifactId>taglist-maven-plugin</artifactId>
191-
<version>2.0</version>
196+
<version>2.4</version>
192197
</plugin>
193198
</reportPlugins>
194199
</configuration>
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.github.underscore;
22

33
public abstract class Template<T> implements Function1<T, String> {
4-
private final String template;
5-
public Template(final String template) {
6-
this.template = template;
4+
public Template() {
75
}
86

97
}

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,10 @@ public <F extends Comparable<? super F>> Chain<T> sortBy(final Function1<T, F> f
913913
return new Chain<T>(_.sortBy(list, func));
914914
}
915915

916+
public <T> Chain<T> concat(final List<T> second) {
917+
return new Chain<T>((List<T>) Arrays.asList(_.concat(list.toArray(), second.toArray())));
918+
}
919+
916920
public String value() {
917921
return item == null ? list.toString() : item.toString();
918922
}
@@ -943,7 +947,7 @@ public static <E> Object result(final Iterable<E> iterable, final Predicate<E> p
943947
}
944948

945949
public static <E> Template<Set<E>> template(final String template) {
946-
return new Template<Set<E>>(template) {
950+
return new Template<Set<E>>() {
947951
@Override
948952
public String apply(Set<E> value) {
949953
String result = template;
@@ -1026,6 +1030,44 @@ public static <T> String join(final T[] array, final String separator) {
10261030
return join(Arrays.asList(array), separator);
10271031
}
10281032

1033+
public static <T> T[] concat(final T[] first, final T[] second) {
1034+
final T[] result = Arrays.copyOf(first, first.length + second.length);
1035+
System.arraycopy(second, 0, result, first.length, second.length);
1036+
return result;
1037+
}
1038+
1039+
public static <T> List<T> concat(final List<T> first, final List<T> second) {
1040+
return (List<T>) Arrays.asList(_.concat(first.toArray(), second.toArray()));
1041+
}
1042+
1043+
public static <T> T[] concat(final T[] first, final T[] ... other) {
1044+
int length = 0;
1045+
for (T[] otherItem : other) {
1046+
length += otherItem.length;
1047+
}
1048+
final T[] result = Arrays.copyOf(first, first.length + length);
1049+
int index = 0;
1050+
for (T[] otherItem : other) {
1051+
System.arraycopy(otherItem, 0, result, first.length + index, otherItem.length);
1052+
index += otherItem.length;
1053+
}
1054+
return result;
1055+
}
1056+
1057+
public static <T> List<T> concat(final List<T> first, final List<T> ... other) {
1058+
int length = 0;
1059+
for (List<T> otherItem : other) {
1060+
length += otherItem.size();
1061+
}
1062+
final T[] result = (T[]) Arrays.copyOf(first.toArray(), first.size() + length);
1063+
int index = 0;
1064+
for (List<T> otherItem : other) {
1065+
System.arraycopy(otherItem.toArray(), 0, result, first.size() + index, otherItem.size());
1066+
index += otherItem.size();
1067+
}
1068+
return (List<T>) Arrays.asList(result);
1069+
}
1070+
10291071
public static void main(String[] args) {
10301072
final String message = "Underscore-java is a java port of Underscore.js.\n\n"
10311073
+ "In addition to porting Underscore's functionality, Underscore-java includes matching unit tests.\n\n"

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ public Boolean apply(String item) {
13651365

13661366
@Test
13671367
public void debounce() throws Exception {
1368-
final Integer[] counter = new Integer[] {0};
1368+
final Integer[] counter = new Integer[] {0};
13691369
Function<Void> incr = new Function<Void>() { public Void apply() { counter[0]++; return null; }};
13701370
Function<Void> debouncedIncr = _.debounce(incr, 50);
13711371
debouncedIncr.apply();
@@ -1391,7 +1391,7 @@ public void main() throws Exception {
13911391
*/
13921392
@Test
13931393
public void sort() throws Exception {
1394-
assertEquals("[example, some, words]", _.sort(Arrays.asList("some", "words", "example")).toString());
1394+
assertEquals("[example, some, words]", _.sort(asList("some", "words", "example")).toString());
13951395
assertEquals("[example, some, words]", asList(_.sort(new String[] {"some", "words", "example"})).toString());
13961396
}
13971397

@@ -1401,12 +1401,25 @@ public void sort() throws Exception {
14011401
*/
14021402
@Test
14031403
public void join() throws Exception {
1404-
assertEquals("some-words-example", _.join(Arrays.asList("some", "words", "example"), "-"));
1404+
assertEquals("some-words-example", _.join(asList("some", "words", "example"), "-"));
14051405
assertEquals("some-words-example", _.join(new String[] {"some", "words", "example"}, "-"));
14061406
}
14071407

14081408
@Test
14091409
public void compareStrings() throws Exception {
14101410
assertEquals(_.sort("CAT".split("")), _.sort("CTA".split("")));
14111411
}
1412+
1413+
/*
1414+
_.concat([1, 2], [3, 4]);
1415+
=> [1, 2, 3, 4]
1416+
*/
1417+
@Test
1418+
public void concat() throws Exception {
1419+
assertEquals(asList(1, 2, 3, 4), asList(_.concat(new Integer[] {1, 2}, new Integer[] {3, 4})));
1420+
assertEquals(asList(1, 2, 3, 4), _.concat(asList(1, 2), asList(3, 4)));
1421+
assertEquals("[1, 2, 3, 4]", _.chain(asList(1, 2)).concat(asList(3, 4)).value());
1422+
assertEquals(asList(1, 2, 3, 4), asList(_.concat(new Integer[] {1, 2}, new Integer[] {3}, new Integer[] {4})));
1423+
assertEquals(asList(1, 2, 3, 4), _.concat(asList(1, 2), asList(3), asList(4)));
1424+
}
14121425
}

underscore.jar

679 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)