Skip to content

Commit 35212a6

Browse files
committed
Sync with underscore-java
1 parent 22014db commit 35212a6

File tree

11 files changed

+147
-77
lines changed

11 files changed

+147
-77
lines changed

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

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
package com.github.underscore;
2525

26+
import org.w3c.dom.NodeList;
27+
2628
import java.io.FileInputStream;
2729
import java.io.FileOutputStream;
2830
import java.io.IOException;
@@ -63,10 +65,10 @@
6365
import java.util.function.Function;
6466
import java.util.function.Predicate;
6567
import java.util.zip.GZIPInputStream;
68+
6669
import javax.xml.xpath.XPath;
6770
import javax.xml.xpath.XPathConstants;
6871
import javax.xml.xpath.XPathFactory;
69-
import org.w3c.dom.NodeList;
7072

7173
@SuppressWarnings({
7274
"java:S135",
@@ -2859,21 +2861,25 @@ public static void jsonFolderToXml(
28592861
throws IOException {
28602862
Path sourceRoot = Paths.get(jsonFolder);
28612863
Path targetRoot = Paths.get(xmlFolder);
2862-
Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() {
2863-
@Override
2864-
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
2865-
covertJsonToXml(path, sourceRoot, targetRoot, identStep);
2866-
return FileVisitResult.CONTINUE;
2867-
}
2868-
});
2864+
Files.walkFileTree(
2865+
sourceRoot,
2866+
new SimpleFileVisitor<>() {
2867+
@Override
2868+
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)
2869+
throws IOException {
2870+
covertJsonToXml(path, sourceRoot, targetRoot, identStep);
2871+
return FileVisitResult.CONTINUE;
2872+
}
2873+
});
28692874
}
28702875

28712876
public static void jsonFolderToXml(String jsonFolder, String xmlFolder) throws IOException {
28722877
jsonFolderToXml(jsonFolder, xmlFolder, Xml.XmlStringBuilder.Step.TWO_SPACES);
28732878
}
28742879

