Skip to content

Commit

Permalink
Sync with underscore-java.
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed Oct 1, 2020
1 parent 3877a45 commit 9824602
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 33 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/github/underscore/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -2762,6 +2762,31 @@ public Chain<T> chain() {
return new U.Chain<T>(newArrayList(iterable));
}

public static <T> Chain<T> of(final List<T> list) {
return new U.Chain<T>(list);
}

public static <T> Chain<T> of(final Iterable<T> iterable) {
return new U.Chain<T>(newArrayList(iterable));
}

public static <T> Chain<T> of(final Iterable<T> iterable, int size) {
return new U.Chain<T>(newArrayList(iterable, size));
}

@SuppressWarnings("unchecked")
public static <T> Chain<T> of(final T ... array) {
return new U.Chain<T>(Arrays.asList(array));
}

public static Chain<Integer> of(final int[] array) {
return new U.Chain<Integer>(newIntegerList(array));
}

public Chain<T> of() {
return new U.Chain<T>(newArrayList(iterable));
}

public static class Chain<T> {
private final T item;
private final List<T> list;
Expand Down Expand Up @@ -3187,6 +3212,10 @@ public List<T> value() {
return list;
}

public List<T> toList() {
return list;
}

public String toString() {
return String.valueOf(list);
}
Expand Down
101 changes: 75 additions & 26 deletions src/main/java/com/github/underscore/lodash/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public class U<T> extends com.github.underscore.U<T> {
}

public enum Mode {
REPLACE_SELF_CLOSING_WITH_NULL;
REPLACE_SELF_CLOSING_WITH_NULL,
REPLACE_SELF_CLOSING_WITH_EMPTY;
}

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

public static Chain<String> of(final String item) {
return new U.Chain<String>(item);
}

public static <T> Chain<T> of(final List<T> list) {
return new U.Chain<T>(list);
}

public static <T> Chain<T> of(final Iterable<T> iterable) {
return new U.Chain<T>(newArrayList(iterable));
}

public static <T> Chain<T> of(final Iterable<T> iterable, int size) {
return new U.Chain<T>(newArrayList(iterable, size));
}

@SuppressWarnings("unchecked")
public static <T> Chain<T> of(final T ... list) {
return new U.Chain<T>(Arrays.asList(list));
}

public static Chain<Integer> of(final int[] array) {
return new U.Chain<Integer>(newIntegerList(array));
}

public Chain<T> of() {
return new U.Chain<T>(newArrayList(value()));
}

public static <T> List<T> drop(final Iterable<T> iterable) {
return rest(newArrayList(iterable));
}
Expand Down Expand Up @@ -2226,12 +2256,19 @@ public static String jsonToXml(String json) {

@SuppressWarnings("unchecked")
public static String xmlToJson(String xml, Json.JsonStringBuilder.Step identStep, Mode mode) {
Object result = Xml.fromXml(xml);
if (result instanceof Map) {
return Json.toJson(mode == Mode.REPLACE_SELF_CLOSING_WITH_NULL ?
replaceSelfClosingWithNull((Map) result) : (Map) result, identStep);
Object object = Xml.fromXml(xml);
final String result;
if (object instanceof Map) {
if (mode == Mode.REPLACE_SELF_CLOSING_WITH_NULL) {
result = Json.toJson(replaceSelfClosingWithNull((Map) object), identStep);
} else if (mode == Mode.REPLACE_SELF_CLOSING_WITH_EMPTY) {
result = Json.toJson(replaceSelfClosingWithEmpty((Map) object), identStep);
} else {
result = Json.toJson((Map) object, identStep);
}
return result;
}
return Json.toJson((List) result, identStep);
return Json.toJson((List) object, identStep);
}

public static String xmlToJson(String xml) {
Expand Down Expand Up @@ -2331,21 +2368,50 @@ public static boolean isJsonNumber(final String string) {
return numberEncountered;
}

@SuppressWarnings("unchecked")
public static Map<String, Object> replaceSelfClosingWithNull(Map<String, Object> map) {
Map<String, Object> outMap = newLinkedHashMap();
return (Map<String, Object>) replaceSelfClosingWithValue(map, null);
}

@SuppressWarnings("unchecked")
public static Map<String, Object> replaceSelfClosingWithEmpty(Map<String, Object> map) {
return (Map<String, Object>) replaceSelfClosingWithValue(map, "");
}

@SuppressWarnings("unchecked")
public static Object replaceSelfClosingWithValue(Map<String, Object> map, String value) {
Object outMap = newLinkedHashMap();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if ("-self-closing".equals(entry.getKey()) && "true".equals(entry.getValue())) {
if (map.size() == 1) {
outMap = null;
outMap = value;
break;
}
continue;
}
outMap.put(String.valueOf(entry.getKey()), makeObjectSelfClose(entry.getValue()));
((Map<String, Object>) outMap).put(String.valueOf(entry.getKey()),
makeObjectSelfClose(entry.getValue(), value));
}
return outMap;
}

@SuppressWarnings("unchecked")
private static Object makeObjectSelfClose(Object value, String newValue) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
for (Object item : (List) value) {
values.add(item instanceof Map ? replaceSelfClosingWithValue((Map) item, newValue) : item);
}
result = values;
} else if (value instanceof Map) {
result = replaceSelfClosingWithValue((Map) value, newValue);
} else {
result = value;
}
return result;
}

public static long gcd(long value1, long value2) {
if (value1 == 0) {
return value2;
Expand All @@ -2361,23 +2427,6 @@ public static long findGcd(long ... array) {
return result;
}

@SuppressWarnings("unchecked")
private static Object makeObjectSelfClose(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
for (Object item : (List) value) {
values.add(item instanceof Map ? replaceSelfClosingWithNull((Map) item) : item);
}
result = values;
} else if (value instanceof Map) {
result = replaceSelfClosingWithNull((Map) value);
} else {
result = value;
}
return result;
}

public static Builder objectBuilder() {
return new U.Builder();
}
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/com/github/underscore/lodash/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -1397,9 +1397,7 @@ private static org.w3c.dom.Document createDocument(final String xml)
factory.setNamespaceAware(true);
try {
factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
} catch (javax.xml.parsers.ParserConfigurationException ignored) {
} catch (Exception ignored) {
// ignored
}
final javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
Expand All @@ -1415,9 +1413,7 @@ private static org.w3c.dom.Document createDocument() {
factory.setNamespaceAware(true);
try {
factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
} catch (javax.xml.parsers.ParserConfigurationException ignored) {
} catch (Exception ignored) {
// ignored
}
final javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/github/underscore/ChainingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public String apply(Map<String, Object> item) {
})
.first().item();
assertEquals("moe is 21", youngest);
U.of(stooges);
}

@Test
Expand All @@ -101,6 +102,7 @@ public String apply(Map<String, Object> item) {
})
.first().item();
assertEquals("moe is 21", youngest);
U.of(stooges);
}

