Skip to content
This repository was archived by the owner on May 26, 2021. It is now read-only.

Commit 94efb56

Browse files
committed
Sync with underscore-java.
1 parent 34686bb commit 94efb56

File tree

3 files changed

+176
-12
lines changed

3 files changed

+176
-12
lines changed

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

+109-3
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,16 @@ public String apply(Map<K, V> value) {
136136
final String escape = TEMPLATE_SETTINGS.get(ESCAPE);
137137
String result = template;
138138
for (final Map.Entry<K, V> element : value.entrySet()) {
139+
final String value1 = String.valueOf(element.getValue()).replace("\\", "\\\\");
139140
result = java.util.regex.Pattern.compile(interpolate.replace(ALL_SYMBOLS,
140141
S_Q + element.getKey()
141-
+ E_S)).matcher(result).replaceAll(String.valueOf(element.getValue()));
142+
+ E_S)).matcher(result).replaceAll(value1);
142143
result = java.util.regex.Pattern.compile(escape.replace(ALL_SYMBOLS,
143144
S_Q + element.getKey()
144-
+ E_S)).matcher(result).replaceAll(escape(String.valueOf(element.getValue())));
145+
+ E_S)).matcher(result).replaceAll(escape(value1));
145146
result = java.util.regex.Pattern.compile(evaluate.replace(ALL_SYMBOLS,
146147
S_Q + element.getKey()
147-
+ E_S)).matcher(result).replaceAll(String.valueOf(element.getValue()));
148+
+ E_S)).matcher(result).replaceAll(value1);
148149
}
149150
return result;
150151
}
@@ -3748,6 +3749,111 @@ public boolean test(T value) {
37483749
};
37493750
}
37503751

