Skip to content

Commit 9824602

Browse files
committed
Sync with underscore-java.
1 parent 3877a45 commit 9824602

File tree

6 files changed

+126
-33
lines changed

6 files changed

+126
-33
lines changed

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

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

2765+
public static <T> Chain<T> of(final List<T> list) {
2766+
return new U.Chain<T>(list);
2767+
}
2768+
2769+
public static <T> Chain<T> of(final Iterable<T> iterable) {
2770+
return new U.Chain<T>(newArrayList(iterable));
2771+
}
2772+
2773+
public static <T> Chain<T> of(final Iterable<T> iterable, int size) {
2774+
return new U.Chain<T>(newArrayList(iterable, size));
2775+
}
2776+
2777+
@SuppressWarnings("unchecked")
2778+
public static <T> Chain<T> of(final T ... array) {
2779+
return new U.Chain<T>(Arrays.asList(array));
2780+
}
2781+
2782+
public static Chain<Integer> of(final int[] array) {
2783+
return new U.Chain<Integer>(newIntegerList(array));
2784+
}
2785+
2786+
public Chain<T> of() {
2787+
return new U.Chain<T>(newArrayList(iterable));
2788+
}
2789+
27652790
public static class Chain<T> {
27662791
private final T item;
27672792
private final List<T> list;
@@ -3187,6 +3212,10 @@ public List<T> value() {
31873212
return list;
31883213
}
31893214

3215+
public List<T> toList() {
3216+
return list;
3217+
}
3218+
31903219
public String toString() {
31913220
return String.valueOf(list);
31923221
}

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

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public class U<T> extends com.github.underscore.U<T> {
8282
}
8383

8484
public enum Mode {
85-
REPLACE_SELF_CLOSING_WITH_NULL;
85+
REPLACE_SELF_CLOSING_WITH_NULL,
86+
REPLACE_SELF_CLOSING_WITH_EMPTY;
8687
}
8788

8889
public U(final Iterable<T> iterable) {
@@ -739,6 +740,35 @@ public Chain<T> chain() {
739740
return new U.Chain<T>(newArrayList(value()));
740741
}
741742

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+
742772
public static <T> List<T> drop(final Iterable<T> iterable) {
743773
return rest(newArrayList(iterable));
744774
}
@@ -2226,12 +2256,19 @@ public static String jsonToXml(String json) {
22262256

22272257
@SuppressWarnings("unchecked")
22282258
public static String xmlToJson(String xml, Json.JsonStringBuilder.Step identStep, Mode mode) {
2229-
Object result = Xml.fromXml(xml);
2230-
if (result instanceof Map) {
2231-
return Json.toJson(mode == Mode.REPLACE_SELF_CLOSING_WITH_NULL ?
2232-
replaceSelfClosingWithNull((Map) result) : (Map) result, identStep);
2259+
Object object = Xml.fromXml(xml);
2260+
final String result;
2261+
if (object instanceof Map) {
2262+
if (mode == Mode.REPLACE_SELF_CLOSING_WITH_NULL) {
2263+
result = Json.toJson(replaceSelfClosingWithNull((Map) object), identStep);
2264+
} else if (mode == Mode.REPLACE_SELF_CLOSING_WITH_EMPTY) {
2265+
result = Json.toJson(replaceSelfClosingWithEmpty((Map) object), identStep);
2266+
} else {
2267+
result = Json.toJson((Map) object, identStep);
2268+
}
2269+
return result;
22332270
}
2234-
return Json.toJson((List) result, identStep);
2271+
return Json.toJson((List) object, identStep);
22352272
}
22362273

22372274
public static String xmlToJson(String xml) {
@@ -2331,21 +2368,50 @@ public static boolean isJsonNumber(final String string) {
23312368
return numberEncountered;
23322369
}
23332370

2371+
@SuppressWarnings("unchecked")
23342372
public static Map<String, Object> replaceSelfClosingWithNull(Map<String, Object> map) {
2335-
Map<String, Object> outMap = newLinkedHashMap();
2373+
return (Map<String, Object>) replaceSelfClosingWithValue(map, null);
2374+
}
2375+
2376+
@SuppressWarnings("unchecked")
2377+
public static Map<String, Object> replaceSelfClosingWithEmpty(Map<String, Object> map) {
2378+
return (Map<String, Object>) replaceSelfClosingWithValue(map, "");
2379+
}
2380+
2381+
@SuppressWarnings("unchecked")
2382+
public static Object replaceSelfClosingWithValue(Map<String, Object> map, String value) {
2383+
Object outMap = newLinkedHashMap();
23362384
for (Map.Entry<String, Object> entry : map.entrySet()) {
23372385
if ("-self-closing".equals(entry.getKey()) && "true".equals(entry.getValue())) {
23382386
if (map.size() == 1) {
2339-
outMap = null;
2387+
outMap = value;
23402388
break;
23412389
}
23422390
continue;
23432391
}
2344-
outMap.put(String.valueOf(entry.getKey()), makeObjectSelfClose(entry.getValue()));
2392+
((Map<String, Object>) outMap).put(String.valueOf(entry.getKey()),
2393+
makeObjectSelfClose(entry.getValue(), value));
23452394
}
23462395
return outMap;
23472396
}
23482397

2398+
@SuppressWarnings("unchecked")
2399+
private static Object makeObjectSelfClose(Object value, String newValue) {
2400+
final Object result;
2401+
if (value instanceof List) {
2402+
List<Object> values = newArrayList();
2403+
for (Object item : (List) value) {
2404+
values.add(item instanceof Map ? replaceSelfClosingWithValue((Map) item, newValue) : item);
2405+
}
2406+
result = values;
2407+
} else if (value instanceof Map) {
2408+
result = replaceSelfClosingWithValue((Map) value, newValue);
2409+
} else {
2410+
result = value;
2411+
}
2412+
return result;
2413+
}
2414+
23492415
public static long gcd(long value1, long value2) {
23502416
if (value1 == 0) {
23512417
return value2;
@@ -2361,23 +2427,6 @@ public static long findGcd(long ... array) {
23612427
return result;
23622428
}
23632429

2364-
@SuppressWarnings("unchecked")
2365-
private static Object makeObjectSelfClose(Object value) {
2366-
final Object result;
2367-
if (value instanceof List) {
2368-
List<Object> values = newArrayList();
2369-
for (Object item : (List) value) {
2370-
values.add(item instanceof Map ? replaceSelfClosingWithNull((Map) item) : item);
2371-
}
2372-
result = values;
2373-
} else if (value instanceof Map) {
2374-
result = replaceSelfClosingWithNull((Map) value);
2375-
} else {
2376-
result = value;
2377-
}
2378-
return result;
2379-
}
2380-
23812430
public static Builder objectBuilder() {
23822431
return new U.Builder();
23832432
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,9 +1397,7 @@ private static org.w3c.dom.Document createDocument(final String xml)
13971397
factory.setNamespaceAware(true);
13981398
try {
13991399
factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
1400-
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, "");
1401-
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
1402-
} catch (javax.xml.parsers.ParserConfigurationException ignored) {
1400+
} catch (Exception ignored) {
14031401
// ignored
14041402
}
14051403
final javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
@@ -1415,9 +1413,7 @@ private static org.w3c.dom.Document createDocument() {
14151413
factory.setNamespaceAware(true);
14161414
try {
14171415
factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
1418-
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, "");
1419-
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
1420-
} catch (javax.xml.parsers.ParserConfigurationException ignored) {
1416+
} catch (Exception ignored) {
14211417
// ignored
14221418
}
14231419
final javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public String apply(Map<String, Object> item) {
7777
})
7878
.first().item();
7979
assertEquals("moe is 21", youngest);
80+
U.of(stooges);
8081
}
8182

8283
@Test
@@ -101,6 +102,7 @@ public String apply(Map<String, Object> item) {
101102
})
102103
.first().item();
103104
assertEquals("moe is 21", youngest);
105+
U.of(stooges);
104106
}
105107

106108
@Test
@@ -126,6 +128,7 @@ public String apply(Map<String, Object> item) {
126128
})
127129
.first().item().toString();
128130
assertEquals("moe is 21", youngest);
131+
new U(stooges).of();
129132
}
130133

131134
@Test
@@ -151,6 +154,7 @@ public String apply(Map<String, Object> item) {
151154
.first().item();
152155
assertEquals("moe is 21", youngest);
153156
assertEquals("[1, 2, 3]", U.chain(new int[] {1, 2, 3}).toString());
157+
assertEquals("[1, 2, 3]", U.of(new int[] {1, 2, 3}).toString());
154158
}
155159

156160
/*
@@ -328,6 +332,7 @@ public Map<String, Object> apply(final Map<String, Object> item) {
328332
})
329333
.value().toString();
330334
assertEquals("[{doctorNumber=#9, playedBy=Christopher Eccleston, yearsPlayed=1}]", result);
335+
U.chain(doctors).toList();
331336
}
332337

333338
/*
@@ -409,6 +414,7 @@ public Integer apply(Integer accum, Integer length) {
409414
}
410415
}, 0).item();
411416
assertEquals(34, sum);
417+
U.of(words);
412418
}
413419

414420
@Test

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ public long[] apply(long[] arg) {
390390
}
391391
});
392392
assertEquals(1L, U.chain(iterable, 5).first().item()[0]);
393+
U.of(iterable, 5);
393394
class MyIterable<T> implements Iterable<T> {
394395
public Iterator<T> iterator() {
395396
return new Iterator<T>() {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2019 Valentyn Kolesnikov
4+
* Copyright 2015-2020 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -623,6 +623,11 @@ public void xmlToJson() {
623623
+ " \"#omit-xml-declaration\": \"yes\"\n"
624624
+ "}",
625625
U.xmlToJson("<a/>", U.Mode.REPLACE_SELF_CLOSING_WITH_NULL));
626+
assertEquals("{\n"
627+
+ " \"a\": \"\",\n"
628+
+ " \"#omit-xml-declaration\": \"yes\"\n"
629+
+ "}",
630+
U.xmlToJson("<a/>", U.Mode.REPLACE_SELF_CLOSING_WITH_EMPTY));
626631
assertEquals("{\n"
627632
+ " \"a\": {\n"
628633
+ " \"-b\": \"c\"\n"
@@ -906,13 +911,20 @@ public void main() {
906911
new U(new ArrayList<String>());
907912
new U("");
908913
new U(asList()).chain();
914+
new U(asList()).of();
909915
new Json();
910916
new Xml();
911917
U.chain(new ArrayList<String>());
912918
U.chain(new ArrayList<String>(), 1);
913919
U.chain(new HashSet<String>());
914920
U.chain(new String[] {});
915921
U.chain("");
922+
U.of(new ArrayList<String>());
923+
U.of(new ArrayList<String>(), 1);
924+
U.of(new HashSet<String>());
925+
U.of(new String[] {});
926+
U.of(new int[] {});
927+
U.of("");
916928
}
917929

918930
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)