@Test
Expand All @@ -126,6 +128,7 @@ public String apply(Map<String, Object> item) {
})
.first().item().toString();
assertEquals("moe is 21", youngest);
new U(stooges).of();
}

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

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

/*
Expand Down Expand Up @@ -409,6 +414,7 @@ public Integer apply(Integer accum, Integer length) {
}
}, 0).item();
assertEquals(34, sum);
U.of(words);
}

@Test
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/github/underscore/UnderscoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ public long[] apply(long[] arg) {
}
});
assertEquals(1L, U.chain(iterable, 5).first().item()[0]);
U.of(iterable, 5);
class MyIterable<T> implements Iterable<T> {
public Iterator<T> iterator() {
return new Iterator<T>() {
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/com/github/underscore/lodash/LodashTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright 2015-2019 Valentyn Kolesnikov
* Copyright 2015-2020 Valentyn Kolesnikov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -623,6 +623,11 @@ public void xmlToJson() {
+ " \"#omit-xml-declaration\": \"yes\"\n"
+ "}",
U.xmlToJson("<a/>", U.Mode.REPLACE_SELF_CLOSING_WITH_NULL));
assertEquals("{\n"
+ " \"a\": \"\",\n"
+ " \"#omit-xml-declaration\": \"yes\"\n"
+ "}",
U.xmlToJson("<a/>", U.Mode.REPLACE_SELF_CLOSING_WITH_EMPTY));
assertEquals("{\n"
+ " \"a\": {\n"
+ " \"-b\": \"c\"\n"
Expand Down Expand Up @@ -906,13 +911,20 @@ public void main() {
new U(new ArrayList<String>());
new U("");
new U(asList()).chain();
new U(asList()).of();
new Json();
new Xml();
U.chain(new ArrayList<String>());
U.chain(new ArrayList<String>(), 1);
U.chain(new HashSet<String>());
U.chain(new String[] {});
U.chain("");
U.of(new ArrayList<String>());
U.of(new ArrayList<String>(), 1);
U.of(new HashSet<String>());
U.of(new String[] {});
U.of(new int[] {});
U.of("");
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit 9824602

Please sign in to comment.