Skip to content

Commit 79c1875

Browse files
committed
Sync with underscore-java
1 parent 9341bf2 commit 79c1875

File tree

8 files changed

+126
-58
lines changed

8 files changed

+126
-58
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@
214214
<version>[5.10.2,)</version>
215215
<scope>test</scope>
216216
</dependency>
217+
<dependency>
218+
<groupId>org.junit.jupiter</groupId>
219+
<artifactId>junit-jupiter-engine</artifactId>
220+
<version>[5.10.2,)</version>
221+
<scope>test</scope>
222+
</dependency>
217223
<dependency>
218224
<groupId>org.junit.platform</groupId>
219225
<artifactId>junit-platform-launcher</artifactId>

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

+9-18
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,29 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import java.nio.charset.StandardCharsets;
2726
import java.util.HashMap;
2827
import java.util.Map;
2928

3029
public final class Base32 {
3130

3231
private static final Base32 INSTANCE = new Base32();
33-
34-
private final char[] digits;
35-
private final int mask;
36-
private final int shift;
37-
private final Map<Character, Integer> charMap;
32+
private final char[] digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef".toCharArray();
33+
private final int mask = digits.length - 1;
34+
private final int shift = Integer.numberOfTrailingZeros(digits.length);
35+
private final Map<Character, Integer> charMap = new HashMap<>();
3836

3937
private Base32() {
40-
digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef".toCharArray();
41-
mask = digits.length - 1;
42-
shift = Integer.numberOfTrailingZeros(digits.length);
43-
charMap = new HashMap<>();
44-
for (int index = 0; index < digits.length; index += 1) {
38+
for (int index = 0; index < digits.length; index++) {
4539
charMap.put(digits[index], index);
4640
}
4741
}
4842

4943
public static String decode(final String encoded) {
50-
return new String(INSTANCE.decodeInternal(encoded), StandardCharsets.UTF_8);
44+
return new String(INSTANCE.decodeInternal(encoded));
5145
}
5246

5347
private byte[] decodeInternal(final String encoded) {
54-
if (encoded.length() == 0) {
48+
if (encoded.isEmpty()) {
5549
return new byte[0];
5650
}
5751
int encodedLength = encoded.length();
@@ -76,25 +70,23 @@ private byte[] decodeInternal(final String encoded) {
7670
}
7771

7872
public static String encode(final String data) {
79-
return INSTANCE.encodeInternal(data.getBytes(StandardCharsets.UTF_8));
73+
return INSTANCE.encodeInternal(data.getBytes());
8074
}
8175

8276
private String encodeInternal(final byte[] data) {
8377
if (data.length == 0) {
8478
return "";
8579
}
86-
8780
int outputLength = (data.length * 8 + shift - 1) / shift;
8881
StringBuilder result = new StringBuilder(outputLength);
89-
9082
int buffer = data[0];
9183
int next = 1;
9284
int bitsLeft = 8;
9385
while (bitsLeft > 0 || next < data.length) {
9486
if (bitsLeft < shift) {
9587
if (next < data.length) {
9688
buffer <<= 8;
97-
buffer = buffer | (data[next++] & 0xff);
89+
buffer |= (data[next++] & 0xff);
9890
bitsLeft += 8;
9991
} else {
10092
int pad = shift - bitsLeft;
@@ -110,7 +102,6 @@ private String encodeInternal(final byte[] data) {
110102
}
111103

112104
public static class DecodingException extends RuntimeException {
113-
114105
public DecodingException(final String message) {
115106
super(message);
116107
}

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ public JsonStringBuilder append(final String string) {
8383
}
8484

8585
public JsonStringBuilder fillSpaces() {
86-
for (int index = 0; index < indent; index += 1) {
87-
builder.append(identStep == Step.TABS ? '\t' : ' ');
88-
}
86+
builder.append(String.valueOf(identStep == Step.TABS ? '\t' : ' ').repeat(Math.max(0, indent)));
8987
return this;
9088
}
9189

@@ -458,7 +456,7 @@ public static class JsonParser {
458456
private int line;
459457
private int lineOffset;
460458
private int current;
461-
private StringBuilder captureBuffer = new StringBuilder();
459+
private final StringBuilder captureBuffer = new StringBuilder();
462460
private int captureStart;
463461
private final int maxDepth;
464462

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

+32-25
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import java.io.File;
2726
import java.io.FileInputStream;
2827
import java.io.FileOutputStream;
2928
import java.io.IOException;
@@ -91,23 +90,23 @@ public class U<T> extends Underscore<T> {
9190
private static final int BUFFER_LENGTH_1024 = 1024;
9291
private static final int RESPONSE_CODE_400 = 400;
9392
private static final String ROOT = "root";
94-
private static String upper = "[A-Z\\xc0-\\xd6\\xd8-\\xde\\u0400-\\u04FF]";
95-
private static String lower = "[a-z\\xdf-\\xf6\\xf8-\\xff]+";
96-
private static String selfClosing = "-self-closing";
97-
private static String nilKey = "-nil";
98-
private static java.util.regex.Pattern reWords =
93+
private static final String UPPER = "[A-Z\\xc0-\\xd6\\xd8-\\xde\\u0400-\\u04FF]";
94+
private static final String LOWER = "[a-z\\xdf-\\xf6\\xf8-\\xff]+";
95+
private static final String SELF_CLOSING = "-self-closing";
96+
private static final String NIL_KEY = "-nil";
97+
private static final java.util.regex.Pattern RE_WORDS =
9998
java.util.regex.Pattern.compile(
100-
upper
99+
UPPER
101100
+ "+(?="
102-
+ upper
103-
+ lower
101+
+ UPPER
102+
+ LOWER
104103
+ ")|"
105-
+ upper
104+
+ UPPER
106105
+ "?"
107-
+ lower
106+
+ LOWER
108107
+ "|"
109-
+ upper
110-
+ "+|[0-9]+");
108+
+ UPPER
109+
+ "+|\\d+");
111110

112111
static {
113112
String[] deburredLetters =
@@ -1460,7 +1459,7 @@ public static String deburr(final String string) {
14601459
public static List<String> words(final String string) {
14611460
final String localString = baseToString(string);
14621461
final List<String> result = new ArrayList<>();
1463-
final java.util.regex.Matcher matcher = reWords.matcher(localString);
1462+
final java.util.regex.Matcher matcher = RE_WORDS.matcher(localString);
14641463
while (matcher.find()) {
14651464
result.add(matcher.group());
14661465
}
@@ -2069,7 +2068,7 @@ public static long downloadUrl(final String url, final String fileName)
20692068
public static void decompressGzip(final String sourceFileName, final String targetFileName)
20702069
throws IOException {
20712070
try (GZIPInputStream gis =
2072-
new GZIPInputStream(new FileInputStream(new File(sourceFileName)))) {
2071+
new GZIPInputStream(new FileInputStream(sourceFileName))) {
20732072
Files.copy(gis, Paths.get(targetFileName));
20742073
}
20752074
}
@@ -2523,6 +2522,14 @@ public static <T> String join(final Iterable<T> iterable, final String separator
25232522
return Underscore.join(iterable, separator);
25242523
}
25252524

2525+
public static <T> String joinToString(final Iterable<T> iterable, final String separator,
2526+
final String prefix, final String postfix,
2527+
final int limit,
2528+
final String truncated,
2529+
final Function<T, String> transform) {
2530+
return Underscore.joinToString(iterable, separator, prefix, postfix, limit, truncated, transform);
2531+
}
2532+
25262533
public static String toJson(Collection collection) {
25272534
return Json.toJson(collection);
25282535
}
@@ -2877,7 +2884,7 @@ public static Map<String, Object> removeMinusesAndConvertNumbers(Map<String, Obj
28772884
} else {
28782885
newKey = entry.getKey();
28792886
}
2880-
if (!entry.getKey().equals(selfClosing)
2887+
if (!entry.getKey().equals(SELF_CLOSING)
28812888
&& !entry.getKey().equals("#omit-xml-declaration")) {
28822889
outMap.put(newKey, makeObject(entry.getValue()));
28832890
}
@@ -2955,7 +2962,7 @@ public static Map<String, Object> replaceSelfClosingWithEmpty(Map<String, Object
29552962
public static Object replaceSelfClosingWithValue(Map<String, Object> map, String value) {
29562963
Object outMap = new LinkedHashMap<>();
29572964
for (Map.Entry<String, Object> entry : map.entrySet()) {
2958-
if (selfClosing.equals(entry.getKey()) && "true".equals(entry.getValue())) {
2965+
if (SELF_CLOSING.equals(entry.getKey()) && "true".equals(entry.getValue())) {
29592966
if (map.size() == 1) {
29602967
outMap = value;
29612968
break;
@@ -3218,9 +3225,9 @@ public static Map<String, Object> replaceFirstLevel(Map<String, Object> map, int
32183225
}
32193226
if (level == 0 && Xml.XmlValue.getMapValue(outMap) instanceof Map) {
32203227
Map<String, Object> outMap2 = (Map<String, Object>) Xml.XmlValue.getMapValue(outMap);
3221-
if (selfClosing.equals(Xml.XmlValue.getMapKey(outMap2))
3228+
if (SELF_CLOSING.equals(Xml.XmlValue.getMapKey(outMap2))
32223229
&& "true".equals(Xml.XmlValue.getMapValue(outMap2))) {
3223-
outMap2.remove(selfClosing);
3230+
outMap2.remove(SELF_CLOSING);
32243231
}
32253232
return outMap2;
32263233
}
@@ -3249,11 +3256,11 @@ public static Map<String, Object> replaceNilWithNull(Map<String, Object> map) {
32493256
for (Map.Entry<String, Object> entry : map.entrySet()) {
32503257
Object outValue = makeReplaceNilWithNull(entry.getValue());
32513258
if (outValue instanceof Map
3252-
&& (nilKey.equals(Xml.XmlValue.getMapKey(outValue))
3259+
&& (NIL_KEY.equals(Xml.XmlValue.getMapKey(outValue))
32533260
|| Xml.XmlValue.getMapKey(outValue).endsWith(":nil"))
32543261
&& "true".equals(Xml.XmlValue.getMapValue(outValue))
3255-
&& ((Map) outValue).containsKey(selfClosing)
3256-
&& "true".equals(((Map) outValue).get(selfClosing))) {
3262+
&& ((Map) outValue).containsKey(SELF_CLOSING)
3263+
&& "true".equals(((Map) outValue).get(SELF_CLOSING))) {
32573264
outValue = null;
32583265
}
32593266
outMap.put(entry.getKey(), outValue);
@@ -3397,7 +3404,7 @@ public Builder addNull(final String key) {
33973404

33983405
@SuppressWarnings("unchecked")
33993406
public Map<String, Object> build() {
3400-
return (Map<String, Object>) ((LinkedHashMap) data).clone();
3407+
return (Map<String, Object>) ((LinkedHashMap<?, ?>) data).clone();
34013408
}
34023409

34033410
public String toXml() {
@@ -3503,13 +3510,13 @@ public ArrayBuilder add(final Builder builder) {
35033510

35043511
@SuppressWarnings("unchecked")
35053512
public ArrayBuilder merge(final List<Object> list) {
3506-
U.merge(data, (List<Object>) ((ArrayList) list).clone());
3513+
U.merge(data, (List<Object>) ((ArrayList<?>) list).clone());
35073514
return this;
35083515
}
35093516

35103517
@SuppressWarnings("unchecked")
35113518
public List<Object> build() {
3512-
return (List<Object>) ((ArrayList) data).clone();
3519+
return (List<Object>) ((ArrayList<?>) data).clone();
35133520
}
35143521

35153522
public String toXml() {

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

+42-10
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ public static <E> List<List<E>> partition(final Iterable<E> iterable, final Pred
12141214

12151215
@SuppressWarnings("unchecked")
12161216
public static <E> List<E>[] partition(final E[] iterable, final Predicate<E> pred) {
1217-
return partition(Arrays.asList(iterable), pred).toArray(new ArrayList[0]);
1217+
return partition(Arrays.asList(iterable), pred).toArray(new List[0]);
12181218
}
12191219

12201220
public T singleOrNull() {
@@ -1255,7 +1255,7 @@ public static <E> E first(final E... array) {
12551255
}
12561256

12571257
public static <E> List<E> first(final List<E> list, final int n) {
1258-
return list.subList(0, Math.min(n < 0 ? 0 : n, list.size()));
1258+
return list.subList(0, Math.min(Math.max(n, 0), list.size()));
12591259
}
12601260

12611261
public T first() {
@@ -1273,7 +1273,7 @@ public static <E> E first(final Iterable<E> iterable, final Predicate<E> pred) {
12731273
public static <E> List<E> first(
12741274
final Iterable<E> iterable, final Predicate<E> pred, final int n) {
12751275
List<E> list = filter(newArrayList(iterable), pred);
1276-
return list.subList(0, Math.min(n < 0 ? 0 : n, list.size()));
1276+
return list.subList(0, Math.min(Math.max(n, 0), list.size()));
12771277
}
12781278

12791279
public T first(final Predicate<T> pred) {
@@ -1482,7 +1482,7 @@ public static <E> List<E> compact(final List<E> list) {
14821482
!String.valueOf(arg).equals("null")
14831483
&& !String.valueOf(arg).equals("0")
14841484
&& !String.valueOf(arg).equals("false")
1485-
&& !String.valueOf(arg).equals(""));
1485+
&& !String.valueOf(arg).isEmpty());
14861486
}
14871487

14881488
@SuppressWarnings("unchecked")
@@ -1605,8 +1605,7 @@ public static <K, E> E[] distinctBy(final E[] array, final Function<E, K> func)
16051605
*/
16061606
@SuppressWarnings("unchecked")
16071607
public static <E> List<E> union(final List<E> list, final List<E>... lists) {
1608-
final Set<E> union = new LinkedHashSet<>();
1609-
union.addAll(list);
1608+
final Set<E> union = new LinkedHashSet<>(list);
16101609
for (List<E> localList : lists) {
16111610
union.addAll(localList);
16121611
}
@@ -2355,8 +2354,7 @@ public static List<String> methods(final Object object) {
23552354
*/
23562355
@SuppressWarnings("unchecked")
23572356
public static <K, V> Map<K, V> extend(final Map<K, V> destination, final Map<K, V>... sources) {
2358-
final Map<K, V> result = new LinkedHashMap<>();
2359-
result.putAll(destination);
2357+
final Map<K, V> result = new LinkedHashMap<>(destination);
23602358
for (final Map<K, V> source : sources) {
23612359
result.putAll(source);
23622360
}
@@ -2735,7 +2733,7 @@ public static <K, V> Template<Map<K, V>> template(final String template) {
27352733

27362734
public static String format(final String template, final Object... params) {
27372735
final java.util.regex.Matcher matcher = FORMAT_PATTERN.matcher(template);
2738-
final StringBuffer buffer = new StringBuffer();
2736+
final StringBuilder buffer = new StringBuilder();
27392737
int index = 0;
27402738
while (matcher.find()) {
27412739
if (matcher.group(1).isEmpty()) {
@@ -3320,6 +3318,40 @@ public static <T> String join(final Iterable<T> iterable, final String separator
33203318
return sb.toString();
33213319
}
33223320

3321+
public static <T> String joinToString(final Iterable<T> iterable, final String separator,
3322+
final String prefix, final String postfix,
3323+
final int limit,
3324+
final String truncated,
3325+
final Function<T, String> transform) {
3326+
final StringBuilder sb = new StringBuilder();
3327+
int index = 0;
3328+
if (prefix != null) {
3329+
sb.append(prefix);
3330+
}
3331+
for (final T item : iterable) {
3332+
if (index > 0) {
3333+
sb.append(separator);
3334+
}
3335+
index += 1;
3336+
if (limit < 0 || index <= limit) {
3337+
sb.append(transform == null ? item.toString() : transform.apply(item));
3338+
} else {
3339+
break;
3340+
}
3341+
}
3342+
joinToStringPostfix(postfix, limit, truncated, index, sb);
3343+
return sb.toString();
3344+
}
3345+
3346+
private static void joinToStringPostfix(String postfix, int limit, String truncated, int index, StringBuilder sb) {
3347+
if (limit >= 0 && index > limit) {
3348+
sb.append(truncated == null ? "..." : truncated);
3349+
}
3350+
if (postfix != null) {
3351+
sb.append(postfix);
3352+
}
3353+
}
3354+
33233355
public static <T> String join(final Iterable<T> iterable) {
33243356
return join(iterable, " ");
33253357
}
@@ -3492,7 +3524,7 @@ public static <T> List<List<T>> splitAt(final Iterable<T> iterable, final int po
34923524
if (position < 0) {
34933525
index = 0;
34943526
} else {
3495-
index = position > size ? size : position;
3527+
index = Math.min(position, size);
34963528
}
34973529
result.add(newArrayList(iterable).subList(0, index));
34983530
result.add(newArrayList(iterable).subList(index, size));

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ void defer() {
199199
return null;
200200
});
201201
assertEquals(0, counter[0].intValue(), "incr was debounced");
202-
await().atLeast(60, TimeUnit.MILLISECONDS)
202+
await().atLeast(90, TimeUnit.MILLISECONDS)
203203
.until(
204204
() -> {
205205
assertEquals(1, counter[0].intValue(), "incr was debounced");

0 commit comments

Comments
 (0)