3752+
public static int minimumDays(int rows, int columns, List<List<Integer>> grid) {
3753+
Queue<int[]> queue = new LinkedList<int[]>();
3754+
int cnt = 0;
3755+
for (int i = 0; i < rows; i++) {
3756+
for (int j = 0; j < columns; j++) {
3757+
if (grid.get(i).get(j) == 1) {
3758+
queue.offer(new int[] {i, j});
3759+
cnt++;
3760+
}
3761+
}
3762+
}
3763+
return getInteger(rows, columns, grid, queue, cnt);
3764+
}
3765+
3766+
private static int getInteger(int rows, int columns, List<List<Integer>> grid, Queue<int[]> queue, int cnt) {
3767+
int target = rows * columns;
3768+
int res = 0;
3769+
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
3770+
while (!queue.isEmpty()) {
3771+
int size = queue.size();
3772+
if (cnt == target) {
3773+
return res;
3774+
}
3775+
for (int i = 0; i < size; i++) {
3776+
int[] cur = queue.poll();
3777+
for (int[] dir : dirs) {
3778+
int ni = cur[0] + dir[0];
3779+
int nj = cur[1] + dir[1];
3780+
if (ni >= 0 && ni < rows && nj >= 0 && nj < columns && grid.get(ni).get(nj) == 0) {
3781+
cnt++;
3782+
queue.offer(new int[] {ni, nj});
3783+
grid.get(ni).set(nj, 1);
3784+
}
3785+
}
3786+
}
3787+
res++;
3788+
}
3789+
return -1;
3790+
}
3791+
3792+
public static List<String> topNCompetitors(int numCompetitors, int topNCompetitors, List<String> competitors,
3793+
int numReviews, List<String> reviews) {
3794+
if (U.isNull(reviews) || reviews.isEmpty() || U.isNull(competitors) || competitors.isEmpty()
3795+
|| numReviews < 1 || numCompetitors < 1) {
3796+
return new ArrayList<String>();
3797+
}
3798+
3799+
List<String> topNCompetitorsList = new ArrayList<String>(topNCompetitors);
3800+
3801+
Set<String> competitorsSet = new HashSet<String>(competitors);
3802+
Map<String, Integer> topCompetitorsMap = new HashMap<String, Integer>();
3803+
List<Map.Entry<String, Integer>> list = getEntries(reviews, competitorsSet, topCompetitorsMap);
3804+
3805+
for (Map.Entry<String, Integer> item : list) {
3806+
if (topNCompetitorsList.size() < topNCompetitors) {
3807+
topNCompetitorsList.add(item.getKey());
3808+
} else {
3809+
break;
3810+
}
3811+
}
3812+
3813+
return topNCompetitorsList;
3814+
}
3815+
3816+
private static List<Map.Entry<String, Integer>> getEntries(List<String> reviews, Set<String> competitorsSet,
3817+
Map<String, Integer> topCompetitorsMap) {
3818+
// clean the reviews first: lowercase, remove special characters and split by spaces.
3819+
for (String review : reviews) {
3820+
String[] reviewArray = review.toLowerCase().replaceAll("[^a-zA-Z0-9 ]", "").split(" ");
3821+
Set<String> tempCompetitorSet = new HashSet<String>();
3822+
3823+
for (String text : reviewArray) {
3824+
if (competitorsSet.contains(text) && !tempCompetitorSet.contains(text)) {
3825+
tempCompetitorSet.add(text);
3826+
if (topCompetitorsMap.containsKey(text)) {
3827+
topCompetitorsMap.put(text, topCompetitorsMap.get(text) + 1);
3828+
} else {
3829+
topCompetitorsMap.put(text, 1);
3830+
}
3831+
}
3832+
}
3833+
}
3834+
3835+
return getEntries(topCompetitorsMap);
3836+
}
3837+
3838+
private static List<Map.Entry<String, Integer>> getEntries(Map<String, Integer> topCompetitorsMap) {
3839+
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(topCompetitorsMap.entrySet());
3840+
Collections.sort(list, new ValueThenKeyComparator<String, Integer>());
3841+
return list;
3842+
}
3843+
3844+
public static class ValueThenKeyComparator<K extends Comparable<? super K>,
3845+
V extends Comparable<? super V>> implements Comparator<Map.Entry<K, V>> {
3846+
3847+
public int compare(Map.Entry<K, V> a, Map.Entry<K, V> b) {
3848+
int cmp1 = b.getValue().compareTo(a.getValue());
3849+
if (cmp1 != 0) {
3850+
return cmp1;
3851+
} else {
3852+
return a.getKey().compareTo(b.getKey());
3853+
}
3854+
}
3855+
}
3856+
37513857
public static void main(String ... args) {
37523858
final String message = "Underscore-java8 is a java 8 port of Underscore.js.\n\n"
37533859
+ "In addition to porting Underscore's functionality, Underscore-java includes matching unit tests.\n\n"

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

+16-8
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,19 @@ public void sort() {
6464
assertEquals("[example, some, words]", new U(asList("some", "words", "example")).sort().toString());
6565
assertEquals("[example, some, words]", U.chain(asList("some", "words", "example")).sort().value().toString());
6666
assertEquals("[4, 5, 7]", U.chain(asList("some", "words", "example"))
67-
.map(new Function<String, Integer>() {
68-
public Integer apply(String arg) {
69-
return arg.length();
70-
}
71-
}).sort().value().toString());
72-
assertEquals("[example, some, words]", asList(U.sort(new String[] {"some", "words", "example"})).toString());
67+
.map(String::length).sort().value().toString());
68+
assertEquals("[example, some, words]", asList(U.sort("some", "words", "example")).toString());
69+
final List<Map<String, Object>> stooges = new ArrayList<Map<String, Object>>() { {
70+
add(new LinkedHashMap<String, Object>() { { put("name", "curly"); put("age", 25); } });
71+
add(new LinkedHashMap<String, Object>() { { put("name", "moe"); put("age", 21); } });
72+
add(new LinkedHashMap<String, Object>() { { put("name", "larry"); put("age", 23); } });
73+
} };
74+
final String youngest = U.chain(stooges)
75+
.sortBy(
76+
item -> (Integer) item.get("age"))
77+
.map(
78+
item -> item.get("name") + " is " + item.get("age"))
79+
.first().item();
7380
}
7481

