Skip to content

Commit e7d77d9

Browse files
committed
Add U.of(...) and toList() methods.
1 parent 7e0dbd2 commit e7d77d9

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2754,6 +2754,31 @@ public Chain<T> chain() {
27542754
return new U.Chain<T>(newArrayList(iterable));
27552755
}
27562756

2757+
public static <T> Chain<T> of(final List<T> list) {
2758+
return new U.Chain<T>(list);
2759+
}
2760+
2761+
public static <T> Chain<T> of(final Iterable<T> iterable) {
2762+
return new U.Chain<T>(newArrayList(iterable));
2763+
}
2764+
2765+
public static <T> Chain<T> of(final Iterable<T> iterable, int size) {
2766+
return new U.Chain<T>(newArrayList(iterable, size));
2767+
}
2768+
2769+
@SuppressWarnings("unchecked")
2770+
public static <T> Chain<T> of(final T ... array) {
2771+
return new U.Chain<T>(Arrays.asList(array));
2772+
}
2773+
2774+
public static Chain<Integer> of(final int[] array) {
2775+
return new U.Chain<Integer>(newIntegerList(array));
2776+
}
2777+
2778+
public Chain<T> of() {
2779+
return new U.Chain<T>(newArrayList(iterable));
2780+
}
2781+
27572782
public static class Chain<T> {
27582783
private final T item;
27592784
private final List<T> list;
@@ -3179,6 +3204,10 @@ public List<T> value() {
31793204
return list;
31803205
}
31813206

3207+
public List<T> toList() {
3208+
return list;
3209+
}
3210+
31823211
public String toString() {
31833212
return String.valueOf(list);
31843213
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,35 @@ public Chain<T> chain() {
740740
return new U.Chain<T>(newArrayList(value()));
741741
}
742742

743+
public static Chain<String> of(final String item) {
744+
return new U.Chain<String>(item);
745+
}
746+
747+
public static <T> Chain<T> of(final List<T> list) {
748+
return new U.Chain<T>(list);
749+
}
750+
751+
public static <T> Chain<T> of(final Iterable<T> iterable) {
752+
return new U.Chain<T>(newArrayList(iterable));
753+
}
754+
755+
public static <T> Chain<T> of(final Iterable<T> iterable, int size) {
756+
return new U.Chain<T>(newArrayList(iterable, size));
757+
}
758+
759+
@SuppressWarnings("unchecked")
760+
public static <T> Chain<T> of(final T ... list) {
761+
return new U.Chain<T>(Arrays.asList(list));
762+
}
763+
764+
public static Chain<Integer> of(final int[] array) {
765+
return new U.Chain<Integer>(newIntegerList(array));
766+
}
767+
768+
public Chain<T> of() {
769+
return new U.Chain<T>(newArrayList(value()));
770+
}
771+
743772
public static <T> List<T> drop(final Iterable<T> iterable) {
744773
return rest(newArrayList(iterable));
745774
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public String apply(Map<String, Object> item) {
7474
})
7575
.first().item();
7676
assertEquals("moe is 21", youngest);
77+
U.of(stooges);
7778
}
7879

7980
@Test
@@ -98,6 +99,7 @@ public String apply(Map<String, Object> item) {
9899
})
99100
.first().item();
100101
assertEquals("moe is 21", youngest);
102+
U.of(stooges);
101103
}
102104

103105
@Test
@@ -123,6 +125,7 @@ public String apply(Map<String, Object> item) {
123125
})
124126
.first().item().toString();
125127
assertEquals("moe is 21", youngest);
128+
new U(stooges).of();
126129
}
127130

128131
@Test
@@ -148,6 +151,7 @@ public String apply(Map<String, Object> item) {
148151
.first().item();
149152
assertEquals("moe is 21", youngest);
150153
assertEquals("[1, 2, 3]", U.chain(new int[] {1, 2, 3}).toString());
154+
assertEquals("[1, 2, 3]", U.of(new int[] {1, 2, 3}).toString());
151155
}
152156

153157
/*
@@ -325,6 +329,7 @@ public Map<String, Object> apply(final Map<String, Object> item) {
325329
})
326330
.value().toString();
327331
assertEquals("[{doctorNumber=#9, playedBy=Christopher Eccleston, yearsPlayed=1}]", result);
332+
U.chain(doctors).toList();
328333
}
329334

330335
/*
@@ -406,6 +411,7 @@ public Integer apply(Integer accum, Integer length) {
406411
}
407412
}, 0).item();
408413
assertEquals(34, sum);
414+
U.of(words);
409415
}
410416

411417
@Test

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ public long[] apply(long[] arg) {
386386
}
387387
});
388388
assertEquals(1L, U.chain(iterable, 5).first().item()[0]);
389+
U.of(iterable, 5);
389390
class MyIterable<T> implements Iterable<T> {
390391
public Iterator<T> iterator() {
391392
return new Iterator<T>() {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,13 +906,20 @@ public void main() {
906906
new U(new ArrayList<String>());
907907
new U("");
908908
new U(asList()).chain();
909+
new U(asList()).of();
909910
new Json();
910911
new Xml();
911912
U.chain(new ArrayList<String>());
912913
U.chain(new ArrayList<String>(), 1);
913914
U.chain(new HashSet<String>());
914915
U.chain(new String[] {});
915916
U.chain("");
917+
U.of(new ArrayList<String>());
918+
U.of(new ArrayList<String>(), 1);
919+
U.of(new HashSet<String>());
920+
U.of(new String[] {});
921+
U.of(new int[] {});
922+
U.of("");
916923
}
917924

918925
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)