Skip to content

Commit 7a03c8b

Browse files
authored
Replaced class Tuple with Map.Entry
1 parent 3638b2f commit 7a03c8b

File tree

10 files changed

+126
-172
lines changed

10 files changed

+126
-172
lines changed

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

Lines changed: 0 additions & 52 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,12 @@ public Chain<Object> pluck(final String propertyName) {
458458
}
459459

460460
@Override
461-
public <E> Chain<T> where(final List<Tuple<String, E>> properties) {
461+
public <E> Chain<T> where(final List<Map.Entry<String, E>> properties) {
462462
return new Chain<>(Underscore.where(value(), properties));
463463
}
464464

465465
@Override
466-
public <E> Chain<Optional<T>> findWhere(final List<Tuple<String, E>> properties) {
466+
public <E> Chain<Optional<T>> findWhere(final List<Map.Entry<String, E>> properties) {
467467
return new Chain<>(Underscore.findWhere(value(), properties));
468468
}
469469

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

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,28 @@ public static void templateSettings(final Map<String, String> templateSettings)
118118
}
119119

120120
private static final class WherePredicate<E, T> implements Predicate<E> {
121-
private final List<Tuple<String, T>> properties;
121+
private final List<Map.Entry<String, T>> properties;
122122

123-
private WherePredicate(List<Tuple<String, T>> properties) {
123+
private WherePredicate(List<Map.Entry<String, T>> properties) {
124124
this.properties = properties;
125125
}
126126

127127
@Override
128128
public boolean test(final E elem) {
129-
for (Tuple<String, T> prop : properties) {
129+
for (Map.Entry<String, T> prop : properties) {
130130
try {
131-
if (!elem.getClass().getField(prop.fst()).get(elem).equals(prop.snd())) {
131+
if (!elem.getClass()
132+
.getField(prop.getKey())
133+
.get(elem)
134+
.equals(prop.getValue())) {
132135
return false;
133136
}
134137
} catch (Exception ex) {
135138
try {
136139
if (!elem.getClass()
137-
.getMethod(prop.fst())
140+
.getMethod(prop.getKey())
138141
.invoke(elem)
139-
.equals(prop.snd())) {
142+
.equals(prop.getValue())) {
140143
return false;
141144
}
142145
} catch (Exception ignored) {
@@ -856,27 +859,28 @@ public static <E> Set<Object> pluck(final Set<E> set, final String propertyName)
856859
* Documented, #where
857860
*/
858861
public static <T, E> List<E> where(
859-
final List<E> list, final List<Tuple<String, T>> properties) {
862+
final List<E> list, final List<Map.Entry<String, T>> properties) {
860863
return filter(list, new WherePredicate<>(properties));
861864
}
862865

863-
public <E> List<T> where(final List<Tuple<String, E>> properties) {
866+
public <E> List<T> where(final List<Map.Entry<String, E>> properties) {
864867
return where(newArrayList(iterable), properties);
865868
}
866869

867-
public static <T, E> Set<E> where(final Set<E> set, final List<Tuple<String, T>> properties) {
870+
public static <T, E> Set<E> where(
871+
final Set<E> set, final List<Map.Entry<String, T>> properties) {
868872
return filter(set, new WherePredicate<>(properties));
869873
}
870874

871875
/*
872876
* Documented, #findWhere
873877
*/
874878
public static <T, E> Optional<E> findWhere(
875-
final Iterable<E> iterable, final List<Tuple<String, T>> properties) {
879+
final Iterable<E> iterable, final List<Map.Entry<String, T>> properties) {
876880
return find(iterable, new WherePredicate<>(properties));
877881
}
878882

879-
public <E> Optional<T> findWhere(final List<Tuple<String, E>> properties) {
883+
public <E> Optional<T> findWhere(final List<Map.Entry<String, E>> properties) {
880884
return findWhere(iterable, properties);
881885
}
882886

@@ -1122,10 +1126,10 @@ public <K, V> Map<K, V> toMap() {
11221126
return toMap((Iterable<Map.Entry<K, V>>) iterable);
11231127
}
11241128

1125-
public static <K, V> Map<K, V> toMap(final List<Tuple<K, V>> tuples) {
1129+
public static <K, V> Map<K, V> toMap(final List<Map.Entry<K, V>> tuples) {
11261130
final Map<K, V> result = newLinkedHashMap();
1127-
for (final Tuple<K, V> entry : tuples) {
1128-
result.put(entry.fst(), entry.snd());
1131+
for (final Map.Entry<K, V> entry : tuples) {
1132+
result.put(entry.getKey(), entry.getValue());
11291133
}
11301134
return result;
11311135
}
@@ -1719,15 +1723,15 @@ public static <T> List<List<T>> unzip(final List<T>... lists) {
17191723
/*
17201724
* Documented, #object
17211725
*/
1722-
public static <K, V> List<Tuple<K, V>> object(final List<K> keys, final List<V> values) {
1726+
public static <K, V> List<Map.Entry<K, V>> object(final List<K> keys, final List<V> values) {
17231727
return map(
17241728
keys,
1725-
new Function<K, Tuple<K, V>>() {
1729+
new Function<K, Map.Entry<K, V>>() {
17261730
private int index;
17271731

17281732
@Override
1729-
public Tuple<K, V> apply(K key) {
1730-
return Tuple.create(key, values.get(index++));
1733+
public Map.Entry<K, V> apply(K key) {
1734+
return Map.entry(key, values.get(index++));
17311735
}
17321736
});
17331737
}
@@ -2287,29 +2291,29 @@ public static <K, V> Collection<V> values(final Map<K, V> object) {
22872291
return object.values();
22882292
}
22892293

2290-
public static <K, V> List<Tuple<K, V>> mapObject(
2294+
public static <K, V> List<Map.Entry<K, V>> mapObject(
22912295
final Map<K, V> object, final Function<? super V, V> func) {
22922296
return map(
22932297
newArrayList(object.entrySet()),
2294-
entry -> Tuple.create(entry.getKey(), func.apply(entry.getValue())));
2298+
entry -> Map.entry(entry.getKey(), func.apply(entry.getValue())));
22952299
}
22962300

22972301
/*
22982302
* Documented, #pairs
22992303
*/
2300-
public static <K, V> List<Tuple<K, V>> pairs(final Map<K, V> object) {
2304+
public static <K, V> List<Map.Entry<K, V>> pairs(final Map<K, V> object) {
23012305
return map(
23022306
newArrayList(object.entrySet()),
2303-
entry -> Tuple.create(entry.getKey(), entry.getValue()));
2307+
entry -> Map.entry(entry.getKey(), entry.getValue()));
23042308
}
23052309

23062310
/*
23072311
* Documented, #invert
23082312
*/
2309-
public static <K, V> List<Tuple<V, K>> invert(final Map<K, V> object) {
2313+
public static <K, V> List<Map.Entry<V, K>> invert(final Map<K, V> object) {
23102314
return map(
23112315
newArrayList(object.entrySet()),
2312-
entry -> Tuple.create(entry.getValue(), entry.getKey()));
2316+
entry -> Map.entry(entry.getValue(), entry.getKey()));
23132317
}
23142318

23152319
/*
@@ -2374,66 +2378,68 @@ public static <E> E findLastKey(final E[] array, final Predicate<E> pred) {
23742378
* Documented, #pick
23752379
*/
23762380
@SuppressWarnings("unchecked")
2377-
public static <K, V> List<Tuple<K, V>> pick(final Map<K, V> object, final K... keys) {
2381+
public static <K, V> List<Map.Entry<K, V>> pick(final Map<K, V> object, final K... keys) {
23782382
return without(
23792383
map(
23802384
newArrayList(object.entrySet()),
23812385
entry -> {
23822386
if (Arrays.asList(keys).contains(entry.getKey())) {
2383-
return Tuple.create(entry.getKey(), entry.getValue());
2387+
return Map.entry(entry.getKey(), entry.getValue());
23842388
} else {
23852389
return null;
23862390
}
23872391
}),
2388-
(Tuple<K, V>) null);
2392+
(Map.Entry<K, V>) null);
23892393
}
23902394

23912395
@SuppressWarnings("unchecked")
2392-
public static <K, V> List<Tuple<K, V>> pick(final Map<K, V> object, final Predicate<V> pred) {
2396+
public static <K, V> List<Map.Entry<K, V>> pick(
2397+
final Map<K, V> object, final Predicate<V> pred) {
23932398
return without(
23942399
map(
23952400
newArrayList(object.entrySet()),
23962401
entry -> {
23972402
if (pred.test(object.get(entry.getKey()))) {
2398-
return Tuple.create(entry.getKey(), entry.getValue());
2403+
return Map.entry(entry.getKey(), entry.getValue());
23992404
} else {
24002405
return null;
24012406
}
24022407
}),
2403-
(Tuple<K, V>) null);
2408+
(Map.Entry<K, V>) null);
24042409
}
24052410

24062411
/*
24072412
* Documented, #omit
24082413
*/
24092414
@SuppressWarnings("unchecked")
2410-
public static <K, V> List<Tuple<K, V>> omit(final Map<K, V> object, final K... keys) {
2415+
public static <K, V> List<Map.Entry<K, V>> omit(final Map<K, V> object, final K... keys) {
24112416
return without(
24122417
map(
24132418
newArrayList(object.entrySet()),
24142419
entry -> {
24152420
if (Arrays.asList(keys).contains(entry.getKey())) {
24162421
return null;
24172422
} else {
2418-
return Tuple.create(entry.getKey(), entry.getValue());
2423+
return Map.entry(entry.getKey(), entry.getValue());
24192424
}
24202425
}),
2421-
(Tuple<K, V>) null);
2426+
(Map.Entry<K, V>) null);
24222427
}
24232428

24242429
@SuppressWarnings("unchecked")
2425-
public static <K, V> List<Tuple<K, V>> omit(final Map<K, V> object, final Predicate<V> pred) {
2430+
public static <K, V> List<Map.Entry<K, V>> omit(
2431+
final Map<K, V> object, final Predicate<V> pred) {
24262432
return without(
24272433
map(
24282434
newArrayList(object.entrySet()),
24292435
entry -> {
24302436
if (pred.test(entry.getValue())) {
24312437
return null;
24322438
} else {
2433-
return Tuple.create(entry.getKey(), entry.getValue());
2439+
return Map.entry(entry.getKey(), entry.getValue());
24342440
}
24352441
}),
2436-
(Tuple<K, V>) null);
2442+
(Map.Entry<K, V>) null);
24372443
}
24382444

24392445
/*
@@ -3071,11 +3077,11 @@ public Chain<Object> pluck(final String propertyName) {
30713077
return new Chain<>(Underscore.pluck(list, propertyName));
30723078
}
30733079

3074-
public <E> Chain<T> where(final List<Tuple<String, E>> properties) {
3080+
public <E> Chain<T> where(final List<Map.Entry<String, E>> properties) {
30753081
return new Chain<>(Underscore.where(list, properties));
30763082
}
30773083

3078-
public <E> Chain<Optional<T>> findWhere(final List<Tuple<String, E>> properties) {
3084+
public <E> Chain<Optional<T>> findWhere(final List<Map.Entry<String, E>> properties) {
30793085
return new Chain<>(Underscore.findWhere(list, properties));
30803086
}
30813087

@@ -3189,11 +3195,11 @@ public Chain<T> push(final T... values) {
31893195
return new Chain<>(Underscore.push(value(), values));
31903196
}
31913197

3192-
public Chain<Tuple<T, List<T>>> pop() {
3198+
public Chain<Map.Entry<T, List<T>>> pop() {
31933199
return new Chain<>(Underscore.pop(value()));
31943200
}
31953201

3196-
public Chain<Tuple<T, List<T>>> shift() {
3202+
public Chain<Map.Entry<T, List<T>>> shift() {
31973203
return new Chain<>(Underscore.shift(value()));
31983204
}
31993205

@@ -3331,11 +3337,11 @@ public List<T> push(final T... values) {
33313337
return push((List<T>) getIterable(), values);
33323338
}
33333339

3334-
public static <T> Tuple<T, List<T>> pop(final List<T> list) {
3335-
return Tuple.create(last(list), initial(list));
3340+
public static <T> Map.Entry<T, List<T>> pop(final List<T> list) {
3341+
return Map.entry(last(list), initial(list));
33363342
}
33373343

3338-
public Tuple<T, List<T>> pop() {
3344+
public Map.Entry<T, List<T>> pop() {
33393345
return pop((List<T>) getIterable());
33403346
}
33413347

@@ -3355,11 +3361,11 @@ public List<T> unshift(final T... values) {
33553361
return unshift((List<T>) getIterable(), values);
33563362
}
33573363

3358-
public static <T> Tuple<T, List<T>> shift(final List<T> list) {
3359-
return Tuple.create(first(list), rest(list));
3364+
public static <T> Map.Entry<T, List<T>> shift(final List<T> list) {
3365+
return Map.entry(first(list), rest(list));
33603366
}
33613367

3362-
public Tuple<T, List<T>> shift() {
3368+
public Map.Entry<T, List<T>> shift() {
33633369
return shift((List<T>) getIterable());
33643370
}
33653371

@@ -3615,12 +3621,13 @@ public T get(final int index) {
36153621
return elementAt((List<T>) value(), index);
36163622
}
36173623

3618-
public static <T> Tuple<T, List<T>> set(final List<T> list, final int index, final T value) {
3624+
public static <T> Map.Entry<T, List<T>> set(
3625+
final List<T> list, final int index, final T value) {
36193626
final List<T> newList = newArrayList(list);
3620-
return Tuple.create(newList.set(index, value), newList);
3627+
return Map.entry(newList.set(index, value), newList);
36213628
}
36223629

3623-
public Tuple<T, List<T>> set(final int index, final T value) {
3630+
public Map.Entry<T, List<T>> set(final int index, final T value) {
36243631
return set((List<T>) value(), index, value);
36253632
}
36263633

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Collections;
3636
import java.util.LinkedHashSet;
3737
import java.util.List;
38+
import java.util.Map;
3839
import java.util.NoSuchElementException;
3940
import java.util.Set;
4041
import org.junit.jupiter.api.Test;
@@ -1033,9 +1034,9 @@ void unzip() {
10331034
*/
10341035
@Test
10351036
void object() {
1036-
final List<Tuple<String, String>> result =
1037+
final List<Map.Entry<String, String>> result =
10371038
Underscore.object(asList("moe", "larry", "curly"), asList("30", "40", "50"));
1038-
assertEquals("[(moe, 30), (larry, 40), (curly, 50)]", result.toString());
1039+
assertEquals("[moe=30, larry=40, curly=50]", result.toString());
10391040
}
10401041

10411042
/*

0 commit comments

Comments
 (0)