7582
/*
@@ -109,6 +116,7 @@ public void push() {
109116
@SuppressWarnings("unchecked")
110117
@Test
111118
public void pop() {
119+
System.out.println("York S1-VG1245CN VALVE,BALL,1.0\"\",2W".replaceAll("\"", "\"\""));
112120
assertEquals("c", U.pop(asList("a", "b", "c")).fst().toString());
113121
assertEquals("c", new U(asList("a", "b", "c")).pop().fst().toString());
114122
assertEquals("c", U.chain(asList("a", "b", "c")).pop().item().fst().toString());
@@ -121,9 +129,9 @@ public void pop() {
121129
@SuppressWarnings("unchecked")
122130
@Test
123131
public void shift() {
124-
assertEquals("a", U.shift(asList("a", "b", "c")).fst().toString());
132+
assertEquals("a", U.shift(asList("a", "b", "c")).fst());
125133
assertEquals("a", new U(asList("a", "b", "c")).shift().fst().toString());
126-
assertEquals("a", U.chain(asList("a", "b", "c")).shift().item().fst().toString());
134+
assertEquals("a", U.chain(asList("a", "b", "c")).shift().item().fst());
127135
}
128136

129137
/*

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

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2019 Valentyn Kolesnikov
4+
* Copyright 2015-2020 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -296,6 +296,56 @@ public void format() {
296296
assertEquals("hello: moe, 123", fortmatted3);
297297
String fortmatted4 = U.format("hello: {1}, {0}", "moe", 123);
298298
assertEquals("hello: 123, moe", fortmatted4);
299+
String fortmatted5 = U.format("hello: {0}", "mo/e");
300+
assertEquals("hello: mo/e", fortmatted5);
301+
String fortmatted6 = U.format("hello: {0}", "mo\\e");
302+
assertEquals("hello: mo\\e", fortmatted6);
303+
}
304+
305+
@Test
306+
public void minimumDays() {
307+
List<List<Integer>> ll = new ArrayList<List<Integer>>();
308+
ll.add(Arrays.asList(1, 1, 1, 1, 1));
309+
ll.add(Arrays.asList(1, 1, 1, 0, 1));
310+
ll.add(Arrays.asList(1, 0, 1, 1, 1));
311+
ll.add(Arrays.asList(1, 1, 1, 1, 1));
312+
assertEquals(1, U.minimumDays(4, 5, ll));
313+
List<List<Integer>> ll2 = new ArrayList<List<Integer>>();
314+
ll2.add(Arrays.asList(0, 0, 0, 0, 0));
315+
ll2.add(Arrays.asList(0, 0, 0, 0, 0));
316+
ll2.add(Arrays.asList(0, 0, 0, 0, 0));
317+
ll2.add(Arrays.asList(0, 0, 0, 0, 0));
318+
assertEquals(-1, U.minimumDays(4, 5, ll2));
319+
}
320+
321+
@Test
322+
public void topNCompetitors() {
323+
List<String> competitors = Arrays.asList("anacell", "betacellular", "cetracular", "deltacellular", "eurocell");
324+
List<String> reviews = Arrays.asList("I love anacell Best services provided by anacell in the town",
325+
"betacellular has great services",
326+
"deltacellular provides much betacellular", "cetracular is worse than eurocell",
327+
"betacellular is better than deltacellular");
328+
List<String> strings = U.topNCompetitors(6, 2, competitors, 6, reviews);
329+
assertEquals("[betacellular, deltacellular]", strings.toString());
330+
List<String> competitors2 = Arrays.asList("ААА", "БББ");
331+
List<String> reviews2 = Arrays.asList("I love anacell Best services provided by anacell in the town",
332+
"betacellular has great services",
333+
"deltacellular provides much betacellular", "cetracular is worse than eurocell",
334+
"betacellular is better than deltacellular");
335+
List<String> strings2 = U.topNCompetitors(2, 2, competitors2, 6, reviews2);
336+
assertEquals("[]", strings2.toString());
337+
List<String> strings3 = U.topNCompetitors(2, 2, competitors2, 6, null);
338+
assertEquals("[]", strings3.toString());
339+
List<String> strings4 = U.topNCompetitors(2, 2, competitors2, 6, Arrays.<String>asList());
340+
assertEquals("[]", strings4.toString());
341+
List<String> strings5 = U.topNCompetitors(2, 2, null, 6, reviews2);
342+
assertEquals("[]", strings5.toString());
343+
List<String> strings6 = U.topNCompetitors(2, 2, Arrays.<String>asList(), 6, reviews2);
344+
assertEquals("[]", strings6.toString());
345+
List<String> strings7 = U.topNCompetitors(0, 2, competitors2, 6, reviews2);
346+
assertEquals("[]", strings7.toString());
347+
List<String> strings8 = U.topNCompetitors(2, 2, competitors2, 0, reviews2);
348+
assertEquals("[]", strings8.toString());
299349
}
300350

301351
/*

0 commit comments

Comments
 (0)