2875-
public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot,
2876-
Xml.XmlStringBuilder.Step identStep) throws IOException {
2880+
public static void covertJsonToXml(
2881+
Path path, Path sourceRoot, Path targetRoot, Xml.XmlStringBuilder.Step identStep)
2882+
throws IOException {
28772883
Path relativePath = sourceRoot.relativize(path);
28782884
String fileName = relativePath.getFileName().toString();
28792885
String xmlFileName;
@@ -2887,6 +2893,43 @@ public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot,
28872893
fileJsonToXml(path.toAbsolutePath().toString(), targetPath.toString(), identStep);
28882894
}
28892895

2896+
public static void xmlFolderToJson(
2897+
String xmlFolder, String jsonFolder, Json.JsonStringBuilder.Step identStep)
2898+
throws IOException {
2899+
Path sourceRoot = Paths.get(xmlFolder);
2900+
Path targetRoot = Paths.get(jsonFolder);
2901+
Files.walkFileTree(
2902+
sourceRoot,
2903+
new SimpleFileVisitor<>() {
2904+
@Override
2905+
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)
2906+
throws IOException {
2907+
covertXmlToJson(path, sourceRoot, targetRoot, identStep);
2908+
return FileVisitResult.CONTINUE;
2909+
}
2910+
});
2911+
}
2912+
2913+
public static void xmlFolderToJson(String xmlFolder, String jsonFolder) throws IOException {
2914+
xmlFolderToJson(xmlFolder, jsonFolder, Json.JsonStringBuilder.Step.TWO_SPACES);
2915+
}
2916+
2917+
public static void covertXmlToJson(
2918+
Path path, Path sourceRoot, Path targetRoot, Json.JsonStringBuilder.Step identStep)
2919+
throws IOException {
2920+
Path relativePath = sourceRoot.relativize(path);
2921+
String fileName = relativePath.getFileName().toString();
2922+
String xmlFileName;
2923+
if (fileName.endsWith(".xml")) {
2924+
xmlFileName = fileName.substring(0, fileName.length() - 4) + ".json";
2925+
} else {
2926+
return;
2927+
}
2928+
Path targetPath = targetRoot.resolve(relativePath).getParent().resolve(xmlFileName);
2929+
Files.createDirectories(targetPath.getParent());
2930+
fileXmlToJson(path.toAbsolutePath().toString(), targetPath.toString(), identStep);
2931+
}
2932+
28902933
public static void streamJsonToXml(
28912934
InputStream jsonInputStream,
28922935
OutputStream xmlOutputStream,
@@ -2951,11 +2994,11 @@ public static String detectEncoding(byte[] buffer) {
29512994
case 0x3C000000:
29522995
encoding = "UTF_32LE";
29532996
break;
2954-
// <?
2997+
// <?
29552998
case 0x3C003F00:
29562999
encoding = "UnicodeLittleUnmarked";
29573000
break;
2958-
// <?xm
3001+
// <?xm
29593002
case 0x3C3F786D:
29603003
encoding = "UTF8";
29613004
break;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import static java.util.Arrays.asList;
27-
import static java.util.Collections.singletonList;
2826
import static org.junit.jupiter.api.Assertions.assertEquals;
2927
import static org.junit.jupiter.api.Assertions.assertNull;
3028
import static org.junit.jupiter.api.Assertions.assertThrows;
3129

30+
import static java.util.Arrays.asList;
31+
import static java.util.Collections.singletonList;
32+
33+
import org.junit.jupiter.api.Test;
34+
3235
import java.util.ArrayList;
3336
import java.util.Arrays;
3437
import java.util.Collection;
@@ -38,7 +41,6 @@
3841
import java.util.Map;
3942
import java.util.NoSuchElementException;
4043
import java.util.Set;
41-
import org.junit.jupiter.api.Test;
4244

4345
/**
4446
* Underscore library unit test.
@@ -383,8 +385,7 @@ void interpose() {
383385
assertEquals("[a]", new Underscore<>(singletonList("a")).interpose("interpose").toString());
384386
assertEquals("[a, b]", new Underscore<>(singletonList("a, b")).interpose(null).toString());
385387
assertEquals("[a]", Underscore.chain(singletonList("a")).interpose("interpose").toString());
386-
assertEquals(
387-
"[]", Underscore.chain(new ArrayList<>()).interpose("interpose").toString());
388+
assertEquals("[]", Underscore.chain(new ArrayList<>()).interpose("interpose").toString());
388389
assertEquals(
389390
"[a, b, c]", Underscore.chain(asList("a", "b", "c")).interpose(null).toString());
390391
assertEquals(
@@ -650,6 +651,7 @@ void lastOrNull() {
650651
new Underscore<>(Collections.<Integer>emptyList())
651652
.lastOrNull(item -> item % 2 == 0));
652653
}
654+
653655
/*
654656
_.compact([0, 1, false, 2, '', 3]);
655657
=> [1, 2, 3]

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import static java.util.Arrays.asList;
2726
import static org.junit.jupiter.api.Assertions.assertEquals;
2827

28+
import static java.util.Arrays.asList;
29+
30+
import org.junit.jupiter.api.Test;
31+
2932
import java.lang.reflect.Method;
3033
import java.util.ArrayList;
3134
import java.util.HashSet;
@@ -35,7 +38,6 @@
3538
import java.util.Set;
3639
import java.util.function.BiFunction;
3740
import java.util.function.Function;
38-
import org.junit.jupiter.api.Test;
3941

4042
/**
4143
* Underscore library unit test.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import static java.util.Arrays.asList;
2726
import static org.junit.jupiter.api.Assertions.assertEquals;
2827
import static org.junit.jupiter.api.Assertions.assertFalse;
2928
import static org.junit.jupiter.api.Assertions.assertThrows;
3029
import static org.junit.jupiter.api.Assertions.assertTrue;
3130

31+
import static java.util.Arrays.asList;
32+
33+
import org.junit.jupiter.api.Test;
34+
3235
import java.io.Serializable;
3336
import java.util.ArrayDeque;
3437
import java.util.ArrayList;
@@ -42,7 +45,6 @@
4245
import java.util.Optional;
4346
import java.util.Set;
4447
import java.util.function.Function;
45-
import org.junit.jupiter.api.Test;
4648

4749
/**
4850
* Underscore library unit test.

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import static java.util.Arrays.asList;
2726
import static org.awaitility.Awaitility.await;
2827
import static org.junit.jupiter.api.Assertions.assertEquals;
2928
import static org.junit.jupiter.api.Assertions.assertTrue;
3029

30+
import static java.util.Arrays.asList;
31+
32+
import org.junit.jupiter.api.Test;
33+
3134
import java.util.ArrayList;
3235
import java.util.Arrays;
3336
import java.util.LinkedHashMap;
@@ -39,7 +42,6 @@
3942
import java.util.function.Predicate;
4043
import java.util.function.Supplier;
4144
import java.util.function.UnaryOperator;
42-
import org.junit.jupiter.api.Test;
4345

4446
@SuppressWarnings("java:S2925")
4547
class FunctionsTest {
@@ -199,7 +201,7 @@ void defer() {
199201
return null;
200202
});
201203
assertEquals(0, counter[0].intValue(), "incr was debounced");
202-
await().atMost(400, TimeUnit.MILLISECONDS)
204+
await().atMost(600, TimeUnit.MILLISECONDS)
203205
.until(
204206
() -> {
205207
assertEquals(1, counter[0].intValue(), "incr was debounced");

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import static java.util.Arrays.asList;
27-
import static java.util.Collections.singletonList;
2826
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2927
import static org.junit.jupiter.api.Assertions.assertEquals;
3028
import static org.junit.jupiter.api.Assertions.assertNull;
3129
import static org.junit.jupiter.api.Assertions.assertThrows;
3230
import static org.junit.jupiter.api.Assertions.assertTrue;
3331

32+
import static java.util.Arrays.asList;
33+
import static java.util.Collections.singletonList;
34+
35+
import org.junit.jupiter.api.Test;
36+
3437
import java.io.IOException;
3538
import java.net.URISyntaxException;
3639
import java.nio.file.Files;
@@ -45,7 +48,6 @@
4548
import java.util.LinkedHashSet;
4649
import java.util.List;
4750
import java.util.Map;
48-
import org.junit.jupiter.api.Test;
4951

5052
/**
5153
* Underscore library unit test.
@@ -687,14 +689,14 @@ void fetchResponseBlob() {
687689

688690
@Test
689691
void fetchGetHttps() {
690-
U.FetchResponse result = U.fetch("https://api.lob.com/v1/addresses");
692+
U.FetchResponse result =
693+
U.fetch(
694+
"https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json");
691695
assertEquals(
692696
"{\n"
693-
+ " \"error\": {\n"
694-
+ " \"message\": \"Missing authentication\",\n"
695-
+ " \"status_code\": 401,\n"
696-
+ " \"code\": \"unauthorized\"\n"
697-
+ " }\n"
697+
+ " \"fruit\": \"Apple\",\n"
698+
+ " \"size\": \"Large\",\n"
699+
+ " \"color\": \"Red\"\n"
698700
+ "}",
699701
result.text());
700702
}
@@ -2010,10 +2012,7 @@ void chainMap() {
20102012
.toString());
20112013
assertEquals(
20122014
"{name1=one, name2=two, 1=2}",
2013-
com.github
2014-
.underscore
2015-
.U
2016-
.chain(
2015+
com.github.underscore.U.chain(
20172016
new LinkedHashMap<>() {
20182017
{
20192018
put("name1", "one");
@@ -2025,10 +2024,7 @@ void chainMap() {
20252024
.toString());
20262025
assertEquals(
20272026
"{name1=one, name2=two, 1=2}",
2028-
com.github
2029-
.underscore
2030-
.U
2031-
.of(
2027+
com.github.underscore.U.of(
20322028
new LinkedHashMap<>() {
20332029
{
20342030
put("name1", "one");

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import static java.util.Arrays.asList;
2726
import static org.junit.jupiter.api.Assertions.assertEquals;
2827
import static org.junit.jupiter.api.Assertions.assertNull;
2928
import static org.junit.jupiter.api.Assertions.assertThrows;
3029

30+
import static java.util.Arrays.asList;
31+
32+
import org.junit.jupiter.api.Test;
33+
3134
import java.math.BigDecimal;
3235
import java.math.BigInteger;
3336
import java.util.ArrayList;
3437
import java.util.Collections;
3538
import java.util.HashSet;
3639
import java.util.List;
3740
import java.util.function.Function;
38-
import org.junit.jupiter.api.Test;
3941

4042
/**
4143
* Underscore library unit test.
@@ -222,8 +224,7 @@ void sum() {
222224
assertEquals("14.2", result12.toString());
223225
final Integer result13 = U.sum(asList(-1, -2, -3));
224226
assertEquals("-6", result13.toString());
225-
final Integer resultChain =
226-
(Integer) U.chain(asList(1, 2, 3)).sum().item();
227+
final Integer resultChain = (Integer) U.chain(asList(1, 2, 3)).sum().item();
227228
assertEquals("6", resultChain.toString());
228229
final Integer result14 = U.sum(new Integer[] {1, 2, 3});
229230
assertEquals("6", result14.toString());
@@ -249,8 +250,7 @@ void sum() {
249250
assertEquals("6", result20.toString());
250251
final Integer result21 = U.sum(new Integer[] {1, 2, null});
251252
assertEquals("3", result21.toString());
252-
final Integer resultChainFunc =
253-
U.chain(asList(1, 2, 3)).sum(item -> item * 2).item();
253+
final Integer resultChainFunc = U.chain(asList(1, 2, 3)).sum(item -> item * 2).item();
254254
assertEquals("12", resultChainFunc.toString());
255255
final Number resultObj = new U(asList(1, 2, 3)).sum();
256256
assertEquals("6", resultObj.toString());
@@ -350,8 +350,7 @@ void mean() {
350350
assertEquals("0.5", result.toString());
351351
final Double resultObj = new U(asList((double) 0, 0.5, (double) 1)).mean();
352352
assertEquals("0.5", resultObj.toString());
353-
final Double resultChain =
354-
U.chain(asList((double) 0, 0.5, (double) 1)).mean().item();
353+
final Double resultChain = U.chain(asList((double) 0, 0.5, (double) 1)).mean().item();
355354
assertEquals("0.5", resultChain.toString());
356355
final Double result2 = U.mean(asList((long) 0, (long) 1, (long) 2));
357356
assertEquals("1.0", result2.toString());
@@ -374,18 +373,15 @@ void mean() {
374373
void median() {
375374
final Double result = U.median(asList(0, 0, 0, 0, 5));
376375
assertEquals("0.0", result.toString());
377-
final Double resultObj =
378-
new U<>(asList(0, 0, 0, 0, 5)).median();
376+
final Double resultObj = new U<>(asList(0, 0, 0, 0, 5)).median();
379377
assertEquals("0.0", resultObj.toString());
380-
final Double resultChain =
381-
U.chain(asList(0, 0, 0, 0, 5)).median().item();
378+
final Double resultChain = U.chain(asList(0, 0, 0, 0, 5)).median().item();
382379
assertEquals("0.0", resultChain.toString());
383380
final Double result2 = U.median(asList(0, 0, 1, 2, 5));
384381
assertEquals("1.0", result2.toString());
385382
final Double result3 = U.median(asList(0, 0, 1, 2));
386383
assertEquals("0.5", result3.toString());
387-
final Double result4 =
388-
U.median(asList(0, 0, 1, 2, 3, 4));
384+
final Double result4 = U.median(asList(0, 0, 1, 2, 3, 4));
389385
assertEquals("1.5", result4.toString());
390386
}
391387

0 commit comments

Comments
 (0)