Skip to content

Commit e9fccde

Browse files
committed
Sync with underscore-java.
1 parent 5e00dca commit e9fccde

File tree

6 files changed

+143
-31
lines changed

6 files changed

+143
-31
lines changed

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class U<T> {
5656
private static final String S_Q = "\\s*\\Q";
5757
private static final String E_S = "\\E\\s*";
5858
private static final java.util.regex.Pattern FORMAT_PATTERN =
59-
java.util.regex.Pattern.compile("\\{\\s*(\\d*)\\s*}");
59+
java.util.regex.Pattern.compile("\\{\\s*(\\d*)\\s*\\}");
6060
private static final Map<Character, String> ESCAPES = new HashMap<Character, String>();
6161
private final Iterable<T> iterable;
6262
private final Optional<String> string;
@@ -1028,11 +1028,28 @@ public static <K, E> Map<K, Integer> countBy(final Iterable<E> iterable, Functio
10281028
return retVal;
10291029
}
10301030

1031+
public static <K> Map<K, Integer> countBy(final Iterable<K> iterable) {
1032+
final Map<K, Integer> retVal = newLinkedHashMap();
1033+
for (K key : iterable) {
1034+
if (retVal.containsKey(key)) {
1035+
retVal.put(key, 1 + retVal.get(key));
1036+
} else {
1037+
retVal.put(key, 1);
1038+
}
1039+
}
1040+
return retVal;
1041+
}
1042+
10311043
@SuppressWarnings("unchecked")
10321044
public <K, E> Map<K, Integer> countBy(Function<E, K> func) {
10331045
return countBy((Iterable<E>) iterable, func);
10341046
}
10351047

1048+
@SuppressWarnings("unchecked")
1049+
public <K> Map<K, Integer> countBy() {
1050+
return countBy((Iterable<K>) iterable);
1051+
}
1052+
10361053
/*
10371054
* Documented, #toArray
10381055
*/
@@ -2035,6 +2052,16 @@ public static <T> java.util.concurrent.ScheduledFuture<T> defer(final Supplier<T
20352052
return delay(function, 0);
20362053
}
20372054

2055+
public static java.util.concurrent.ScheduledFuture<Void> defer(final Runnable runnable) {
2056+
return delay(new Supplier<Void>() {
2057+
@Override
2058+
public Void get() {
2059+
runnable.run();
2060+
return null;
2061+
}
2062+
}, 0);
2063+
}
2064+
20382065
public static <T> Supplier<T> throttle(final Supplier<T> function, final int waitMilliseconds) {
20392066
class ThrottleFunction implements Supplier<T> {
20402067
private final Supplier<T> localFunction;
@@ -2546,9 +2573,9 @@ public boolean test(final Map<K, V> item) {
25462573
/*
25472574
* Documented, #times
25482575
*/
2549-
public static <E> void times(final int count, final Supplier<E> function) {
2576+
public static void times(final int count, final Runnable runnable) {
25502577
for (int index = 0; index < count; index += 1) {
2551-
function.get();
2578+
runnable.run();
25522579
}
25532580
}
25542581

@@ -2883,6 +2910,10 @@ public <F> Chain<Map<F, Integer>> countBy(final Function<T, F> func) {
28832910
return new Chain<Map<F, Integer>>(U.countBy(list, func));
28842911
}
28852912

2913+
public Chain<Map<T, Integer>> countBy() {
2914+
return new Chain<Map<T, Integer>>(U.countBy(list));
2915+
}
2916+
28862917
public Chain<T> shuffle() {
28872918
return new Chain<T>(U.shuffle(list));
28882919
}

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

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ public <F> Chain<Map<F, Integer>> countBy(final Function<T, F> func) {
268268
return new Chain<Map<F, Integer>>(U.countBy(value(), func));
269269
}
270270

271+
public Chain<Map<T, Integer>> countBy() {
272+
return new Chain<Map<T, Integer>>(U.countBy(value()));
273+
}
274+
271275
public Chain<T> shuffle() {
272276
return new Chain<T>(U.shuffle(value()));
273277
}
@@ -1190,8 +1194,8 @@ public static String camelCase(final String string) {
11901194
return createCompounder(new Function3<String, String, Integer, String>() {
11911195
public String apply(final String result, final String word, final Integer index) {
11921196
final String localWord = word.toLowerCase(Locale.getDefault());
1193-
return result + (index > 0 ? word.substring(0, 1).toUpperCase(Locale.getDefault())
1194-
+ word.substring(1) : localWord);
1197+
return result + (index > 0 ? localWord.substring(0, 1).toUpperCase(Locale.getDefault())
1198+
+ localWord.substring(1) : localWord);
11951199
}
11961200
}).apply(string);
11971201
}
@@ -2115,6 +2119,7 @@ public static String jsonToXml(String json) {
21152119
return jsonToXml(json, Xml.XmlStringBuilder.Step.TWO_SPACES);
21162120
}
21172121

2122+
@SuppressWarnings("unchecked")
21182123
public static String xmlToJson(String xml, Json.JsonStringBuilder.Step identStep, Mode mode) {
21192124
Object result = Xml.fromXml(xml);
21202125
if (result instanceof Map) {
@@ -2148,7 +2153,6 @@ public static String formatXml(String xml) {
21482153
return Xml.formatXml(xml);
21492154
}
21502155

2151-
@SuppressWarnings("unchecked")
21522156
public static Map<String, Object> removeMinusesAndConvertNumbers(Map<String, Object> map) {
21532157
Map<String, Object> outMap = newLinkedHashMap();
21542158
for (Map.Entry<String, Object> entry : map.entrySet()) {
@@ -2175,7 +2179,7 @@ private static Object makeObject(Object value) {
21752179
}
21762180
result = values;
21772181
} else if (value instanceof Map) {
2178-
result = removeMinusesAndConvertNumbers((Map<String, Object>) value);
2182+
result = removeMinusesAndConvertNumbers((Map) value);
21792183
} else {
21802184
String stringValue = String.valueOf(value);
21812185
result = isJsonNumber(stringValue) ? Xml.stringToNumber(stringValue) : value;
@@ -2214,11 +2218,9 @@ public static boolean isJsonNumber(final String string) {
22142218
return numberEncountered;
22152219
}
22162220

2217-
@SuppressWarnings("unchecked")
2218-
public static Map<String, Object> replaceSelfClosingWithNull(Map map) {
2221+
public static Map<String, Object> replaceSelfClosingWithNull(Map<String, Object> map) {
22192222
Map<String, Object> outMap = newLinkedHashMap();
2220-
for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
2221-
Map.Entry entry = (Map.Entry) it.next();
2223+
for (Map.Entry<String, Object> entry : map.entrySet()) {
22222224
if ("-self-closing".equals(entry.getKey()) && "true".equals(entry.getValue())) {
22232225
if (map.size() == 1) {
22242226
outMap = null;
@@ -2248,4 +2250,59 @@ private static Object makeObjectSelfClose(Object value) {
22482250
return result;
22492251
}
22502252

2253+
public static Builder objectBuilder() {
2254+
return new U.Builder();
2255+
}
2256+
2257+
public static class Builder {
2258+
private final Map<String, Object> data;
2259+
public Builder() {
2260+
data = newLinkedHashMap();
2261+
}
2262+
2263+
public Builder add(final String key, final Object value) {
2264+
data.put(key, value);
2265+
return this;
2266+
}
2267+
2268+
public Builder add(final Object value) {
2269+
data.put(String.valueOf(data.size()), value);
2270+
return this;
2271+
}
2272+
2273+
public Builder set(final String path, final Object value) {
2274+
U.set(data, path, value);
2275+
return this;
2276+
}
2277+
2278+
public Builder add(final Builder builder) {
2279+
data.putAll(builder.data);
2280+
return this;
2281+
}
2282+
2283+
@SuppressWarnings("unchecked")
2284+
public Map<String, Object> build() {
2285+
return (Map<String, Object>) ((LinkedHashMap) data).clone();
2286+
}
2287+
2288+
public String toXml() {
2289+
return Xml.toXml(data);
2290+
}
2291+
2292+
public static Builder fromXml(final String xml) {
2293+
final Builder builder = new Builder();
2294+
builder.data.putAll(fromXmlMap(xml));
2295+
return builder;
2296+
}
2297+
2298+
public String toJson() {
2299+
return Xml.toXml(data);
2300+
}
2301+
2302+
public static Builder fromJson(final String json) {
2303+
final Builder builder = new Builder();
2304+
builder.data.putAll(fromJsonMap(json));
2305+
return builder;
2306+
}
2307+
}
22512308
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,9 @@ public String apply(Person person) {
17181718
}
17191719
}).item();
17201720
assertEquals("{moe=2, curly=1}", resultChain.toString());
1721+
U.countBy(asList(1, 2, 3));
1722+
new U<Integer>(asList(1, 2, 3)).countBy();
1723+
U.chain(asList(1, 2, 2, 3)).countBy().item();
17211724
}
17221725

17231726
/*

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public Integer apply(final Integer n) {
116116
*/
117117

118118
@Test
119-
public void throttle() throws Exception {
119+
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; } };
@@ -131,7 +131,7 @@ public Void get() {
131131
}
132132
}, 60);
133133
await().atMost(180, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
134-
public Boolean call() throws Exception {
134+
public Boolean call() {
135135
throttleIncr.get();
136136
return true;
137137
}
@@ -148,7 +148,7 @@ public Boolean call() throws Exception {
148148
*/
149149

150150
@Test
151-
public void debounce() throws Exception {
151+
public void debounce() {
152152
final Integer[] counter = new Integer[] {0};
153153
Supplier<Void> incr = new Supplier<Void>() { public Void get() {
154154
counter[0]++; return null; } };
@@ -162,8 +162,8 @@ public Void get() {
162162
return null;
163163
}
164164
}, 60);
165-
await().atMost(160, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
166-
public Boolean call() throws Exception {
165+
await().atMost(120, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
166+
public Boolean call() {
167167
return true;
168168
}
169169
});
@@ -174,7 +174,7 @@ public Boolean call() throws Exception {
174174
// Returns from the function before the alert runs.
175175
*/
176176
@Test
177-
public void defer() throws Exception {
177+
public void defer() {
178178
final Integer[] counter = new Integer[] {0};
179179
U.defer(new Supplier<Void>() { public Void get() {
180180
try {
@@ -185,11 +185,14 @@ public void defer() throws Exception {
185185
counter[0]++; return null; } });
186186
assertEquals("incr was debounced", 0, counter[0].intValue());
187187
await().atLeast(60, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
188-
public Boolean call() throws Exception {
188+
public Boolean call() {
189189
assertEquals("incr was debounced", 1, counter[0].intValue());
190190
return true;
191191
}
192192
});
193+
U.defer(new Runnable() { public void run() {
194+
}
195+
});
193196
}
194197

195198
/*
@@ -199,15 +202,15 @@ public Boolean call() throws Exception {
199202
// Application is only created once.
200203
*/
201204
@Test
202-
public void once() throws Exception {
205+
public void once() {
203206
final Integer[] counter = new Integer[] {0};
204207
Supplier<Integer> incr = new Supplier<Integer>() { public Integer get() {
205208
counter[0]++; return counter[0]; } };
206209
final Supplier<Integer> onceIncr = U.once(incr);
207210
onceIncr.get();
208211
onceIncr.get();
209212
await().atLeast(60, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
210-
public Boolean call() throws Exception {
213+
public Boolean call() {
211214
assertEquals("incr was called only once", 1, counter[0].intValue());
212215
assertEquals("stores a memo to the last value", 1, onceIncr.get().intValue());
213216
return true;
@@ -348,37 +351,37 @@ public void iteratee() {
348351
}
349352

350353
@Test
351-
public void setTimeout() throws Exception {
354+
public void setTimeout() {
352355
final Integer[] counter = new Integer[] {0};
353356
Supplier<Void> incr = new Supplier<Void>() { public Void get() {
354357
counter[0]++; return null; } };
355358
U.setTimeout(incr, 0);
356359
await().atLeast(40, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
357-
public Boolean call() throws Exception {
360+
public Boolean call() {
358361
assertEquals(1, counter[0].intValue());
359362
return true;
360363
}
361364
});
362365
}
363366

364367
@Test
365-
public void clearTimeout() throws Exception {
368+
public void clearTimeout() {
366369
final Integer[] counter = new Integer[] {0};
367370
Supplier<Void> incr = new Supplier<Void>() { public Void get() {
368371
counter[0]++; return null; } };
369372
java.util.concurrent.ScheduledFuture future = U.setTimeout(incr, 20);
370373
U.clearTimeout(future);
371374
U.clearTimeout(null);
372375
await().atLeast(40, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
373-
public Boolean call() throws Exception {
376+
public Boolean call() {
374377
assertEquals(0, counter[0].intValue());
375378
return true;
376379
}
377380
});
378381
}
379382

380383
@Test
381-
public void setInterval() throws Exception {
384+
public void setInterval() {
382385
final Integer[] counter = new Integer[] {0};
383386
Supplier<Void> incr = new Supplier<Void>() { public Void get() {
384387
if (counter[0] < 4) {
@@ -387,7 +390,7 @@ public void setInterval() throws Exception {
387390
return null; } };
388391
U.setInterval(incr, 10);
389392
await().atLeast(45, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
390-
public Boolean call() throws Exception {
393+
public Boolean call() {
391394
assertTrue("Counter is not in range [0, 4] " + counter[0],
392395
asList(0, 4).contains(counter[0]));
393396
return true;
@@ -396,15 +399,15 @@ public Boolean call() throws Exception {
396399
}
397400

398401
@Test
399-
public void clearInterval() throws Exception {
402+
public void clearInterval() {
400403
final Integer[] counter = new Integer[] {0};
401404
Supplier<Void> incr = new Supplier<Void>() { public Void get() {
402405
counter[0]++; return null; } };
403406
java.util.concurrent.ScheduledFuture future = U.setInterval(incr, 20);
404407
U.clearInterval(future);
405408
U.clearInterval(null);
406409
await().atLeast(40, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
407-
public Boolean call() throws Exception {
410+
public Boolean call() {
408411
assertEquals(0, counter[0].intValue());
409412
return true;
410413
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,9 @@ public void uniquePassword() {
148148
@Test
149149
public void times() {
150150
final List<Integer> result = new ArrayList<Integer>();
151-
U.times(3, new Supplier<Integer>() {
152-
public Integer get() {
151+
U.times(3, new Runnable() {
152+
public void run() {
153153
result.add(1);
154-
return null;
155154
}
156155
});
157156
assertEquals("[1, 1, 1]", result.toString());

0 commit comments

Comments
 (0)