Skip to content

Commit 6776400

Browse files
committed
Sync with underscore-java.
1 parent 542edfd commit 6776400

File tree

11 files changed

+219
-18
lines changed

11 files changed

+219
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Include the following in your `pom.xml` for Maven:
2525
<dependency>
2626
<groupId>com.github.javadev</groupId>
2727
<artifactId>underscore11</artifactId>
28-
<version>1.12</version>
28+
<version>1.13</version>
2929
</dependency>
3030
...
3131
</dependencies>
@@ -34,7 +34,7 @@ Include the following in your `pom.xml` for Maven:
3434
Gradle:
3535

3636
```groovy
37-
compile 'com.github.javadev:underscore11:1.12'
37+
compile 'com.github.javadev:underscore11:1.13'
3838
```
3939

4040
Underscore-java is a java port of [Underscore.js](http://underscorejs.org/).

pom-central.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.javadev</groupId>
66
<artifactId>underscore11</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.12</version>
8+
<version>1.13</version>
99
<name>java 11 port of Underscore.js</name>
1010
<description>The java 11 port of Underscore.js</description>
1111
<url>https://github.com/javadev/underscore-java11</url>

pom-pack.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.javadev</groupId>
66
<artifactId>underscore11</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.12</version>
8+
<version>1.13</version>
99
<name>java 11 port of Underscore.js</name>
1010
<description>The java 11 port of Underscore.js</description>
1111
<url>https://github.com/javadev/underscore-java11</url>
@@ -92,6 +92,7 @@
9292
<option>-keep public class com.github.underscore.PredicateIndexed { *; }</option>
9393
<option>-keep public class com.github.underscore.Template { *; }</option>
9494
<option>-keep public class com.github.underscore.Trie { *; }</option>
95+
<option>-keep public class com.github.underscore.Trie$TrieNode { *; }</option>
9596
<option>-keep public class com.github.underscore.Tuple { *; }</option>
9697
<option>-keep public class com.github.underscore.lodash.Base32 { *; }</option>
9798
<option>-keep public class com.github.underscore.lodash.Json { *; }</option>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.javadev</groupId>
66
<artifactId>underscore11</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.12-SNAPSHOT</version>
8+
<version>1.13-SNAPSHOT</version>
99
<name>java 11 port of Underscore.js</name>
1010
<description>The java 11 port of Underscore.js</description>
1111
<url>https://github.com/javadev/underscore-java11</url>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
package com.github.underscore;
2525

2626
public class Trie {
27-
static class TrieNode {
27+
public static class TrieNode {
2828
// Initialize your data structure here.
2929
TrieNode[] children;
3030
boolean isWord;

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

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,30 +2076,63 @@ public Void get() {
20762076
}
20772077

20782078
public static <T> Supplier<T> throttle(final Supplier<T> function, final int waitMilliseconds) {
2079-
class ThrottleFunction implements Supplier<T> {
2079+
class ThrottleLater implements Supplier<T> {
20802080
private final Supplier<T> localFunction;
2081-
private long previous;
20822081
private java.util.concurrent.ScheduledFuture<T> timeout;
2082+
private long previous;
2083+
2084+
ThrottleLater(final Supplier<T> function) {
2085+
this.localFunction = function;
2086+
}
2087+
2088+
@Override
2089+
public T get() {
2090+
previous = now();
2091+
timeout = null;
2092+
return localFunction.get();
2093+
}
2094+
2095+
java.util.concurrent.ScheduledFuture<T> getTimeout() {
2096+
return timeout;
2097+
}
2098+
2099+
void setTimeout(java.util.concurrent.ScheduledFuture<T> timeout) {
2100+
this.timeout = timeout;
2101+
}
2102+
2103+
long getPrevious() {
2104+
return previous;
2105+
}
2106+
2107+
void setPrevious(long previous) {
2108+
this.previous = previous;
2109+
}
2110+
}
2111+
2112+
class ThrottleFunction implements Supplier<T> {
2113+
private final Supplier<T> localFunction;
2114+
private final ThrottleLater throttleLater;
20832115

20842116
ThrottleFunction(final Supplier<T> function) {
20852117
this.localFunction = function;
2118+
this.throttleLater = new ThrottleLater(function);
20862119
}
20872120

20882121
@Override
20892122
public T get() {
20902123
final long now = now();
2091-
if (previous == 0L) {
2092-
previous = now;
2124+
if (throttleLater.getPrevious() == 0L) {
2125+
throttleLater.setPrevious(now);
20932126
}
2094-
final long remaining = waitMilliseconds - (now - previous);
2127+
final long remaining = waitMilliseconds - (now - throttleLater.getPrevious());
2128+
T result = null;
20952129
if (remaining <= 0) {
2096-
clearTimeout(timeout);
2097-
previous = now;
2098-
localFunction.get();
2099-
} else {
2100-
timeout = delay(localFunction, waitMilliseconds);
2130+
throttleLater.setPrevious(now);
2131+
result = localFunction.get();
2132+
} else if (throttleLater.getTimeout() == null) {
2133+
throttleLater.setTimeout(delay(throttleLater, waitMilliseconds));
21012134
}
2102-
return null;
2135+
return result;
21032136
}
21042137
}
21052138
return new ThrottleFunction(function);

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,72 @@ private static Object makeObjectForRename(Object value, final String oldKey, fin
16131613
return result;
16141614
}
16151615

1616+
public static Map<String, Object> setValue(final Map<String, Object> map, final String key,
1617+
final Object newValue) {
1618+
return setValue(map, key, new BiFunction<String, Object, Object>() {
1619+
public Object apply(String key, Object value) { return newValue; } });
1620+
}
1621+
1622+
public static Map<String, Object> setValue(final Map<String, Object> map, final String key,
1623+
final BiFunction<String, Object, Object> newValue) {
1624+
Map<String, Object> outMap = newLinkedHashMap();
1625+
for (Map.Entry<String, Object> entry : map.entrySet()) {
1626+
if (entry.getKey().equals(key)) {
1627+
outMap.put(key, makeObjectForSetValue(newValue.apply(key, entry.getValue()), key, newValue));
1628+
} else {
1629+
outMap.put(entry.getKey(), makeObjectForSetValue(entry.getValue(), key, newValue));
1630+
}
1631+
}
1632+
return outMap;
1633+
}
1634+
1635+
@SuppressWarnings("unchecked")
1636+
private static Object makeObjectForSetValue(Object value, final String key,
1637+
final BiFunction<String, Object, Object> newValue) {
1638+
final Object result;
1639+
if (value instanceof List) {
1640+
List<Object> values = newArrayList();
1641+
for (Object item : (List) value) {
1642+
values.add(item instanceof Map ? setValue((Map<String, Object>) item, key, newValue) : item);
1643+
}
1644+
result = values;
1645+
} else if (value instanceof Map) {
1646+
result = setValue((Map<String, Object>) value, key, newValue);
1647+
} else {
1648+
result = value;
1649+
}
1650+
return result;
1651+
}
1652+
1653+
@SuppressWarnings("unchecked")
1654+
public static Map<String, Object> update(final Map<String, Object> map1, final Map<String, Object> map2) {
1655+
Map<String, Object> outMap = newLinkedHashMap();
1656+
for (String key : map2.keySet()) {
1657+
Object value2 = map2.get(key);
1658+
if (map1.containsKey(key)) {
1659+
Object value1 = map1.get(key);
1660+
if (value1 instanceof Map && value2 instanceof Map) {
1661+
outMap.put(key, update((Map<String, Object>) value1, (Map<String, Object>) value2));
1662+
} else if (value1 instanceof List && value2 instanceof List) {
1663+
outMap.put(key, merge((List<Object>) value1, (List<Object>) value2));
1664+
} else {
1665+
outMap.put(key, value2);
1666+
}
1667+
} else {
1668+
outMap.put(key, value2);
1669+
}
1670+
}
1671+
return outMap;
1672+
}
1673+
1674+
public static List<Object> merge(List<Object> list1, List<Object> list2) {
1675+
List<Object> outList1 = newArrayList(list1);
1676+
List<Object> outList2 = newArrayList(list2);
1677+
outList2.removeAll(list1);
1678+
outList1.addAll(outList2);
1679+
return outList1;
1680+
}
1681+
16161682
public static class FetchResponse {
16171683
private final boolean ok;
16181684
private final int status;
@@ -2187,6 +2253,14 @@ public static String formatXml(String xml) {
21872253
return Xml.formatXml(xml);
21882254
}
21892255

2256+
public static String changeXmlEncoding(String xml, Xml.XmlStringBuilder.Step identStep, String encoding) {
2257+
return Xml.changeXmlEncoding(xml, identStep, encoding);
2258+
}
2259+
2260+
public static String changeXmlEncoding(String xml, String encoding) {
2261+
return Xml.changeXmlEncoding(xml, encoding);
2262+
}
2263+
21902264
public static Map<String, Object> removeMinusesAndConvertNumbers(Map<String, Object> map) {
21912265
Map<String, Object> outMap = newLinkedHashMap();
21922266
for (Map.Entry<String, Object> entry : map.entrySet()) {
@@ -2267,6 +2341,21 @@ public static Map<String, Object> replaceSelfClosingWithNull(Map<String, Object>
22672341
return outMap;
22682342
}
22692343

2344+
public static long gcd(long value1, long value2) {
2345+
if (value1 == 0) {
2346+
return value2;
2347+
}
2348+
return gcd(value2 % value1, value1);
2349+
}
2350+
2351+
public static long findGcd(long ... array) {
2352+
long result = array[0];
2353+
for (int index = 1; index < array.length; index += 1) {
2354+
result = gcd(array[index], result);
2355+
}
2356+
return result;
2357+
}
2358+
22702359
@SuppressWarnings("unchecked")
22712360
private static Object makeObjectSelfClose(Object value) {
22722361
final Object result;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,4 +1522,17 @@ public static String formatXml(String xml, XmlStringBuilder.Step identStep) {
15221522
public static String formatXml(String xml) {
15231523
return formatXml(xml, XmlStringBuilder.Step.THREE_SPACES);
15241524
}
1525+
1526+
@SuppressWarnings("unchecked")
1527+
public static String changeXmlEncoding(String xml, XmlStringBuilder.Step identStep, String encoding) {
1528+
Object result = fromXml(xml, FromType.FOR_FORMAT);
1529+
if (result instanceof Map) {
1530+
((Map) result).put(ENCODING, encoding);
1531+
}
1532+
return toXml((Map) result, identStep);
1533+
}
1534+
1535+
public static String changeXmlEncoding(String xml, String encoding) {
1536+
return changeXmlEncoding(xml, XmlStringBuilder.Step.THREE_SPACES, encoding);
1537+
}
15251538
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ public void throttle() {
120120
final Integer[] counter = new Integer[] {0};
121121
Supplier<Void> incr = new Supplier<Void>() { public Void get() {
122122
counter[0]++; return null; } };
123-
final Supplier<Void> throttleIncr = U.throttle(incr, 50);
123+
final Supplier<Void> throttleIncr = U.throttle(incr, 40);
124124
throttleIncr.get();
125125
throttleIncr.get();
126126
U.delay(throttleIncr, 16);
127127
U.delay(new Supplier<Void>() {
128128
public Void get() {
129129
assertEquals("incr was throttled", 1, counter[0].intValue());
130+
throttleIncr.get();
130131
return null;
131132
}
132133
}, 60);

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,51 @@ public void renameMapKey() {
686686
U.rename(map2, "test", "test1");
687687
}
688688

689+
@Test
690+
public void updateMapKey() {
691+
Map<String, Object> map = U.newLinkedHashMap();
692+
map.put("-self-closing", "false");
693+
U.rename(map, "test", "test1");
694+
Map<String, Object> newMap = U.update(map, map);
695+
assertEquals("{\n"
696+
+ " \"-self-closing\": \"false\"\n"
697+
+ "}",
698+
U.toJson(newMap));
699+
Map<String, Object> map2 = U.newLinkedHashMap();
700+
List<Object> list = U.newArrayList();
701+
list.add(U.newArrayList());
702+
list.add(U.newLinkedHashMap());
703+
map2.put("list", list);
704+
U.update(map2, map2);
705+
map2.put("list", U.newLinkedHashMap());
706+
U.update(map2, map2);
707+
U.update(map2, map);
708+
Map<String, Object> map3 = U.newLinkedHashMap();
709+
map3.put("list", U.newArrayList());
710+
U.update(map2, map3);
711+
U.update(map3, map2);
712+
}
713+
714+
@Test
715+
public void setValue() {
716+
Map<String, Object> map = U.newLinkedHashMap();
717+
map.put("-self-closing", "false");
718+
U.setValue(map, "test", "test1");
719+
Map<String, Object> newMap = U.setValue(map, "-self-closing", "true");
720+
assertEquals("{\n"
721+
+ " \"-self-closing\": \"true\"\n"
722+
+ "}",
723+
U.toJson(newMap));
724+
Map<String, Object> map2 = U.newLinkedHashMap();
725+
List<Object> list = U.newArrayList();
726+
list.add(U.newArrayList());
727+
list.add(U.newLinkedHashMap());
728+
map2.put("list", list);
729+
U.setValue(map2, "test", "test1");
730+
map2.put("list", U.newLinkedHashMap());
731+
U.setValue(map2, "test", "test1");
732+
}
733+
689734
@Test
690735
public void jsonToXml() {
691736
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<a></a>", U.jsonToXml("{\n \"a\": {\n }\n}"));
@@ -728,6 +773,17 @@ public void formatXml() {
728773
assertEquals("<a number=\"true\">2.01</a>", U.formatXml("<a number=\"true\">2.01</a>"));
729774
}
730775

776+
@Test
777+
public void changeXmlEncoding() {
778+
assertEquals("<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<a>Test</a>",
779+
U.changeXmlEncoding("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a>Test</a>", "windows-1251"));
780+
assertEquals("<?xml version=\"1.0\" encoding=\"windows-1251\"?><a>Test</a>",
781+
U.changeXmlEncoding("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a>Test</a>",
782+
Xml.XmlStringBuilder.Step.COMPACT, "windows-1251"));
783+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>null</root>",
784+
U.changeXmlEncoding(null, Xml.XmlStringBuilder.Step.COMPACT, "windows-1251"));
785+
}
786+
731787
@Test
732788
public void formatJson() {
733789
assertEquals("{\n \"a\": {\n }\n}", U.formatJson("{\n \"a\": {\n }\n}"));

0 commit comments

Comments
 (0)