Skip to content

Commit 9a59f4c

Browse files
committed
Sync with underscore-java.
1 parent 6cbff38 commit 9a59f4c

File tree

8 files changed

+82
-29
lines changed

8 files changed

+82
-29
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.underscore;
22

33
import java.util.function.Function;
4+
import java.util.function.Predicate;
45
import java.util.function.Supplier;
56

67
public final class Optional<T> {
@@ -61,6 +62,16 @@ public boolean isPresent() {
6162
return !absent;
6263
}
6364

65+
@SuppressWarnings("unchecked")
66+
public Optional<T> filter(Predicate<? super T> predicate) {
67+
U.checkNotNull(predicate);
68+
if (isPresent()) {
69+
return predicate.test(arg) ? this : Optional.<T>absent();
70+
} else {
71+
return this;
72+
}
73+
}
74+
6475
public <F> Optional<F> map(Function<? super T, F> mapper) {
6576
U.checkNotNull(mapper);
6677
if (isPresent()) {

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
public class U<T> {
4444
private static final Map<String, Function<String, String>> FUNCTIONS = newLinkedHashMap();
4545
private static final Map<String, String> TEMPLATE_SETTINGS = new HashMap<String, String>();
46-
private static final int ARRAY_SIZE_2 = 2;
4746
private static final int MIN_PASSWORD_LENGTH_8 = 8;
4847
private static final long CAPACITY_SIZE_5 = 5L;
4948
private static final long CAPACITY_COEFF_2 = 2L;
@@ -57,7 +56,7 @@ public class U<T> {
5756
private static final String S_Q = "\\s*\\Q";
5857
private static final String E_S = "\\E\\s*";
5958
private static final java.util.regex.Pattern FORMAT_PATTERN =
60-
java.util.regex.Pattern.compile("\\{\\s*(\\d*)\\s*\\}");
59+
java.util.regex.Pattern.compile("\\{\\s*(\\d*)\\s*}");
6160
private static final Map<Character, String> ESCAPES = new HashMap<Character, String>();
6261
private final Iterable<T> iterable;
6362
private final Optional<String> string;
@@ -721,10 +720,10 @@ public Class<?> apply(Object input) {
721720
});
722721
try {
723722
final Method method = iterable.iterator().next().getClass().getMethod(methodName, argTypes.toArray(
724-
new Class[argTypes.size()]));
723+
new Class[0]));
725724
for (E arg : iterable) {
726725
try {
727-
result.add((E) method.invoke(arg, args.toArray(new Object[args.size()])));
726+
result.add((E) method.invoke(arg, args.toArray(new Object[0])));
728727
} catch (Exception e) {
729728
throw new IllegalArgumentException(e);
730729
}
@@ -920,8 +919,8 @@ public static <T extends Comparable<? super T>> List<T> sortWith(final Iterable<
920919
}
921920

922921
@SuppressWarnings("unchecked")
923-
public <T extends Comparable<? super T>> List<T> sortWith(final Comparator<T> comparator) {
924-
return sortWith((Iterable<T>) iterable, comparator);
922+
public <E extends Comparable<? super E>> List<E> sortWith(final Comparator<E> comparator) {
923+
return sortWith((Iterable<E>) iterable, comparator);
925924
}
926925

927926
/*
@@ -1131,7 +1130,7 @@ public static <E> List<List<E>> partition(final Iterable<E> iterable, final Pred
11311130

11321131
@SuppressWarnings("unchecked")
11331132
public static <E> List<E>[] partition(final E[] iterable, final Predicate<E> pred) {
1134-
return (List<E>[]) partition(Arrays.asList(iterable), pred).toArray(new ArrayList[ARRAY_SIZE_2]);
1133+
return partition(Arrays.asList(iterable), pred).toArray(new ArrayList[0]);
11351134
}
11361135

11371136
public T singleOrNull() {
@@ -1568,8 +1567,8 @@ public static <E> List<E> intersection(final List<E> list1, final List<E> list2)
15681567
public static <E> List<E> intersection(final List<E> list, final List<E> ... lists) {
15691568
final Deque<List<E>> stack = new ArrayDeque<List<E>>();
15701569
stack.push(list);
1571-
for (int index = 0; index < lists.length; index += 1) {
1572-
stack.push(intersection(stack.peek(), lists[index]));
1570+
for (List<E> es : lists) {
1571+
stack.push(intersection(stack.peek(), es));
15731572
}
15741573
return stack.peek();
15751574
}
@@ -1606,8 +1605,8 @@ public static <E> List<E> difference(final List<E> list1, final List<E> list2) {
16061605
public static <E> List<E> difference(final List<E> list, final List<E> ... lists) {
16071606
final Deque<List<E>> stack = new ArrayDeque<List<E>>();
16081607
stack.push(list);
1609-
for (int index = 0; index < lists.length; index += 1) {
1610-
stack.push(difference(stack.peek(), lists[index]));
1608+
for (List<E> es : lists) {
1609+
stack.push(difference(stack.peek(), es));
16111610
}
16121611
return stack.peek();
16131612
}
@@ -1655,8 +1654,8 @@ public static <T> List<List<T>> unzip(final List<T> ... lists) {
16551654
final List<List<T>> unzipped = newArrayList();
16561655
for (int index = 0; index < lists[0].size(); index += 1) {
16571656
final List<T> nTuple = newArrayList();
1658-
for (int index2 = 0; index2 < lists.length; index2 += 1) {
1659-
nTuple.add(lists[index2].get(index));
1657+
for (List<T> list : lists) {
1658+
nTuple.add(list.get(index));
16601659
}
16611660
unzipped.add(nTuple);
16621661
}
@@ -1866,7 +1865,7 @@ public static <T> List<List<T>> chunk(final Iterable<T> iterable, final int size
18661865
}
18671866
int index = 0;
18681867
int length = size(iterable);
1869-
final List<List<T>> result = new ArrayList<List<T>>(size == 0 ? size : ((length / size) + 1));
1868+
final List<List<T>> result = new ArrayList<List<T>>(size == 0 ? size : (length / size) + 1);
18701869
while (index < length) {
18711870
result.add(newArrayList(iterable).subList(index, Math.min(length, index + size)));
18721871
index += step;
@@ -2264,9 +2263,9 @@ public static <K, V> Map<K, V> extend(final Map<K, V> destination, final Map<K,
22642263
}
22652264

22662265
public static <E> E findKey(final List<E> list, final Predicate<E> pred) {
2267-
for (int index = 0; index < list.size(); index++) {
2268-
if (pred.test(list.get(index))) {
2269-
return list.get(index);
2266+
for (E e : list) {
2267+
if (pred.test(e)) {
2268+
return e;
22702269
}
22712270
}
22722271
return null;
@@ -3127,7 +3126,7 @@ public Optional<String> call(final String funcName) {
31273126

31283127
public static <T extends Comparable<T>> List<T> sort(final Iterable<T> iterable) {
31293128
final List<T> localList = newArrayList(iterable);
3130-
Collections.<T>sort(localList);
3129+
Collections.sort(localList);
31313130
return localList;
31323131
}
31333132

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,15 +781,15 @@ private void startCapture() {
781781
}
782782

783783
private void pauseCapture() {
784-
captureBuffer.append(json.substring(captureStart, index - 1));
784+
captureBuffer.append(json, captureStart, index - 1);
785785
captureStart = -1;
786786
}
787787

788788
private String endCapture() {
789789
int end = current == -1 ? index : index - 1;
790790
String captured;
791791
if (captureBuffer.length() > 0) {
792-
captureBuffer.append(json.substring(captureStart, end));
792+
captureBuffer.append(json, captureStart, end);
793793
captured = captureBuffer.toString();
794794
captureBuffer.setLength(0);
795795
} else {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -935,8 +935,7 @@ public static <T> List<T> at(final List<T> list, final Integer ... indexes) {
935935
final List<T> result = newArrayList();
936936
final List<Integer> indexesList = Arrays.asList(indexes);
937937
int index = 0;
938-
for (final Iterator<T> iterator = list.iterator(); iterator.hasNext(); ) {
939-
final T object = iterator.next();
938+
for (final T object : list) {
940939
if (indexesList.contains(index)) {
941940
result.add(object);
942941
}
@@ -1259,7 +1258,7 @@ public String apply(final String string) {
12591258
final String localString = baseToString(string);
12601259
final String chr = localString.isEmpty() ? "" : localString.substring(0, 1);
12611260
final String trailing = localString.length() > 1 ? localString.substring(1) : "";
1262-
return U.invoke(Arrays.asList(chr), methodName).get(0) + trailing;
1261+
return U.invoke(Collections.singletonList(chr), methodName).get(0) + trailing;
12631262
}
12641263
};
12651264
}
@@ -1444,7 +1443,7 @@ public static String trimStart(final String string, final String chars) {
14441443
localChars = chars;
14451444
}
14461445
final int leftIndex = charsLeftIndex(localString, localChars);
1447-
return leftIndex > -1 ? localString.substring(leftIndex, localString.length()) : localString;
1446+
return leftIndex > -1 ? localString.substring(leftIndex) : localString;
14481447
}
14491448

14501449
public static String trimEnd(final String string) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,8 +1088,9 @@ private static Object checkArray(final Map<String, Object> map, final String nam
10881088
final Map<String, Object> localMap4 = (Map) ((LinkedHashMap) localMap).clone();
10891089
localMap4.remove(ARRAY);
10901090
object = name.equals(XmlValue.getMapKey(localMap4))
1091-
? U.newArrayList(Arrays.asList(getValue(XmlValue.getMapValue(localMap4), FromType.FOR_CONVERT)))
1092-
: U.newArrayList(Arrays.asList(getValue(localMap4, FromType.FOR_CONVERT)));
1091+
? U.newArrayList(Collections.singletonList(getValue(XmlValue.getMapValue(localMap4),
1092+
FromType.FOR_CONVERT)))
1093+
: U.newArrayList(Collections.singletonList(getValue(localMap4, FromType.FOR_CONVERT)));
10931094
} else {
10941095
object = localMap;
10951096
}
@@ -1180,7 +1181,7 @@ static String getAttributes(final int sourceIndex, final String source) {
11801181

11811182
private static String unescapeName(final String name) {
11821183
if (name == null) {
1183-
return name;
1184+
return null;
11841185
}
11851186
final int length = name.length();
11861187
if ("__EE__EMPTY__EE__".equals(name)) {
@@ -1428,7 +1429,7 @@ public String apply(Object object, Set<String> namespaces) {
14281429
}
14291430
}, new Function<Object, Object>() {
14301431
public Object apply(Object object) {
1431-
return object instanceof List ? object : U.newArrayList(Arrays.asList(object));
1432+
return object instanceof List ? object : U.newArrayList(Collections.singletonList(object));
14321433
}
14331434
}, Collections.<String, Object>emptyMap(), new int[] {1, 1, 1}, xml, new int[] {0},
14341435
U.<String>newLinkedHashSet(), FromType.FOR_CONVERT);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public Void get() {
162162
return null;
163163
}
164164
}, 60);
165-
await().atMost(120, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
165+
await().atMost(160, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
166166
public Boolean call() throws Exception {
167167
return true;
168168
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,26 @@ public String apply(Integer arg) {
456456
fail("IllegalStateException expected");
457457
} catch (IllegalStateException ex) {
458458
}
459+
assertFalse(Optional.<Integer>absent().filter(new Predicate<Integer>() {
460+
public boolean test(Integer arg) {
461+
return true;
462+
}
463+
}).isPresent());
464+
assertTrue(Optional.<Integer>absent().filter(new Predicate<Integer>() {
465+
public boolean test(Integer arg) {
466+
return false;
467+
}
468+
}).isEmpty());
469+
assertEquals("1", Optional.of(1).filter(new Predicate<Integer>() {
470+
public boolean test(Integer arg) {
471+
return true;
472+
}
473+
}).get().toString());
474+
assertTrue("1", Optional.of(1).filter(new Predicate<Integer>() {
475+
public boolean test(Integer arg) {
476+
return false;
477+
}
478+
}).isEmpty());
459479
}
460480

461481
@Test(expected = Exception.class)

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright 2015-2019 Valentyn Kolesnikov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
124
package com.github.underscore.lodash;
225

326
import static org.junit.Assert.assertEquals;

0 commit comments

Comments
 (0)