Skip to content

Commit 2987dd3

Browse files
committed
Add java 8 for examples.
1 parent cb38896 commit 2987dd3

File tree

11 files changed

+56
-143
lines changed

11 files changed

+56
-143
lines changed

examples/src/main/java/com/github/underscore/examples/Chaining.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2018 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
@@ -24,6 +24,7 @@
2424
package com.github.underscore.examples;
2525

2626
import java.util.*;
27+
import java.util.function.Function;
2728

2829
/**
2930
* Examples for underscore-java.
@@ -36,22 +37,17 @@ public static <E> E first(final Iterable<E> iterable) {
3637
}
3738

3839
public static <T, E> List<T> map(final List<E> list, final Function<? super E, T> func) {
39-
final List<T> transformed = new ArrayList<T>(list.size());
40+
final List<T> transformed = new ArrayList<>(list.size());
4041
for (E element : list) {
41-
transformed.add(func.apply(element));
42+
func.andThen(transformed::add).apply(element);
4243
}
4344
return transformed;
4445
}
4546

4647
public static <E, T extends Comparable<? super T>> List<E> sortBy(final List<E> iterable,
4748
final Function<E, T> func) {
48-
final List<E> sortedList = new ArrayList<E>(iterable);
49-
Collections.sort(sortedList, new Comparator<E>() {
50-
@Override
51-
public int compare(E o1, E o2) {
52-
return func.apply(o1).compareTo(func.apply(o2));
53-
}
54-
});
49+
final List<E> sortedList = new ArrayList<>(iterable);
50+
sortedList.sort(Comparator.comparing(func::apply));
5551
return sortedList;
5652
}
5753

@@ -65,7 +61,7 @@ public int compare(E o1, E o2) {
6561
=> "moe is 21"
6662
*/
6763
public static <T> Chain<T> chain(final List<T> list) {
68-
return new Chaining.Chain<T>(list);
64+
return new Chaining.Chain<>(list);
6965
}
7066

