Skip to content

Commit c6dbd9b

Browse files
authored
Improved creation of ArrayList and LinkedHashMap
1 parent d87fa4c commit c6dbd9b

File tree

6 files changed

+191
-216
lines changed

6 files changed

+191
-216
lines changed

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

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ public List<Object> pull(Object... values) {
10451045
}
10461046

10471047
public static List<Object> pullAt(final List<Object> list, final Integer... indexes) {
1048-
final List<Object> result = newArrayList();
1048+
final List<Object> result = new ArrayList<>();
10491049
final List<Integer> indexesList = Arrays.asList(indexes);
10501050
int index = 0;
10511051
for (final Iterator<Object> iterator = list.iterator(); iterator.hasNext(); ) {
@@ -1065,7 +1065,7 @@ public List<Object> pullAt(final Integer... indexes) {
10651065
}
10661066

10671067
public static <T> List<T> remove(final List<T> list, final Predicate<T> pred) {
1068-
final List<T> result = newArrayList();
1068+
final List<T> result = new ArrayList<>();
10691069
for (final Iterator<T> iterator = list.iterator(); iterator.hasNext(); ) {
10701070
final T object = iterator.next();
10711071
if (pred.test(object)) {
@@ -1149,7 +1149,7 @@ public List<T> xor(final List<T> list) {
11491149
}
11501150

11511151
public static <T> List<T> at(final List<T> list, final Integer... indexes) {
1152-
final List<T> result = newArrayList();
1152+
final List<T> result = new ArrayList<>();
11531153
final List<Integer> indexesList = Arrays.asList(indexes);
11541154
int index = 0;
11551155
for (final T object : list) {
@@ -1874,7 +1874,7 @@ public static <T> T remove(final Map<String, Object> object, final List<String>
18741874

18751875
public static Map<String, Object> rename(
18761876
final Map<String, Object> map, final String oldKey, final String newKey) {
1877-
Map<String, Object> outMap = newLinkedHashMap();
1877+
Map<String, Object> outMap = new LinkedHashMap<>();
18781878
for (Map.Entry<String, Object> entry : map.entrySet()) {
18791879
if (entry.getKey().equals(oldKey)) {
18801880
outMap.put(newKey, makeObjectForRename(entry.getValue(), oldKey, newKey));
@@ -1890,7 +1890,7 @@ private static Object makeObjectForRename(
18901890
Object value, final String oldKey, final String newKey) {
18911891
final Object result;
18921892
if (value instanceof List) {
1893-
List<Object> values = newArrayList();
1893+
List<Object> values = new ArrayList<>();
18941894
for (Object item : (List) value) {
18951895
values.add(
18961896
item instanceof Map
@@ -1915,7 +1915,7 @@ public static Map<String, Object> setValue(
19151915
final Map<String, Object> map,
19161916
final String key,
19171917
final BiFunction<String, Object, Object> newValue) {
1918-
Map<String, Object> outMap = newLinkedHashMap();
1918+
Map<String, Object> outMap = new LinkedHashMap<>();
19191919
for (Map.Entry<String, Object> entry : map.entrySet()) {
19201920
if (entry.getKey().equals(key)) {
19211921
outMap.put(
@@ -1934,7 +1934,7 @@ private static Object makeObjectForSetValue(
19341934
Object value, final String key, final BiFunction<String, Object, Object> newValue) {
19351935
final Object result;
19361936
if (value instanceof List) {
1937-
List<Object> values = newArrayList();
1937+
List<Object> values = new ArrayList<>();
19381938
for (Object item : (List) value) {
19391939
values.add(
19401940
item instanceof Map
@@ -1952,7 +1952,7 @@ private static Object makeObjectForSetValue(
19521952

19531953
public static Map<String, Object> update(
19541954
final Map<String, Object> map1, final Map<String, Object> map2) {
1955-
Map<String, Object> outMap = newLinkedHashMap();
1955+
Map<String, Object> outMap = new LinkedHashMap<>();
19561956
for (Map.Entry<String, Object> entry : map1.entrySet()) {
19571957
String key = entry.getKey();
19581958
Object value2 = entry.getValue();
@@ -2309,7 +2309,7 @@ && nonNull(timeBetweenRetry)
23092309
}
23102310

23112311
public static List<String> explode(final String input) {
2312-
List<String> result = newArrayList();
2312+
List<String> result = new ArrayList<>();
23132313
if (isNull(input)) {
23142314
return result;
23152315
}
@@ -2511,22 +2511,10 @@ public List<List<T>> createPermutationWithRepetition(final int permutationLength
25112511
return createPermutationWithRepetition((List<T>) value(), permutationLength);
25122512
}
25132513

2514-
protected static <T> List<T> newArrayList() {
2515-
return Underscore.newArrayList();
2516-
}
2517-
25182514
protected static <T> List<T> newArrayList(final Iterable<T> iterable) {
25192515
return Underscore.newArrayList(iterable);
25202516
}
25212517

2522-
protected static <T> Set<T> newLinkedHashSet() {
2523-
return Underscore.newLinkedHashSet();
2524-
}
2525-
2526-
protected static <K, E> Map<K, E> newLinkedHashMap() {
2527-
return Underscore.newLinkedHashMap();
2528-
}
2529-
25302518
public static <T> String join(final Iterable<T> iterable, final String separator) {
25312519
return Underscore.join(iterable, separator);
25322520
}
@@ -2615,7 +2603,7 @@ private static Map<String, Object> getStringObjectMap(Object object) {
26152603
if (object instanceof Map) {
26162604
result = (Map<String, Object>) object;
26172605
} else {
2618-
result = newLinkedHashMap();
2606+
result = new LinkedHashMap<>();
26192607
result.put("value", object);
26202608
}
26212609
return result;
@@ -2652,7 +2640,7 @@ public static String jsonToXml(
26522640
newRootName);
26532641
} else if (mode == JsonToXmlMode.ADD_ROOT
26542642
&& !Xml.XmlValue.getMapKey(object).equals(ROOT)) {
2655-
final Map<String, Object> map = U.newLinkedHashMap();
2643+
final Map<String, Object> map = new LinkedHashMap<>();
26562644
map.put(newRootName, object);
26572645
result = Xml.toXml(map, identStep);
26582646
} else if (mode == JsonToXmlMode.REMOVE_ARRAY_ATTRIBUTE) {
@@ -2863,7 +2851,7 @@ public static String changeXmlEncoding(String xml, String encoding) {
28632851
}
28642852

28652853
public static Map<String, Object> removeMinusesAndConvertNumbers(Map<String, Object> map) {
2866-
Map<String, Object> outMap = newLinkedHashMap();
2854+
Map<String, Object> outMap = new LinkedHashMap<>();
28672855
for (Map.Entry<String, Object> entry : map.entrySet()) {
28682856
final String newKey;
28692857
if (entry.getKey().startsWith("-")) {
@@ -2883,7 +2871,7 @@ public static Map<String, Object> removeMinusesAndConvertNumbers(Map<String, Obj
28832871
private static Object makeObject(Object value) {
28842872
final Object result;
28852873
if (value instanceof List) {
2886-
List<Object> values = newArrayList();
2874+
List<Object> values = new ArrayList<>();
28872875
for (Object item : (List) value) {
28882876
values.add(
28892877
item instanceof Map
@@ -2947,7 +2935,7 @@ public static Map<String, Object> replaceSelfClosingWithEmpty(Map<String, Object
29472935

29482936
@SuppressWarnings("unchecked")
29492937
public static Object replaceSelfClosingWithValue(Map<String, Object> map, String value) {
2950-
Object outMap = newLinkedHashMap();
2938+
Object outMap = new LinkedHashMap<>();
29512939
for (Map.Entry<String, Object> entry : map.entrySet()) {
29522940
if (selfClosing.equals(entry.getKey()) && "true".equals(entry.getValue())) {
29532941
if (map.size() == 1) {
@@ -2968,7 +2956,7 @@ public static Object replaceSelfClosingWithValue(Map<String, Object> map, String
29682956
private static Object makeObjectSelfClose(Object value, String newValue) {
29692957
final Object result;
29702958
if (value instanceof List) {
2971-
List<Object> values = newArrayList();
2959+
List<Object> values = new ArrayList<>();
29722960
for (Object item : (List) value) {
29732961
values.add(
29742962
item instanceof Map
@@ -2988,7 +2976,7 @@ public static Map<String, Object> replaceEmptyValueWithNull(Map<String, Object>
29882976
if (map == null || map.isEmpty()) {
29892977
return null;
29902978
}
2991-
Map<String, Object> outMap = newLinkedHashMap();
2979+
Map<String, Object> outMap = new LinkedHashMap<>();
29922980
for (Map.Entry<String, Object> entry : map.entrySet()) {
29932981
outMap.put(String.valueOf(entry.getKey()), makeObjectEmptyValue(entry.getValue()));
29942982
}
@@ -2999,7 +2987,7 @@ public static Map<String, Object> replaceEmptyValueWithNull(Map<String, Object>
29992987
private static Object makeObjectEmptyValue(Object value) {
30002988
final Object result;
30012989
if (value instanceof List) {
3002-
List<Object> values = newArrayList();
2990+
List<Object> values = new ArrayList<>();
30032991
for (Object item : (List) value) {
30042992
values.add(item instanceof Map ? replaceEmptyValueWithNull((Map) item) : item);
30052993
}
@@ -3016,7 +3004,7 @@ public static Object replaceEmptyValueWithEmptyString(Map<String, Object> map) {
30163004
if (map.isEmpty()) {
30173005
return "";
30183006
}
3019-
Map<String, Object> outMap = newLinkedHashMap();
3007+
Map<String, Object> outMap = new LinkedHashMap<>();
30203008
for (Map.Entry<String, Object> entry : map.entrySet()) {
30213009
outMap.put(String.valueOf(entry.getKey()), makeObjectEmptyString(entry.getValue()));
30223010
}
@@ -3027,7 +3015,7 @@ public static Object replaceEmptyValueWithEmptyString(Map<String, Object> map) {
30273015
private static Object makeObjectEmptyString(Object value) {
30283016
final Object result;
30293017
if (value instanceof List) {
3030-
List<Object> values = newArrayList();
3018+
List<Object> values = new ArrayList<>();
30313019
for (Object item : (List) value) {
30323020
values.add(
30333021
item instanceof Map ? replaceEmptyValueWithEmptyString((Map) item) : item);
@@ -3042,7 +3030,7 @@ private static Object makeObjectEmptyString(Object value) {
30423030
}
30433031

30443032
public static Map<String, Object> forceAttributeUsage(Map<String, Object> map) {
3045-
Map<String, Object> outMap = newLinkedHashMap();
3033+
Map<String, Object> outMap = new LinkedHashMap<>();
30463034
for (Map.Entry<String, Object> entry : map.entrySet()) {
30473035
outMap.put(
30483036
entry.getValue() instanceof Map
@@ -3059,7 +3047,7 @@ public static Map<String, Object> forceAttributeUsage(Map<String, Object> map) {
30593047
private static Object makeAttributeUsage(Object value) {
30603048
final Object result;
30613049
if (value instanceof List) {
3062-
List<Object> values = newArrayList();
3050+
List<Object> values = new ArrayList<>();
30633051
for (Object item : (List) value) {
30643052
values.add(item instanceof Map ? forceAttributeUsage((Map) item) : item);
30653053
}
@@ -3073,12 +3061,12 @@ private static Object makeAttributeUsage(Object value) {
30733061
}
30743062

30753063
public static Map<String, Object> replaceNullWithEmptyValue(Map<String, Object> map) {
3076-
Map<String, Object> outMap = newLinkedHashMap();
3064+
Map<String, Object> outMap = new LinkedHashMap<>();
30773065
for (Map.Entry<String, Object> entry : map.entrySet()) {
30783066
outMap.put(
30793067
entry.getKey(),
30803068
entry.getValue() == null
3081-
? newLinkedHashMap()
3069+
? new LinkedHashMap<>()
30823070
: makeReplaceNullValue(entry.getValue()));
30833071
}
30843072
return outMap;
@@ -3088,7 +3076,7 @@ public static Map<String, Object> replaceNullWithEmptyValue(Map<String, Object>
30883076
private static Object makeReplaceNullValue(Object value) {
30893077
final Object result;
30903078
if (value instanceof List) {
3091-
List<Object> values = newArrayList();
3079+
List<Object> values = new ArrayList<>();
30923080
for (Object item : (List) value) {
30933081
values.add(item instanceof Map ? replaceNullWithEmptyValue((Map) item) : item);
30943082
}
@@ -3102,12 +3090,12 @@ private static Object makeReplaceNullValue(Object value) {
31023090
}
31033091

31043092
public static Map<String, Object> replaceEmptyStringWithEmptyValue(Map<String, Object> map) {
3105-
Map<String, Object> outMap = newLinkedHashMap();
3093+
Map<String, Object> outMap = new LinkedHashMap<>();
31063094
for (Map.Entry<String, Object> entry : map.entrySet()) {
31073095
outMap.put(
31083096
entry.getKey(),
31093097
"".equals(entry.getValue())
3110-
? newLinkedHashMap()
3098+
? new LinkedHashMap<>()
31113099
: makeReplaceEmptyString(entry.getValue()));
31123100
}
31133101
return outMap;
@@ -3117,7 +3105,7 @@ public static Map<String, Object> replaceEmptyStringWithEmptyValue(Map<String, O
31173105
private static Object makeReplaceEmptyString(Object value) {
31183106
final Object result;
31193107
if (value instanceof List) {
3120-
List<Object> values = newArrayList();
3108+
List<Object> values = new ArrayList<>();
31213109
for (Object item : (List) value) {
31223110
values.add(
31233111
item instanceof Map ? replaceEmptyStringWithEmptyValue((Map) item) : item);
@@ -3132,7 +3120,7 @@ private static Object makeReplaceEmptyString(Object value) {
31323120
}
31333121

31343122
public static Map<String, Object> replaceNumberAndBooleanWithString(Map<String, Object> map) {
3135-
Map<String, Object> outMap = newLinkedHashMap();
3123+
Map<String, Object> outMap = new LinkedHashMap<>();
31363124
for (Map.Entry<String, Object> entry : map.entrySet()) {
31373125
outMap.put(
31383126
entry.getKey(),
@@ -3147,7 +3135,7 @@ public static Map<String, Object> replaceNumberAndBooleanWithString(Map<String,
31473135
private static Object makeReplaceNumberAndBoolean(Object value) {
31483136
final Object result;
31493137
if (value instanceof List) {
3150-
List<Object> values = newArrayList();
3138+
List<Object> values = new ArrayList<>();
31513139
for (Object item : (List) value) {
31523140
if (item instanceof Map) {
31533141
values.add(replaceNumberAndBooleanWithString((Map) item));
@@ -3174,7 +3162,7 @@ public static Map<String, Object> replaceFirstLevel(Map<String, Object> map) {
31743162

31753163
@SuppressWarnings("unchecked")
31763164
public static Map<String, Object> replaceFirstLevel(Map<String, Object> map, int level) {
3177-
Map<String, Object> outMap = newLinkedHashMap();
3165+
Map<String, Object> outMap = new LinkedHashMap<>();
31783166
for (Map.Entry<String, Object> entry : map.entrySet()) {
31793167
outMap.put(entry.getKey(), makeReplaceFirstLevel(entry.getValue(), level + 1));
31803168
}
@@ -3193,7 +3181,7 @@ public static Map<String, Object> replaceFirstLevel(Map<String, Object> map, int
31933181
private static Object makeReplaceFirstLevel(Object value, int level) {
31943182
final Object result;
31953183
if (value instanceof List) {
3196-
List<Object> values = newArrayList();
3184+
List<Object> values = new ArrayList<>();
31973185
for (Object item : (List) value) {
31983186
values.add(item instanceof Map ? replaceFirstLevel((Map) item, level + 1) : item);
31993187
}
@@ -3207,7 +3195,7 @@ private static Object makeReplaceFirstLevel(Object value, int level) {
32073195
}
32083196

32093197
public static Map<String, Object> replaceNilWithNull(Map<String, Object> map) {
3210-
Map<String, Object> outMap = newLinkedHashMap();
3198+
Map<String, Object> outMap = new LinkedHashMap<>();
32113199
for (Map.Entry<String, Object> entry : map.entrySet()) {
32123200
Object outValue = makeReplaceNilWithNull(entry.getValue());
32133201
if (outValue instanceof Map
@@ -3227,7 +3215,7 @@ public static Map<String, Object> replaceNilWithNull(Map<String, Object> map) {
32273215
private static Object makeReplaceNilWithNull(Object value) {
32283216
final Object result;
32293217
if (value instanceof List) {
3230-
List<Object> values = newArrayList();
3218+
List<Object> values = new ArrayList<>();
32313219
for (Object item : (List) value) {
32323220
values.add(item instanceof Map ? replaceNilWithNull((Map) item) : item);
32333221
}
@@ -3241,7 +3229,7 @@ private static Object makeReplaceNilWithNull(Object value) {
32413229
}
32423230

32433231
public static Map<String, Object> deepCopyMap(Map<String, Object> map) {
3244-
Map<String, Object> outMap = newLinkedHashMap();
3232+
Map<String, Object> outMap = new LinkedHashMap<>();
32453233
for (Map.Entry<String, Object> entry : map.entrySet()) {
32463234
outMap.put(entry.getKey(), makeDeepCopyMap(entry.getValue()));
32473235
}
@@ -3252,7 +3240,7 @@ public static Map<String, Object> deepCopyMap(Map<String, Object> map) {
32523240
private static Object makeDeepCopyMap(Object value) {
32533241
final Object result;
32543242
if (value instanceof List) {
3255-
List<Object> values = newArrayList();
3243+
List<Object> values = new ArrayList<>();
32563244
for (Object item : (List) value) {
32573245
values.add(item instanceof Map ? deepCopyMap((Map) item) : item);
32583246
}
@@ -3273,7 +3261,7 @@ public static class Builder {
32733261
private final Map<String, Object> data;
32743262

32753263
public Builder() {
3276-
data = newLinkedHashMap();
3264+
data = new LinkedHashMap<>();
32773265
}
32783266

32793267
public Builder add(final String key, final Object value) {
@@ -3406,7 +3394,7 @@ public static class ArrayBuilder {
34063394
private final List<Object> data;
34073395

34083396
public ArrayBuilder() {
3409-
data = newArrayList();
3397+
data = new ArrayList<>();
34103398
}
34113399

34123400
public ArrayBuilder add(final Object value) {

0 commit comments

Comments
 (0)