Skip to content

Commit a50c00b

Browse files
committed
Sync with underscore-java
1 parent 837e9d8 commit a50c00b

File tree

3 files changed

+14
-192
lines changed

3 files changed

+14
-192
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030

3131
public final class Optional<T> {
3232
private static final Optional<?> EMPTY = new Optional<>();
33-
private final T arg;
33+
private final T value;
3434
private final boolean absent;
3535

3636
private Optional() {
37-
this.arg = null;
37+
this.value = null;
3838
this.absent = true;
3939
}
4040

41-
private Optional(final T arg) {
42-
this.arg = arg;
41+
private Optional(final T value) {
42+
this.value = value;
4343
this.absent = false;
4444
}
4545

@@ -60,21 +60,21 @@ public T get() {
6060
if (absent) {
6161
throw new IllegalStateException("Optional.get() cannot be called on an empty value");
6262
}
63-
return arg;
63+
return value;
6464
}
6565

6666
public T or(final T defaultValue) {
6767
if (absent) {
6868
return defaultValue;
6969
}
70-
return arg;
70+
return value;
7171
}
7272

7373
public T orNull() {
7474
if (absent) {
7575
return null;
7676
}
77-
return arg;
77+
return value;
7878
}
7979

8080
public boolean isEmpty() {
@@ -88,7 +88,7 @@ public boolean isPresent() {
8888
public Optional<T> filter(Predicate<? super T> predicate) {
8989
Underscore.checkNotNull(predicate);
9090
if (isPresent()) {
91-
return predicate.test(arg) ? this : Optional.empty();
91+
return predicate.test(value) ? this : Optional.empty();
9292
} else {
9393
return this;
9494
}
@@ -97,7 +97,7 @@ public Optional<T> filter(Predicate<? super T> predicate) {
9797
public <F> Optional<F> map(Function<? super T, F> mapper) {
9898
Underscore.checkNotNull(mapper);
9999
if (isPresent()) {
100-
return Optional.fromNullable(mapper.apply(arg));
100+
return Optional.fromNullable(mapper.apply(value));
101101
} else {
102102
return empty();
103103
}
@@ -107,12 +107,12 @@ public <X extends Throwable> T orThrow(Supplier<? extends X> exceptionFunction)
107107
if (absent) {
108108
throw exceptionFunction.get();
109109
} else {
110-
return arg;
110+
return value;
111111
}
112112
}
113113

114114
public java.util.Optional<T> toJavaOptional() {
115-
return java.util.Optional.ofNullable(arg);
115+
return java.util.Optional.ofNullable(value);
116116
}
117117

118118
@Override
@@ -126,18 +126,18 @@ public boolean equals(final Object o) {
126126

127127
final Optional<?> optional = (Optional<?>) o;
128128

129-
return absent == optional.absent && Objects.equals(arg, optional.arg);
129+
return absent == optional.absent && Objects.equals(value, optional.value);
130130
}
131131

132132
@Override
133133
public int hashCode() {
134-
int result = arg == null ? 0 : arg.hashCode();
134+
int result = value == null ? 0 : value.hashCode();
135135
result = 31 * result + (absent ? 1 : 0);
136136
return result;
137137
}
138138

139139
@Override
140140
public String toString() {
141-
return absent ? "Optional.empty" : "Optional[" + arg + "]";
141+
return absent ? "Optional.empty" : "Optional[" + value + "]";
142142
}
143143
}

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

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -3801,129 +3801,6 @@ public static <T> Predicate<T> or(
38013801
};
38023802
}
38033803

3804-
public static int minimumDays(int rows, int columns, List<List<Integer>> grid) {
3805-
Queue<int[]> queue = new LinkedList<>();
3806-
int cnt = 0;
3807-
for (int i = 0; i < rows; i++) {
3808-
for (int j = 0; j < columns; j++) {
3809-
if (grid.get(i).get(j) == 1) {
3810-
queue.offer(new int[] {i, j});
3811-
cnt++;
3812-
}
3813-
}
3814-
}
3815-
return getInteger(rows, columns, grid, queue, cnt);
3816-
}
3817-
3818-
private static int getInteger(
3819-
int rows, int columns, List<List<Integer>> grid, Queue<int[]> queue, int cnt) {
3820-
int target = rows * columns;
3821-
int res = 0;
3822-
int localCnt = cnt;
3823-
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
3824-
while (!queue.isEmpty()) {
3825-
int size = queue.size();
3826-
if (localCnt == target) {
3827-
return res;
3828-
}
3829-
for (int i = 0; i < size; i++) {
3830-
int[] cur = queue.poll();
3831-
for (int[] dir : dirs) {
3832-
int ni = cur[0] + dir[0];
3833-
int nj = cur[1] + dir[1];
3834-
if (ni >= 0
3835-
&& ni < rows
3836-
&& nj >= 0
3837-
&& nj < columns
3838-
&& grid.get(ni).get(nj) == 0) {
3839-
localCnt++;
3840-
queue.offer(new int[] {ni, nj});
3841-
grid.get(ni).set(nj, 1);
3842-
}
3843-
}
3844-
}
3845-
res++;
3846-
}
3847-
return -1;
3848-
}
3849-
3850-
public static List<String> topNCompetitors(
3851-
int numCompetitors,
3852-
int topNCompetitors,
3853-
List<String> competitors,
3854-
int numReviews,
3855-
List<String> reviews) {
3856-
if (Underscore.isNull(reviews)
3857-
|| reviews.isEmpty()
3858-
|| Underscore.isNull(competitors)
3859-
|| competitors.isEmpty()
3860-
|| numReviews < 1
3861-
|| numCompetitors < 1) {
3862-
return new ArrayList<>();
3863-
}
3864-
3865-
List<String> topNCompetitorsList = new ArrayList<>(topNCompetitors);
3866-
3867-
Set<String> competitorsSet = new HashSet<>(competitors);
3868-
Map<String, Integer> topCompetitorsMap = new HashMap<>();
3869-
List<Map.Entry<String, Integer>> list =
3870-
getEntries(reviews, competitorsSet, topCompetitorsMap);
3871-
3872-
for (Map.Entry<String, Integer> item : list) {
3873-
if (topNCompetitorsList.size() < topNCompetitors) {
3874-
topNCompetitorsList.add(item.getKey());
3875-
} else {
3876-
break;
3877-
}
3878-
}
3879-
3880-
return topNCompetitorsList;
3881-
}
3882-
3883-
private static List<Map.Entry<String, Integer>> getEntries(
3884-
List<String> reviews,
3885-
Set<String> competitorsSet,
3886-
Map<String, Integer> topCompetitorsMap) {
3887-
// clean the reviews first: lowercase, remove special characters and split by spaces.
3888-
for (String review : reviews) {
3889-
String[] reviewArray = review.toLowerCase().replaceAll("[^a-zA-Z0-9 ]", "").split(" ");
3890-
Set<String> tempCompetitorSet = new HashSet<>();
3891-
3892-
for (String text : reviewArray) {
3893-
if (competitorsSet.contains(text) && !tempCompetitorSet.contains(text)) {
3894-
tempCompetitorSet.add(text);
3895-
if (topCompetitorsMap.containsKey(text)) {
3896-
topCompetitorsMap.put(text, topCompetitorsMap.get(text) + 1);
3897-
} else {
3898-
topCompetitorsMap.put(text, 1);
3899-
}
3900-
}
3901-
}
3902-
}
3903-
3904-
return getEntries(topCompetitorsMap);
3905-
}
3906-
3907-
private static List<Map.Entry<String, Integer>> getEntries(
3908-
Map<String, Integer> topCompetitorsMap) {
3909-
List<Map.Entry<String, Integer>> list = new ArrayList<>(topCompetitorsMap.entrySet());
3910-
list.sort(new ValueThenKeyComparator<>());
3911-
return list;
3912-
}
3913-
3914-
public static class ValueThenKeyComparator<
3915-
K extends Comparable<? super K>, V extends Comparable<? super V>>
3916-
implements Comparator<Map.Entry<K, V>>, java.io.Serializable {
3917-
3918-
public int compare(Map.Entry<K, V> a, Map.Entry<K, V> b) {
3919-
int cmp1 = b.getValue().compareTo(a.getValue());
3920-
if (cmp1 == 0) {
3921-
return a.getKey().compareTo(b.getKey());
3922-
}
3923-
return cmp1;
3924-
}
3925-
}
3926-
39273804
public static void main(String... args) {
39283805
final String message =
39293806
"Underscore-java17 is a java 17 port of Underscore.js.\n\n"

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

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -445,61 +445,6 @@ void format() {
445445
assertEquals("hello: mo\\e", fortmatted6);
446446
}
447447

448-
@Test
449-
void minimumDays() {
450-
List<List<Integer>> ll = new ArrayList<>();
451-
ll.add(Arrays.asList(1, 1, 1, 1, 1));
452-
ll.add(Arrays.asList(1, 1, 1, 0, 1));
453-
ll.add(Arrays.asList(1, 0, 1, 1, 1));
454-
ll.add(Arrays.asList(1, 1, 1, 1, 1));
455-
assertEquals(1, Underscore.minimumDays(4, 5, ll));
456-
List<List<Integer>> ll2 = new ArrayList<>();
457-
ll2.add(Arrays.asList(0, 0, 0, 0, 0));
458-
ll2.add(Arrays.asList(0, 0, 0, 0, 0));
459-
ll2.add(Arrays.asList(0, 0, 0, 0, 0));
460-
ll2.add(Arrays.asList(0, 0, 0, 0, 0));
461-
assertEquals(-1, Underscore.minimumDays(4, 5, ll2));
462-
}
463-
464-
@Test
465-
void topNCompetitors() {
466-
List<String> competitors =
467-
Arrays.asList("anacell", "betacellular", "cetracular", "deltacellular", "eurocell");
468-
List<String> reviews =
469-
Arrays.asList(
470-
"I love anacell Best services provided by anacell in the town",
471-
"betacellular has great services",
472-
"deltacellular provides much betacellular",
473-
"cetracular is worse than eurocell",
474-
"betacellular is better than deltacellular");
475-
List<String> strings = Underscore.topNCompetitors(6, 2, competitors, 6, reviews);
476-
assertEquals("[betacellular, deltacellular]", strings.toString());
477-
List<String> competitors2 = Arrays.asList("ААА", "БББ");
478-
List<String> reviews2 =
479-
Arrays.asList(
480-
"I love anacell Best services provided by anacell in the town",
481-
"betacellular has great services",
482-
"deltacellular provides much betacellular",
483-
"cetracular is worse than eurocell",
484-
"betacellular is better than deltacellular");
485-
List<String> strings2 = Underscore.topNCompetitors(2, 2, competitors2, 6, reviews2);
486-
assertEquals("[]", strings2.toString());
487-
List<String> strings3 = Underscore.topNCompetitors(2, 2, competitors2, 6, null);
488-
assertEquals("[]", strings3.toString());
489-
List<String> strings4 =
490-
Underscore.topNCompetitors(2, 2, competitors2, 6, Collections.<String>emptyList());
491-
assertEquals("[]", strings4.toString());
492-
List<String> strings5 = Underscore.topNCompetitors(2, 2, null, 6, reviews2);
493-
assertEquals("[]", strings5.toString());
494-
List<String> strings6 =
495-
Underscore.topNCompetitors(2, 2, Collections.<String>emptyList(), 6, reviews2);
496-
assertEquals("[]", strings6.toString());
497-
List<String> strings7 = Underscore.topNCompetitors(0, 2, competitors2, 6, reviews2);
498-
assertEquals("[]", strings7.toString());
499-
List<String> strings8 = Underscore.topNCompetitors(2, 2, competitors2, 0, reviews2);
500-
assertEquals("[]", strings8.toString());
501-
}
502-
503448
/*
504449
var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};
505450
_.result(object, 'cheese');

0 commit comments

Comments
 (0)