7167
public static class Chain<T> {
@@ -82,15 +78,15 @@ public Chain(final List<T> list) {
8278
}
8379

8480
public Chain<T> first() {
85-
return new Chain<T>(Chaining.first(list));
81+
return new Chain<>(Chaining.first(list));
8682
}
8783

8884
public <F> Chain<F> map(final Function<? super T, F> func) {
89-
return new Chain<F>(Chaining.map(list, func));
85+
return new Chain<>(Chaining.map(list, func));
9086
}
9187

9288
public <F extends Comparable<? super F>> Chain<T> sortBy(final Function<T, F> func) {
93-
return new Chain<T>(Chaining.sortBy(list, func));
89+
return new Chain<>(Chaining.sortBy(list, func));
9490
}
9591

9692
public T item() {

examples/src/main/java/com/github/underscore/examples/FromJson.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2018 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
@@ -115,7 +115,7 @@ private Object readValue() {
115115

116116
private List<Object> readArray() {
117117
read();
118-
List<Object> array = new ArrayList<Object>();
118+
List<Object> array = new ArrayList<>();
119119
skipWhiteSpace();
120120
if (readChar(']')) {
121121
return array;
@@ -133,7 +133,7 @@ private List<Object> readArray() {
133133

134134
private Map<String, Object> readObject() {
135135
read();
136-
Map<String, Object> object = new LinkedHashMap<String, Object>();
136+
Map<String, Object> object = new LinkedHashMap<>();
137137
skipWhiteSpace();
138138
if (readChar('}')) {
139139
return object;

examples/src/main/java/com/github/underscore/examples/FromXml.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2018 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
@@ -23,6 +23,7 @@
2323
*/
2424
package com.github.underscore.examples;
2525

26+
import java.nio.charset.StandardCharsets;
2627
import java.util.*;
2728

2829
/**
@@ -44,7 +45,7 @@ private static Object getValue(final Object value) {
4445

4546
@SuppressWarnings("unchecked")
4647
private static Map<String, Object> createMap(final org.w3c.dom.Node node) {
47-
final Map<String, Object> map = new LinkedHashMap<String, Object>();
48+
final Map<String, Object> map = new LinkedHashMap<>();
4849
final org.w3c.dom.NodeList nodeList = node.getChildNodes();
4950
for (int index = 0; index < nodeList.getLength(); index++) {
5051
final org.w3c.dom.Node currentNode = nodeList.item(index);
@@ -63,7 +64,7 @@ private static Map<String, Object> createMap(final org.w3c.dom.Node node) {
6364
if (object instanceof List) {
6465
((List<Object>) object).add(getValue(value));
6566
} else {
66-
final List<Object> objects = new ArrayList<Object>();
67+
final List<Object> objects = new ArrayList<>();
6768
objects.add(object);
6869
objects.add(getValue(value));
6970
map.put(name, objects);
@@ -77,7 +78,7 @@ private static Map<String, Object> createMap(final org.w3c.dom.Node node) {
7778

7879
public static Object fromXml(final String xml) {
7980
try {
80-
final java.io.InputStream stream = new java.io.ByteArrayInputStream(xml.getBytes("UTF-8"));
81+
final java.io.InputStream stream = new java.io.ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
8182
final javax.xml.parsers.DocumentBuilderFactory factory =
8283
javax.xml.parsers.DocumentBuilderFactory.newInstance();
8384
factory.setNamespaceAware(true);

examples/src/main/java/com/github/underscore/examples/Function.java

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

examples/src/main/java/com/github/underscore/examples/Function3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2018 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

examples/src/main/java/com/github/underscore/examples/Intersection.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2018 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
@@ -24,6 +24,7 @@
2424
package com.github.underscore.examples;
2525

2626
import java.util.*;
27+
import java.util.function.Predicate;
2728

2829
/**
2930
* Examples for underscore-java.
@@ -45,16 +46,11 @@ public static <E> boolean some(final Iterable<E> iterable, final Predicate<E> pr
4546
}
4647

4748
public static <E> boolean contains(final Iterable<E> iterable, final E elem) {
48-
return some(iterable, new Predicate<E>() {
49-
@Override
50-
public boolean test(E e) {
51-
return elem == null ? e == null : elem.equals(e);
52-
}
53-
});
49+
return some(iterable, e -> Objects.equals(elem, e));
5450
}
5551

5652
public static <E> List<E> filter(final List<E> list, final Predicate<E> pred) {
57-
final List<E> filtered = new ArrayList<E>();
53+
final List<E> filtered = new ArrayList<>();
5854
for (E element : list) {
5955
if (pred.test(element)) {
6056
filtered.add(element);
@@ -64,12 +60,7 @@ public static <E> List<E> filter(final List<E> list, final Predicate<E> pred) {
6460
}
6561

6662
public static <E> List<E> intersection(final List<E> list1, final List<E> list2) {
67-
return filter(list1, new Predicate<E>() {
68-
@Override
69-
public boolean test(E elem) {
70-
return contains(list2, elem);
71-
}
72-
});
63+
return filter(list1, elem -> contains(list2, elem));
7364
}
7465

7566
/*
@@ -78,10 +69,10 @@ public boolean test(E elem) {
7869
*/
7970
@SuppressWarnings("unchecked")
8071
public static <E> List<E> intersection(final List<E> list, final List<E> ... lists) {
81-
final Stack<List<E>> stack = new Stack<List<E>>();
72+
final Stack<List<E>> stack = new Stack<>();
8273
stack.push(list);
83-
for (int index = 0; index < lists.length; index += 1) {
84-
stack.push(intersection(stack.peek(), lists[index]));
74+
for (List<E> es : lists) {
75+
stack.push(intersection(stack.peek(), es));
8576
}
8677
return stack.peek();
8778
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2018 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
@@ -23,6 +23,8 @@
2323
*/
2424
package com.github.underscore.examples;
2525

26+
import java.util.Objects;
27+
2628
public final class Optional<T> {
2729
private static final Optional<?> EMPTY = new Optional();
2830
private final T arg;
@@ -39,12 +41,12 @@ private Optional(final T arg) {
3941
}
4042

4143
public static <T> Optional<T> of(final T arg) {
42-
return new Optional<T>(arg);
44+
return new Optional<>(arg);
4345
}
4446

4547
public static <T> Optional<T> fromNullable(final T nullableReference) {
4648
return nullableReference == null ? Optional.<T>absent()
47-
: new Optional<T>(nullableReference);
49+
: new Optional<>(nullableReference);
4850
}
4951

5052
@SuppressWarnings("unchecked")
@@ -91,7 +93,7 @@ public boolean equals(final Object o) {
9193
if (absent != optional.absent) {
9294
return false;
9395
}
94-
return !(arg == null ? optional.arg != null : !arg.equals(optional.arg));
96+
return Objects.equals(arg, optional.arg);
9597
}
9698

9799
@Override

examples/src/main/java/com/github/underscore/examples/Predicate.java

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

examples/src/main/java/com/github/underscore/examples/SnakeCase.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015-2018 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
@@ -24,6 +24,7 @@
2424
package com.github.underscore.examples;
2525

2626
import java.util.*;
27+
import java.util.function.Function;
2728

2829
/**
2930
* Examples for underscore-java.
@@ -33,7 +34,7 @@
3334
public class SnakeCase {
3435
private static final java.util.regex.Pattern RE_LATIN_1 = java.util.regex.Pattern.compile(
3536
"[\\xc0-\\xd6\\xd8-\\xde\\xdf-\\xf6\\xf8-\\xff]");
36-
private static final Map<String, String> DEBURRED_LETTERS = new LinkedHashMap<String, String>();
37+
private static final Map<String, String> DEBURRED_LETTERS = new LinkedHashMap<>();
3738

3839
static {
3940
String[] deburredLetters = new String[] {
@@ -87,7 +88,7 @@ public static String deburr(final String string) {
8788

8889
public static List<String> words(final String string) {
8990
final String localString = baseToString(string);
90-
final List<String> result = new ArrayList<String>();
91+
final List<String> result = new ArrayList<>();
9192
final java.util.regex.Matcher matcher = reWords.matcher(localString);
9293
while (matcher.find()) {
9394
result.add(matcher.group());
@@ -97,18 +98,16 @@ public static List<String> words(final String string) {
9798

9899
private static Function<String, String> createCompounder(
99100
final Function3<String, String, Integer, String> callback) {
100-
return new Function<String, String>() {
101-
public String apply(final String string) {
102-
int index = -1;
103-
List<String> array = words(deburr(string));
104-
int length = array.size();
105-
String result = "";
101+
return string -> {
102+
int index = -1;
103+
List<String> array = words(deburr(string));
104+
int length = array.size();
105+
String result = "";
106106

107-
while (++index < length) {
108-
result = callback.apply(result, array.get(index), index);
109-
}
110-
return result;
107+
while (++index < length) {
108+
result = callback.apply(result, array.get(index), index);
111109
}
110+
return result;
112111
};
113112
}
114113

@@ -123,10 +122,7 @@ public String apply(final String string) {
123122
=> 'foo_bar'
124123
*/
125124
public static String snakeCase(final String string) {
126-
return createCompounder(new Function3<String, String, Integer, String>() {
127-
public String apply(final String result, final String word, final Integer index) {
128-
return result + (index > 0 ? "_" : "") + word.toLowerCase(Locale.getDefault());
129-
}
130-
}).apply(string);
125+
return createCompounder((result, word, index) -> result + (index > 0 ? "_" : "")
126+
+ word.toLowerCase(Locale.getDefault())).apply(string);
131127
}
132128
}

0 commit comments

Comments
 (0)