Skip to content

Commit

Permalink
Improved creation of ArrayList and LinkedHashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed Oct 2, 2023
1 parent d87fa4c commit 5c0b8e2
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 216 deletions.
86 changes: 37 additions & 49 deletions src/main/java/com/github/underscore/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ public List<Object> pull(Object... values) {
}

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

public static <T> List<T> remove(final List<T> list, final Predicate<T> pred) {
final List<T> result = newArrayList();
final List<T> result = new ArrayList<>();
for (final Iterator<T> iterator = list.iterator(); iterator.hasNext(); ) {
final T object = iterator.next();
if (pred.test(object)) {
Expand Down Expand Up @@ -1149,7 +1149,7 @@ public List<T> xor(final List<T> list) {
}

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

public static Map<String, Object> rename(
final Map<String, Object> map, final String oldKey, final String newKey) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().equals(oldKey)) {
outMap.put(newKey, makeObjectForRename(entry.getValue(), oldKey, newKey));
Expand All @@ -1890,7 +1890,7 @@ private static Object makeObjectForRename(
Object value, final String oldKey, final String newKey) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(
item instanceof Map
Expand All @@ -1915,7 +1915,7 @@ public static Map<String, Object> setValue(
final Map<String, Object> map,
final String key,
final BiFunction<String, Object, Object> newValue) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().equals(key)) {
outMap.put(
Expand All @@ -1934,7 +1934,7 @@ private static Object makeObjectForSetValue(
Object value, final String key, final BiFunction<String, Object, Object> newValue) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(
item instanceof Map
Expand All @@ -1952,7 +1952,7 @@ private static Object makeObjectForSetValue(

public static Map<String, Object> update(
final Map<String, Object> map1, final Map<String, Object> map2) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map1.entrySet()) {
String key = entry.getKey();
Object value2 = entry.getValue();
Expand Down Expand Up @@ -2309,7 +2309,7 @@ && nonNull(timeBetweenRetry)
}

public static List<String> explode(final String input) {
List<String> result = newArrayList();
List<String> result = new ArrayList<>();
if (isNull(input)) {
return result;
}
Expand Down Expand Up @@ -2511,22 +2511,10 @@ public List<List<T>> createPermutationWithRepetition(final int permutationLength
return createPermutationWithRepetition((List<T>) value(), permutationLength);
}

protected static <T> List<T> newArrayList() {
return Underscore.newArrayList();
}

protected static <T> List<T> newArrayList(final Iterable<T> iterable) {
return Underscore.newArrayList(iterable);
}

protected static <T> Set<T> newLinkedHashSet() {
return Underscore.newLinkedHashSet();
}

protected static <K, E> Map<K, E> newLinkedHashMap() {
return Underscore.newLinkedHashMap();
}

public static <T> String join(final Iterable<T> iterable, final String separator) {
return Underscore.join(iterable, separator);
}
Expand Down Expand Up @@ -2615,7 +2603,7 @@ private static Map<String, Object> getStringObjectMap(Object object) {
if (object instanceof Map) {
result = (Map<String, Object>) object;
} else {
result = newLinkedHashMap();
result = new LinkedHashMap<>();
result.put("value", object);
}
return result;
Expand Down Expand Up @@ -2652,7 +2640,7 @@ public static String jsonToXml(
newRootName);
} else if (mode == JsonToXmlMode.ADD_ROOT
&& !Xml.XmlValue.getMapKey(object).equals(ROOT)) {
final Map<String, Object> map = U.newLinkedHashMap();
final Map<String, Object> map = new LinkedHashMap<>();
map.put(newRootName, object);
result = Xml.toXml(map, identStep);
} else if (mode == JsonToXmlMode.REMOVE_ARRAY_ATTRIBUTE) {
Expand Down Expand Up @@ -2863,7 +2851,7 @@ public static String changeXmlEncoding(String xml, String encoding) {
}

public static Map<String, Object> removeMinusesAndConvertNumbers(Map<String, Object> map) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
final String newKey;
if (entry.getKey().startsWith("-")) {
Expand All @@ -2883,7 +2871,7 @@ public static Map<String, Object> removeMinusesAndConvertNumbers(Map<String, Obj
private static Object makeObject(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(
item instanceof Map
Expand Down Expand Up @@ -2947,7 +2935,7 @@ public static Map<String, Object> replaceSelfClosingWithEmpty(Map<String, Object

@SuppressWarnings("unchecked")
public static Object replaceSelfClosingWithValue(Map<String, Object> map, String value) {
Object outMap = newLinkedHashMap();
Object outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (selfClosing.equals(entry.getKey()) && "true".equals(entry.getValue())) {
if (map.size() == 1) {
Expand All @@ -2968,7 +2956,7 @@ public static Object replaceSelfClosingWithValue(Map<String, Object> map, String
private static Object makeObjectSelfClose(Object value, String newValue) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(
item instanceof Map
Expand All @@ -2988,7 +2976,7 @@ public static Map<String, Object> replaceEmptyValueWithNull(Map<String, Object>
if (map == null || map.isEmpty()) {
return null;
}
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
outMap.put(String.valueOf(entry.getKey()), makeObjectEmptyValue(entry.getValue()));
}
Expand All @@ -2999,7 +2987,7 @@ public static Map<String, Object> replaceEmptyValueWithNull(Map<String, Object>
private static Object makeObjectEmptyValue(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(item instanceof Map ? replaceEmptyValueWithNull((Map) item) : item);
}
Expand All @@ -3016,7 +3004,7 @@ public static Object replaceEmptyValueWithEmptyString(Map<String, Object> map) {
if (map.isEmpty()) {
return "";
}
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
outMap.put(String.valueOf(entry.getKey()), makeObjectEmptyString(entry.getValue()));
}
Expand All @@ -3027,7 +3015,7 @@ public static Object replaceEmptyValueWithEmptyString(Map<String, Object> map) {
private static Object makeObjectEmptyString(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(
item instanceof Map ? replaceEmptyValueWithEmptyString((Map) item) : item);
Expand All @@ -3042,7 +3030,7 @@ private static Object makeObjectEmptyString(Object value) {
}

public static Map<String, Object> forceAttributeUsage(Map<String, Object> map) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
outMap.put(
entry.getValue() instanceof Map
Expand All @@ -3059,7 +3047,7 @@ public static Map<String, Object> forceAttributeUsage(Map<String, Object> map) {
private static Object makeAttributeUsage(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(item instanceof Map ? forceAttributeUsage((Map) item) : item);
}
Expand All @@ -3073,12 +3061,12 @@ private static Object makeAttributeUsage(Object value) {
}

public static Map<String, Object> replaceNullWithEmptyValue(Map<String, Object> map) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
outMap.put(
entry.getKey(),
entry.getValue() == null
? newLinkedHashMap()
? (Map<Object, Object>) new LinkedHashMap<Object, Object>()
: makeReplaceNullValue(entry.getValue()));
}
return outMap;
Expand All @@ -3088,7 +3076,7 @@ public static Map<String, Object> replaceNullWithEmptyValue(Map<String, Object>
private static Object makeReplaceNullValue(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(item instanceof Map ? replaceNullWithEmptyValue((Map) item) : item);
}
Expand All @@ -3102,12 +3090,12 @@ private static Object makeReplaceNullValue(Object value) {
}

public static Map<String, Object> replaceEmptyStringWithEmptyValue(Map<String, Object> map) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
outMap.put(
entry.getKey(),
"".equals(entry.getValue())
? newLinkedHashMap()
? (Map<Object, Object>) new LinkedHashMap<Object, Object>()
: makeReplaceEmptyString(entry.getValue()));
}
return outMap;
Expand All @@ -3117,7 +3105,7 @@ public static Map<String, Object> replaceEmptyStringWithEmptyValue(Map<String, O
private static Object makeReplaceEmptyString(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(
item instanceof Map ? replaceEmptyStringWithEmptyValue((Map) item) : item);
Expand All @@ -3132,7 +3120,7 @@ private static Object makeReplaceEmptyString(Object value) {
}

public static Map<String, Object> replaceNumberAndBooleanWithString(Map<String, Object> map) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
outMap.put(
entry.getKey(),
Expand All @@ -3147,7 +3135,7 @@ public static Map<String, Object> replaceNumberAndBooleanWithString(Map<String,
private static Object makeReplaceNumberAndBoolean(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
if (item instanceof Map) {
values.add(replaceNumberAndBooleanWithString((Map) item));
Expand All @@ -3174,7 +3162,7 @@ public static Map<String, Object> replaceFirstLevel(Map<String, Object> map) {

@SuppressWarnings("unchecked")
public static Map<String, Object> replaceFirstLevel(Map<String, Object> map, int level) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
outMap.put(entry.getKey(), makeReplaceFirstLevel(entry.getValue(), level + 1));
}
Expand All @@ -3193,7 +3181,7 @@ public static Map<String, Object> replaceFirstLevel(Map<String, Object> map, int
private static Object makeReplaceFirstLevel(Object value, int level) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(item instanceof Map ? replaceFirstLevel((Map) item, level + 1) : item);
}
Expand All @@ -3207,7 +3195,7 @@ private static Object makeReplaceFirstLevel(Object value, int level) {
}

public static Map<String, Object> replaceNilWithNull(Map<String, Object> map) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object outValue = makeReplaceNilWithNull(entry.getValue());
if (outValue instanceof Map
Expand All @@ -3227,7 +3215,7 @@ public static Map<String, Object> replaceNilWithNull(Map<String, Object> map) {
private static Object makeReplaceNilWithNull(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(item instanceof Map ? replaceNilWithNull((Map) item) : item);
}
Expand All @@ -3241,7 +3229,7 @@ private static Object makeReplaceNilWithNull(Object value) {
}

public static Map<String, Object> deepCopyMap(Map<String, Object> map) {
Map<String, Object> outMap = newLinkedHashMap();
Map<String, Object> outMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
outMap.put(entry.getKey(), makeDeepCopyMap(entry.getValue()));
}
Expand All @@ -3252,7 +3240,7 @@ public static Map<String, Object> deepCopyMap(Map<String, Object> map) {
private static Object makeDeepCopyMap(Object value) {
final Object result;
if (value instanceof List) {
List<Object> values = newArrayList();
List<Object> values = new ArrayList<>();
for (Object item : (List) value) {
values.add(item instanceof Map ? deepCopyMap((Map) item) : item);
}
Expand All @@ -3273,7 +3261,7 @@ public static class Builder {
private final Map<String, Object> data;

public Builder() {
data = newLinkedHashMap();
data = new LinkedHashMap<>();
}

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

public ArrayBuilder() {
data = newArrayList();
data = new ArrayList<>();
}

public ArrayBuilder add(final Object value) {
Expand Down
Loading

0 comments on commit 5c0b8e2

Please sign in to comment.