From 5c0b8e23cd262e5e65665e5a1dabbe0b2874f1fe Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 2 Oct 2023 11:56:17 +0300 Subject: [PATCH] Improved creation of ArrayList and LinkedHashMap --- src/main/java/com/github/underscore/U.java | 86 ++++---- .../com/github/underscore/Underscore.java | 102 ++++----- .../com/github/underscore/ArraysTest.java | 12 +- .../github/underscore/CollectionsTest.java | 4 +- .../com/github/underscore/LodashTest.java | 201 +++++++++--------- .../com/github/underscore/StringTest.java | 2 +- 6 files changed, 191 insertions(+), 216 deletions(-) diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index 5e7a8496..81c4c369 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -1045,7 +1045,7 @@ public List pull(Object... values) { } public static List pullAt(final List list, final Integer... indexes) { - final List result = newArrayList(); + final List result = new ArrayList<>(); final List indexesList = Arrays.asList(indexes); int index = 0; for (final Iterator iterator = list.iterator(); iterator.hasNext(); ) { @@ -1065,7 +1065,7 @@ public List pullAt(final Integer... indexes) { } public static List remove(final List list, final Predicate pred) { - final List result = newArrayList(); + final List result = new ArrayList<>(); for (final Iterator iterator = list.iterator(); iterator.hasNext(); ) { final T object = iterator.next(); if (pred.test(object)) { @@ -1149,7 +1149,7 @@ public List xor(final List list) { } public static List at(final List list, final Integer... indexes) { - final List result = newArrayList(); + final List result = new ArrayList<>(); final List indexesList = Arrays.asList(indexes); int index = 0; for (final T object : list) { @@ -1874,7 +1874,7 @@ public static T remove(final Map object, final List public static Map rename( final Map map, final String oldKey, final String newKey) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { if (entry.getKey().equals(oldKey)) { outMap.put(newKey, makeObjectForRename(entry.getValue(), oldKey, newKey)); @@ -1890,7 +1890,7 @@ private static Object makeObjectForRename( Object value, final String oldKey, final String newKey) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add( item instanceof Map @@ -1915,7 +1915,7 @@ public static Map setValue( final Map map, final String key, final BiFunction newValue) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { if (entry.getKey().equals(key)) { outMap.put( @@ -1934,7 +1934,7 @@ private static Object makeObjectForSetValue( Object value, final String key, final BiFunction newValue) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add( item instanceof Map @@ -1952,7 +1952,7 @@ private static Object makeObjectForSetValue( public static Map update( final Map map1, final Map map2) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map1.entrySet()) { String key = entry.getKey(); Object value2 = entry.getValue(); @@ -2309,7 +2309,7 @@ && nonNull(timeBetweenRetry) } public static List explode(final String input) { - List result = newArrayList(); + List result = new ArrayList<>(); if (isNull(input)) { return result; } @@ -2511,22 +2511,10 @@ public List> createPermutationWithRepetition(final int permutationLength return createPermutationWithRepetition((List) value(), permutationLength); } - protected static List newArrayList() { - return Underscore.newArrayList(); - } - protected static List newArrayList(final Iterable iterable) { return Underscore.newArrayList(iterable); } - protected static Set newLinkedHashSet() { - return Underscore.newLinkedHashSet(); - } - - protected static Map newLinkedHashMap() { - return Underscore.newLinkedHashMap(); - } - public static String join(final Iterable iterable, final String separator) { return Underscore.join(iterable, separator); } @@ -2615,7 +2603,7 @@ private static Map getStringObjectMap(Object object) { if (object instanceof Map) { result = (Map) object; } else { - result = newLinkedHashMap(); + result = new LinkedHashMap<>(); result.put("value", object); } return result; @@ -2652,7 +2640,7 @@ public static String jsonToXml( newRootName); } else if (mode == JsonToXmlMode.ADD_ROOT && !Xml.XmlValue.getMapKey(object).equals(ROOT)) { - final Map map = U.newLinkedHashMap(); + final Map map = new LinkedHashMap<>(); map.put(newRootName, object); result = Xml.toXml(map, identStep); } else if (mode == JsonToXmlMode.REMOVE_ARRAY_ATTRIBUTE) { @@ -2863,7 +2851,7 @@ public static String changeXmlEncoding(String xml, String encoding) { } public static Map removeMinusesAndConvertNumbers(Map map) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { final String newKey; if (entry.getKey().startsWith("-")) { @@ -2883,7 +2871,7 @@ public static Map removeMinusesAndConvertNumbers(Map values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add( item instanceof Map @@ -2947,7 +2935,7 @@ public static Map replaceSelfClosingWithEmpty(Map map, String value) { - Object outMap = newLinkedHashMap(); + Object outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { if (selfClosing.equals(entry.getKey()) && "true".equals(entry.getValue())) { if (map.size() == 1) { @@ -2968,7 +2956,7 @@ public static Object replaceSelfClosingWithValue(Map map, String private static Object makeObjectSelfClose(Object value, String newValue) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add( item instanceof Map @@ -2988,7 +2976,7 @@ public static Map replaceEmptyValueWithNull(Map if (map == null || map.isEmpty()) { return null; } - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { outMap.put(String.valueOf(entry.getKey()), makeObjectEmptyValue(entry.getValue())); } @@ -2999,7 +2987,7 @@ public static Map replaceEmptyValueWithNull(Map private static Object makeObjectEmptyValue(Object value) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add(item instanceof Map ? replaceEmptyValueWithNull((Map) item) : item); } @@ -3016,7 +3004,7 @@ public static Object replaceEmptyValueWithEmptyString(Map map) { if (map.isEmpty()) { return ""; } - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { outMap.put(String.valueOf(entry.getKey()), makeObjectEmptyString(entry.getValue())); } @@ -3027,7 +3015,7 @@ public static Object replaceEmptyValueWithEmptyString(Map map) { private static Object makeObjectEmptyString(Object value) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add( item instanceof Map ? replaceEmptyValueWithEmptyString((Map) item) : item); @@ -3042,7 +3030,7 @@ private static Object makeObjectEmptyString(Object value) { } public static Map forceAttributeUsage(Map map) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { outMap.put( entry.getValue() instanceof Map @@ -3059,7 +3047,7 @@ public static Map forceAttributeUsage(Map map) { private static Object makeAttributeUsage(Object value) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add(item instanceof Map ? forceAttributeUsage((Map) item) : item); } @@ -3073,12 +3061,12 @@ private static Object makeAttributeUsage(Object value) { } public static Map replaceNullWithEmptyValue(Map map) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { outMap.put( entry.getKey(), entry.getValue() == null - ? newLinkedHashMap() + ? (Map) new LinkedHashMap() : makeReplaceNullValue(entry.getValue())); } return outMap; @@ -3088,7 +3076,7 @@ public static Map replaceNullWithEmptyValue(Map private static Object makeReplaceNullValue(Object value) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add(item instanceof Map ? replaceNullWithEmptyValue((Map) item) : item); } @@ -3102,12 +3090,12 @@ private static Object makeReplaceNullValue(Object value) { } public static Map replaceEmptyStringWithEmptyValue(Map map) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { outMap.put( entry.getKey(), "".equals(entry.getValue()) - ? newLinkedHashMap() + ? (Map) new LinkedHashMap() : makeReplaceEmptyString(entry.getValue())); } return outMap; @@ -3117,7 +3105,7 @@ public static Map replaceEmptyStringWithEmptyValue(Map values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add( item instanceof Map ? replaceEmptyStringWithEmptyValue((Map) item) : item); @@ -3132,7 +3120,7 @@ private static Object makeReplaceEmptyString(Object value) { } public static Map replaceNumberAndBooleanWithString(Map map) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { outMap.put( entry.getKey(), @@ -3147,7 +3135,7 @@ public static Map replaceNumberAndBooleanWithString(Map values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { if (item instanceof Map) { values.add(replaceNumberAndBooleanWithString((Map) item)); @@ -3174,7 +3162,7 @@ public static Map replaceFirstLevel(Map map) { @SuppressWarnings("unchecked") public static Map replaceFirstLevel(Map map, int level) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { outMap.put(entry.getKey(), makeReplaceFirstLevel(entry.getValue(), level + 1)); } @@ -3193,7 +3181,7 @@ public static Map replaceFirstLevel(Map map, int private static Object makeReplaceFirstLevel(Object value, int level) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add(item instanceof Map ? replaceFirstLevel((Map) item, level + 1) : item); } @@ -3207,7 +3195,7 @@ private static Object makeReplaceFirstLevel(Object value, int level) { } public static Map replaceNilWithNull(Map map) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { Object outValue = makeReplaceNilWithNull(entry.getValue()); if (outValue instanceof Map @@ -3227,7 +3215,7 @@ public static Map replaceNilWithNull(Map map) { private static Object makeReplaceNilWithNull(Object value) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add(item instanceof Map ? replaceNilWithNull((Map) item) : item); } @@ -3241,7 +3229,7 @@ private static Object makeReplaceNilWithNull(Object value) { } public static Map deepCopyMap(Map map) { - Map outMap = newLinkedHashMap(); + Map outMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { outMap.put(entry.getKey(), makeDeepCopyMap(entry.getValue())); } @@ -3252,7 +3240,7 @@ public static Map deepCopyMap(Map map) { private static Object makeDeepCopyMap(Object value) { final Object result; if (value instanceof List) { - List values = newArrayList(); + List values = new ArrayList<>(); for (Object item : (List) value) { values.add(item instanceof Map ? deepCopyMap((Map) item) : item); } @@ -3273,7 +3261,7 @@ public static class Builder { private final Map data; public Builder() { - data = newLinkedHashMap(); + data = new LinkedHashMap<>(); } public Builder add(final String key, final Object value) { @@ -3406,7 +3394,7 @@ public static class ArrayBuilder { private final List data; public ArrayBuilder() { - data = newArrayList(); + data = new ArrayList<>(); } public ArrayBuilder add(final Object value) { diff --git a/src/main/java/com/github/underscore/Underscore.java b/src/main/java/com/github/underscore/Underscore.java index 691e7ac1..548070b8 100644 --- a/src/main/java/com/github/underscore/Underscore.java +++ b/src/main/java/com/github/underscore/Underscore.java @@ -67,7 +67,7 @@ "java:S5852" }) public class Underscore { - private static final Map> FUNCTIONS = newLinkedHashMap(); + private static final Map> FUNCTIONS = new LinkedHashMap<>(); private static final Map TEMPLATE_SETTINGS = new HashMap<>(); private static final int MIN_PASSWORD_LENGTH_8 = 8; private static final long CAPACITY_SIZE_5 = 5L; @@ -557,7 +557,7 @@ public static Optional findLast(final Iterable iterable, final Predica * Documented, #filter */ public static List filter(final Iterable iterable, final Predicate pred) { - final List filtered = newArrayList(); + final List filtered = new ArrayList<>(); for (E element : iterable) { if (pred.test(element)) { filtered.add(element); @@ -567,7 +567,7 @@ public static List filter(final Iterable iterable, final Predicate } public static List filter(final List list, final Predicate pred) { - final List filtered = newArrayList(); + final List filtered = new ArrayList<>(); for (E element : list) { if (pred.test(element)) { filtered.add(element); @@ -577,7 +577,7 @@ public static List filter(final List list, final Predicate pred) { } public List filter(final Predicate pred) { - final List filtered = newArrayList(); + final List filtered = new ArrayList<>(); for (final T element : value()) { if (pred.test(element)) { filtered.add(element); @@ -587,7 +587,7 @@ public List filter(final Predicate pred) { } public static List filterIndexed(final List list, final PredicateIndexed pred) { - final List filtered = newArrayList(); + final List filtered = new ArrayList<>(); int index = 0; for (E element : list) { if (pred.test(index, element)) { @@ -599,7 +599,7 @@ public static List filterIndexed(final List list, final PredicateIndex } public static Set filter(final Set set, final Predicate pred) { - final Set filtered = newLinkedHashSet(); + final Set filtered = new LinkedHashSet<>(); for (E element : set) { if (pred.test(element)) { filtered.add(element); @@ -778,7 +778,7 @@ public static boolean include(final Iterable iterable, final E elem) { @SuppressWarnings("unchecked") public static List invoke( final Iterable iterable, final String methodName, final List args) { - final List result = newArrayList(); + final List result = new ArrayList<>(); final List> argTypes = map(args, Object::getClass); try { final Method method = @@ -1009,14 +1009,14 @@ public static > List> sortBy( */ public static Map> groupBy( final Iterable iterable, final Function func) { - final Map> retVal = newLinkedHashMap(); + final Map> retVal = new LinkedHashMap<>(); for (E e : iterable) { final K key = func.apply(e); List val; if (retVal.containsKey(key)) { val = retVal.get(key); } else { - val = newArrayList(); + val = new ArrayList<>(); } val.add(e); retVal.put(key, val); @@ -1033,7 +1033,7 @@ public static Map> groupBy( final Iterable iterable, final Function func, final BinaryOperator binaryOperator) { - final Map> retVal = newLinkedHashMap(); + final Map> retVal = new LinkedHashMap<>(); for (Map.Entry> entry : groupBy(iterable, func).entrySet()) { retVal.put(entry.getKey(), reduce(entry.getValue(), binaryOperator)); } @@ -1048,7 +1048,7 @@ public Map> groupBy( public static Map associateBy( final Iterable iterable, final Function func) { - final Map retVal = newLinkedHashMap(); + final Map retVal = new LinkedHashMap<>(); for (E e : iterable) { final K key = func.apply(e); retVal.putIfAbsent(key, e); @@ -1084,7 +1084,7 @@ public Map> indexBy(final String property) { * Documented, #countBy */ public static Map countBy(final Iterable iterable, Function func) { - final Map retVal = newLinkedHashMap(); + final Map retVal = new LinkedHashMap<>(); for (E e : iterable) { final K key = func.apply(e); if (retVal.containsKey(key)) { @@ -1097,7 +1097,7 @@ public static Map countBy(final Iterable iterable, Functio } public static Map countBy(final Iterable iterable) { - final Map retVal = newLinkedHashMap(); + final Map retVal = new LinkedHashMap<>(); for (K key : iterable) { if (retVal.containsKey(key)) { retVal.put(key, 1 + retVal.get(key)); @@ -1135,7 +1135,7 @@ public E[] toArray() { * Documented, #toMap */ public static Map toMap(final Iterable> iterable) { - final Map result = newLinkedHashMap(); + final Map result = new LinkedHashMap<>(); for (Map.Entry entry : iterable) { result.put(entry.getKey(), entry.getValue()); } @@ -1148,7 +1148,7 @@ public Map toMap() { } public static Map toMap(final List> tuples) { - final Map result = newLinkedHashMap(); + final Map result = new LinkedHashMap<>(); for (final Map.Entry entry : tuples) { result.put(entry.getKey(), entry.getValue()); } @@ -1161,7 +1161,7 @@ public Map toCardinalityMap() { public static Map toCardinalityMap(final Iterable iterable) { Iterator iterator = iterable.iterator(); - Map result = newLinkedHashMap(); + Map result = new LinkedHashMap<>(); while (iterator.hasNext()) { K item = iterator.next(); @@ -1200,8 +1200,8 @@ public static int size(final E... array) { } public static List> partition(final Iterable iterable, final Predicate pred) { - final List retVal1 = newArrayList(); - final List retVal2 = newArrayList(); + final List retVal1 = new ArrayList<>(); + final List retVal2 = new ArrayList<>(); for (final E e : iterable) { if (pred.test(e)) { retVal1.add(e); @@ -1511,13 +1511,13 @@ public List compact(final T falsyValue) { * Documented, #flatten */ public static List flatten(final List list) { - List flattened = newArrayList(); + List flattened = new ArrayList<>(); flatten(list, flattened, -1); return flattened; } public static List flatten(final List list, final boolean shallow) { - List flattened = newArrayList(); + List flattened = new ArrayList<>(); flatten(list, flattened, shallow ? 1 : -1); return flattened; } @@ -1569,7 +1569,7 @@ public static E[] uniq(final E... array) { } public static Collection uniq(final Iterable iterable, final Function func) { - final Map retVal = newLinkedHashMap(); + final Map retVal = new LinkedHashMap<>(); for (final E e : iterable) { final K key = func.apply(e); retVal.put(key, e); @@ -1605,7 +1605,7 @@ public static E[] distinctBy(final E[] array, final Function func) */ @SuppressWarnings("unchecked") public static List union(final List list, final List... lists) { - final Set union = newLinkedHashSet(); + final Set union = new LinkedHashSet<>(); union.addAll(list); for (List localList : lists) { union.addAll(localList); @@ -1620,7 +1620,7 @@ public List unionWith(final List... lists) { @SuppressWarnings("unchecked") public static E[] union(final E[]... arrays) { - final Set union = newLinkedHashSet(); + final Set union = new LinkedHashSet<>(); for (E[] array : arrays) { union.addAll(Arrays.asList(array)); } @@ -1631,7 +1631,7 @@ public static E[] union(final E[]... arrays) { * Documented, #intersection */ public static List intersection(final List list1, final List list2) { - final List result = newArrayList(); + final List result = new ArrayList<>(); for (final E item : list1) { if (list2.contains(item)) { result.add(item); @@ -1669,7 +1669,7 @@ public static E[] intersection(final E[]... arrays) { * Documented, #difference */ public static List difference(final List list1, final List list2) { - final List result = newArrayList(); + final List result = new ArrayList<>(); for (final E item : list1) { if (!list2.contains(item)) { result.add(item); @@ -1708,16 +1708,14 @@ public static E[] difference(final E[]... arrays) { */ @SuppressWarnings("unchecked") public static List> zip(final List... lists) { - final List> zipped = newArrayList(); + final List> zipped = new ArrayList<>(); each( Arrays.asList(lists), list -> { int index = 0; for (T elem : list) { - final List nTuple = - index >= zipped.size() - ? Underscore.newArrayList() - : zipped.get(index); + final List nTuple; + nTuple = index >= zipped.size() ? new ArrayList() : zipped.get(index); if (index >= zipped.size()) { zipped.add(nTuple); } @@ -1730,9 +1728,9 @@ public static List> zip(final List... lists) { @SuppressWarnings("unchecked") public static List> unzip(final List... lists) { - final List> unzipped = newArrayList(); + final List> unzipped = new ArrayList<>(); for (int index = 0; index < lists[0].size(); index += 1) { - final List nTuple = newArrayList(); + final List nTuple = new ArrayList<>(); for (List list : lists) { nTuple.add(list.get(index)); } @@ -1893,7 +1891,7 @@ public static List range(int start, int stop) { } public static List range(int start, int stop, int step) { - List list = Underscore.newArrayList(); + List list = new ArrayList<>(); if (step == 0) { return list; } @@ -1918,7 +1916,7 @@ public static List range(char start, char stop) { } public static List range(char start, char stop, int step) { - List list = Underscore.newArrayList(); + List list = new ArrayList<>(); if (step == 0) { return list; } @@ -1936,7 +1934,7 @@ public static List range(char start, char stop, int step) { public static List> chunk(final Iterable iterable, final int size) { if (size <= 0) { - return newArrayList(); + return new ArrayList<>(); } return chunk(iterable, size, size); } @@ -1944,7 +1942,7 @@ public static List> chunk(final Iterable iterable, final int size public static List> chunk( final Iterable iterable, final int size, final int step) { if (step <= 0 || size < 0) { - return newArrayList(); + return new ArrayList<>(); } int index = 0; int length = size(iterable); @@ -1959,7 +1957,7 @@ public static List> chunk( public static List> chunkFill( final Iterable iterable, final int size, final T fillValue) { if (size <= 0) { - return newArrayList(); + return new ArrayList<>(); } return chunkFill(iterable, size, size, fillValue); } @@ -1967,7 +1965,7 @@ public static List> chunkFill( public static List> chunkFill( final Iterable iterable, final int size, final int step, final T fillValue) { if (step <= 0 || size < 0) { - return newArrayList(); + return new ArrayList<>(); } final List> result = chunk(iterable, size, step); int difference = size - result.get(result.size() - 1).size(); @@ -1996,7 +1994,7 @@ public List> chunkFill(final int size, final int step, T fillvalue) { public static List cycle(final Iterable iterable, final int times) { int size = Math.abs(size(iterable) * times); if (size == 0) { - return newArrayList(); + return new ArrayList<>(); } List list = newArrayListWithExpectedSize(size); int round = 0; @@ -2019,7 +2017,7 @@ public List cycle(final int times) { public static List repeat(final T element, final int times) { if (times <= 0) { - return newArrayList(); + return new ArrayList<>(); } List result = newArrayListWithExpectedSize(times); for (int i = 0; i < times; i++) { @@ -2341,7 +2339,7 @@ public static List> invert(final Map object) { * Documented, #functions */ public static List functions(final Object object) { - final List result = newArrayList(); + final List result = new ArrayList<>(); for (final Method method : object.getClass().getDeclaredMethods()) { result.add(method.getName()); } @@ -2357,7 +2355,7 @@ public static List methods(final Object object) { */ @SuppressWarnings("unchecked") public static Map extend(final Map destination, final Map... sources) { - final Map result = newLinkedHashMap(); + final Map result = new LinkedHashMap<>(); result.putAll(destination); for (final Map source : sources) { result.putAll(source); @@ -2463,7 +2461,7 @@ public static List> omit( * Documented, #defaults */ public static Map defaults(final Map object, final Map defaults) { - final Map result = newLinkedHashMap(); + final Map result = new LinkedHashMap<>(); result.putAll(defaults); result.putAll(object); return result; @@ -2748,7 +2746,7 @@ public static String format(final String template, final Object... params) { } matcher.appendTail(buffer); final String newTemplate = buffer.toString(); - final Map args = newLinkedHashMap(); + final Map args = new LinkedHashMap<>(); index = 0; for (Object param : params) { args.put(index, param.toString()); @@ -3488,7 +3486,7 @@ public List slice(final int start, final int end) { } public static List> splitAt(final Iterable iterable, final int position) { - List> result = newArrayList(); + List> result = new ArrayList<>(); int size = size(iterable); final int index; if (position < 0) { @@ -3510,7 +3508,7 @@ public List> splitAt(final int position) { } public static List takeSkipping(final Iterable iterable, final int stepSize) { - List result = newArrayList(); + List result = new ArrayList<>(); if (stepSize <= 0) { return result; } @@ -3719,10 +3717,6 @@ public static T defaultTo(T value, T defaultValue) { return value; } - protected static List newArrayList() { - return new ArrayList<>(); - } - protected static List newArrayList(final Iterable iterable) { final List result; if (iterable instanceof Collection) { @@ -3762,10 +3756,6 @@ protected static List newArrayListWithExpectedSize(int size) { return new ArrayList<>((int) (CAPACITY_SIZE_5 + size + (size / 10))); } - protected static Set newLinkedHashSet() { - return new LinkedHashSet<>(); - } - protected static Set newLinkedHashSet(Iterable iterable) { final Set result = new LinkedHashSet<>(); for (final T item : iterable) { @@ -3778,10 +3768,6 @@ protected static Set newLinkedHashSetWithExpectedSize(int size) { return new LinkedHashSet<>((int) Math.max(size * CAPACITY_COEFF_2, CAPACITY_SIZE_16)); } - protected static Map newLinkedHashMap() { - return new LinkedHashMap<>(); - } - @SuppressWarnings("unchecked") public static Predicate and( final Predicate pred1, diff --git a/src/test/java/com/github/underscore/ArraysTest.java b/src/test/java/com/github/underscore/ArraysTest.java index 5ae88215..32375166 100644 --- a/src/test/java/com/github/underscore/ArraysTest.java +++ b/src/test/java/com/github/underscore/ArraysTest.java @@ -318,7 +318,7 @@ void chunkFill() { @Test void cycle() { assertEquals("[]", Underscore.cycle(Underscore.range(5), 0).toString()); - assertEquals("[]", Underscore.cycle(Underscore.newArrayList(), 5).toString()); + assertEquals("[]", Underscore.cycle(new ArrayList(), 5).toString()); assertEquals("[4, 3, 2, 1, 0]", Underscore.cycle(Underscore.range(5), -1).toString()); assertEquals( "[0, 1, 2, 0, 1, 2, 0, 1, 2]", Underscore.cycle(Underscore.range(3), 3).toString()); @@ -370,8 +370,8 @@ void interpose() { assertEquals( "[0, 500, 1, 500, 2, 500, 3]", Underscore.interpose(Underscore.range(4), 500).toString()); - assertEquals("[]", Underscore.interpose(Underscore.newArrayList(), 500).toString()); - assertEquals("[]", Underscore.interpose(Underscore.newArrayList(), null).toString()); + assertEquals("[]", Underscore.interpose(new ArrayList(), 500).toString()); + assertEquals("[]", Underscore.interpose(new ArrayList(), null).toString()); assertEquals( "[0, 1, 2, 3]", Underscore.interpose(Underscore.newArrayList(Underscore.range(4)), null) @@ -385,7 +385,7 @@ void interpose() { assertEquals("[a]", Underscore.chain(singletonList("a")).interpose("interpose").toString()); assertEquals( "[]", - Underscore.chain(Underscore.newArrayList()).interpose("interpose").toString()); + Underscore.chain(new ArrayList()).interpose("interpose").toString()); assertEquals( "[a, b, c]", Underscore.chain(asList("a", "b", "c")).interpose(null).toString()); assertEquals( @@ -405,8 +405,8 @@ void interpose() { */ @Test void interposeByList() { - List list1 = Underscore.newArrayList(); - List list2 = Underscore.newArrayList(); + List list1 = new ArrayList<>(); + List list2 = new ArrayList<>(); assertEquals( "[0, 100, 1, 200, 2, 300, 3]", Underscore.interposeByList(Underscore.range(4), Underscore.range(100, 600, 100)) diff --git a/src/test/java/com/github/underscore/CollectionsTest.java b/src/test/java/com/github/underscore/CollectionsTest.java index 4d783dfe..4990b523 100644 --- a/src/test/java/com/github/underscore/CollectionsTest.java +++ b/src/test/java/com/github/underscore/CollectionsTest.java @@ -1684,12 +1684,12 @@ void toCardinalityMap() { assertEquals( "{a=2, b=1, c=2}", Underscore.toCardinalityMap(asList("a", "a", "b", "c", "c")).toString()); - assertEquals("{}", Underscore.toCardinalityMap(Underscore.newArrayList()).toString()); + assertEquals("{}", Underscore.toCardinalityMap(new ArrayList()).toString()); assertEquals( "{a=2, b=1, c=2}", new Underscore<>(asList("a", "a", "b", "c", "c")).toCardinalityMap().toString()); assertEquals( - "{}", new Underscore<>(Underscore.newArrayList()).toCardinalityMap().toString()); + "{}", new Underscore<>(new ArrayList()).toCardinalityMap().toString()); } /* diff --git a/src/test/java/com/github/underscore/LodashTest.java b/src/test/java/com/github/underscore/LodashTest.java index 31b06a77..fbbd72c6 100644 --- a/src/test/java/com/github/underscore/LodashTest.java +++ b/src/test/java/com/github/underscore/LodashTest.java @@ -41,6 +41,7 @@ import java.util.Comparator; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.function.Predicate; @@ -421,19 +422,19 @@ void set() { "a[0]", "e") .toString()); - Map map = U.newLinkedHashMap(); - Map map2 = U.newLinkedHashMap(); - Map map3 = U.newLinkedHashMap(); + Map map = new LinkedHashMap<>(); + Map map2 = new LinkedHashMap<>(); + Map map3 = new LinkedHashMap<>(); map.put("a", map2); map2.put("#item", map3); map3.put("b", "c"); assertEquals("c", U.set(map, "a.b", "b").toString()); assertNull(U.set((Map) null, "a", "b")); - assertNull(U.set(U.newLinkedHashMap(), "a.b", "b")); - Map map4 = U.newLinkedHashMap(); + assertNull(U.set(new LinkedHashMap(), "a.b", "b")); + Map map4 = new LinkedHashMap<>(); map4.put("a", "b"); assertNull(U.set(map4, "a.b", "b")); - Map map5 = U.newLinkedHashMap(); + Map map5 = new LinkedHashMap<>(); map5.put("a", "b"); assertNull(U.chain(map5.entrySet()).set(asList("a", "b"), "b").value()); } @@ -787,31 +788,31 @@ void fetchWrongUrlWithRetry2() { @Test void toJson() { - Map map = U.newLinkedHashMap(); + Map map = new LinkedHashMap<>(); map.put(null, 3); assertEquals("{\n \"null\": 3\n}", U.toJson(map)); - Map map2 = U.newLinkedHashMap(); + Map map2 = new LinkedHashMap<>(); map2.put(1, 3); assertEquals("{\n \"1\": 3\n}", U.toJson(map2)); - Map map3 = U.newLinkedHashMap(); + Map map3 = new LinkedHashMap<>(); map3.put(true, 3); assertEquals("{\n \"true\": 3\n}", U.toJson(map3)); } @Test void toXml() { - Map map = U.newLinkedHashMap(); + Map map = new LinkedHashMap<>(); map.put(null, 3); assertEquals( "\n3", U.toXml(map)); - Map map2 = U.newLinkedHashMap(); + Map map2 = new LinkedHashMap<>(); map2.put(1, 3); assertEquals( "\n" + "<__GE__ number=\"true\">3", U.toXml(map2)); - Map map3 = U.newLinkedHashMap(); + Map map3 = new LinkedHashMap<>(); map3.put(true, 3); assertEquals( "\n3", @@ -917,15 +918,15 @@ void xmlToJson() { + " \"#omit-xml-declaration\": \"yes\"\n" + "}", U.xmlToJson("", U.XmlToJsonMode.REPLACE_SELF_CLOSING_WITH_NULL)); - Map map = U.newLinkedHashMap(); + Map map = new LinkedHashMap<>(); map.put("-self-closing", "false"); U.replaceSelfClosingWithNull(map); - Map mapEmpty = U.newLinkedHashMap(); + Map mapEmpty = new LinkedHashMap<>(); mapEmpty.put("-self-closing", "true"); U.replaceSelfClosingWithEmpty(mapEmpty); - Map map2 = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newArrayList()); + Map map2 = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new ArrayList()); map2.put("list", list); U.replaceSelfClosingWithNull(map2); assertEquals( @@ -940,15 +941,15 @@ void xmlToJson() { + "}", U.xmlToJson( "", U.XmlToJsonMode.REPLACE_EMPTY_VALUE_WITH_NULL)); - Map map3 = U.newLinkedHashMap(); - List list2 = U.newArrayList(); - list2.add(U.newArrayList()); + Map map3 = new LinkedHashMap<>(); + List list2 = new ArrayList<>(); + list2.add(new ArrayList()); map3.put("list", list2); U.replaceEmptyValueWithNull(map3); U.replaceEmptyValueWithNull(null); - Map map4 = U.newLinkedHashMap(); - List list3 = U.newArrayList(); - list3.add(U.newArrayList()); + Map map4 = new LinkedHashMap<>(); + List list3 = new ArrayList<>(); + list3.add(new ArrayList()); map4.put("list", list3); U.replaceEmptyValueWithEmptyString(map4); } @@ -1002,24 +1003,24 @@ void xmlOrJsonToXml() { @Test void removeMapKey() { - Map map = U.newLinkedHashMap(); + Map map = new LinkedHashMap<>(); map.put("-self-closing", "false"); U.remove(map, "test"); U.remove(map, "-self-closing"); - Map map2 = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newArrayList()); - list.add(U.newLinkedHashMap()); + Map map2 = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new ArrayList()); + list.add(new LinkedHashMap()); map2.put("list", list); U.remove(map2, "test"); - map2.put("list2", U.newLinkedHashMap()); + map2.put("list2", new LinkedHashMap()); U.remove(map2, "test"); U.remove(map2, "list.0"); } @Test void updateMapValue() { - Map map = U.newLinkedHashMap(); + Map map = new LinkedHashMap<>(); map.put("-self-closing1", "true"); map.put("-self-closing2", "false"); U.update(map, "-self-closing1", "false"); @@ -1036,18 +1037,18 @@ void updateMapValue() { @Test void renameMapKey() { - Map map = U.newLinkedHashMap(); + Map map = new LinkedHashMap<>(); map.put("-self-closing", "false"); U.rename(map, "test", "test1"); Map newMap = U.rename(map, "-self-closing", "-self-closing1"); assertEquals("{\n" + " \"-self-closing1\": \"false\"\n" + "}", U.toJson(newMap)); - Map map2 = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newArrayList()); - list.add(U.newLinkedHashMap()); + Map map2 = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new ArrayList()); + list.add(new LinkedHashMap()); map2.put("list", list); U.rename(map2, "test", "test1"); - map2.put("list", U.newLinkedHashMap()); + map2.put("list", new LinkedHashMap()); U.rename(map2, "test", "test1"); } @@ -1121,13 +1122,13 @@ void forceAddRoot() { + " ]\n" + "}", U.JsonToXmlMode.ADD_ROOT)); - Map map2 = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newArrayList()); - list.add(U.newLinkedHashMap()); + Map map2 = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new ArrayList()); + list.add(new LinkedHashMap()); map2.put("list", list); U.replaceNumberAndBooleanWithString(map2); - map2.put("list", U.newLinkedHashMap()); + map2.put("list", new LinkedHashMap()); U.replaceNumberAndBooleanWithString(map2); } @@ -1187,40 +1188,40 @@ void forceRemoveArrayTrueBooleanAndNumber() { @Test void updateMapKey() { - Map map = U.newLinkedHashMap(); + Map map = new LinkedHashMap<>(); map.put("-self-closing", "false"); U.rename(map, "test", "test1"); Map newMap = U.update(map, map); assertEquals("{\n" + " \"-self-closing\": \"false\"\n" + "}", U.toJson(newMap)); - Map map2 = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newArrayList()); - list.add(U.newLinkedHashMap()); + Map map2 = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new ArrayList()); + list.add(new LinkedHashMap()); map2.put("list", list); U.update(map2, map2); - map2.put("list", U.newLinkedHashMap()); + map2.put("list", new LinkedHashMap()); U.update(map2, map2); U.update(map2, map); - Map map3 = U.newLinkedHashMap(); - map3.put("list", U.newArrayList()); + Map map3 = new LinkedHashMap<>(); + map3.put("list", new ArrayList()); U.update(map2, map3); U.update(map3, map2); } @Test void setValue() { - Map map = U.newLinkedHashMap(); + Map map = new LinkedHashMap<>(); map.put("-self-closing", "false"); U.setValue(map, "test", "test1"); Map newMap = U.setValue(map, "-self-closing", "true"); assertEquals("{\n" + " \"-self-closing\": \"true\"\n" + "}", U.toJson(newMap)); - Map map2 = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newArrayList()); - list.add(U.newLinkedHashMap()); + Map map2 = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new ArrayList()); + list.add(new LinkedHashMap()); map2.put("list", list); U.setValue(map2, "test", "test1"); - map2.put("list", U.newLinkedHashMap()); + map2.put("list", new LinkedHashMap()); U.setValue(map2, "test", "test1"); } @@ -1343,14 +1344,14 @@ void forceAttributeUsage() { + " }\n" + "}", U.JsonToXmlMode.FORCE_ATTRIBUTE_USAGE)); - Map map = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newLinkedHashMap()); + Map map = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new LinkedHashMap()); map.put("list", list); U.forceAttributeUsage(map); - Map map2 = U.newLinkedHashMap(); - List list2 = U.newArrayList(); - list2.add(U.newArrayList()); + Map map2 = new LinkedHashMap<>(); + List list2 = new ArrayList<>(); + list2.add(new ArrayList()); map2.put("list", list2); U.forceAttributeUsage(map2); } @@ -1375,14 +1376,14 @@ void replaceNullWithEmptyValue() { + " }\n" + "}", U.JsonToXmlMode.REPLACE_NULL_WITH_EMPTY_VALUE)); - Map map = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newLinkedHashMap()); + Map map = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new LinkedHashMap()); map.put("list", list); U.replaceNullWithEmptyValue(map); - Map map2 = U.newLinkedHashMap(); - List list2 = U.newArrayList(); - list2.add(U.newArrayList()); + Map map2 = new LinkedHashMap<>(); + List list2 = new ArrayList<>(); + list2.add(new ArrayList()); map2.put("list", list2); U.replaceNullWithEmptyValue(map2); } @@ -1429,20 +1430,20 @@ void replaceNilWithNull() { + " \n" + " \n" + "")))); - Map map = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newLinkedHashMap()); + Map map = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new LinkedHashMap()); map.put("list", list); U.replaceNilWithNull(map); - Map map2 = U.newLinkedHashMap(); - List list2 = U.newArrayList(); - list2.add(U.newArrayList()); + Map map2 = new LinkedHashMap<>(); + List list2 = new ArrayList<>(); + list2.add(new ArrayList()); map2.put("list", list2); U.replaceNilWithNull(map2); - Map map3 = U.newLinkedHashMap(); + Map map3 = new LinkedHashMap<>(); map3.put("-nil", "true"); map3.put("-self-closing", "true1"); - Map map4 = U.newLinkedHashMap(); + Map map4 = new LinkedHashMap<>(); map4.put("map", map3); U.replaceNilWithNull(map4); } @@ -1467,14 +1468,14 @@ void replaceEmptyStringWithEmptyValue() { + " }\n" + "}", U.JsonToXmlMode.REPLACE_EMPTY_STRING_WITH_EMPTY_VALUE)); - Map map = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newLinkedHashMap()); + Map map = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new LinkedHashMap()); map.put("list", list); U.replaceEmptyStringWithEmptyValue(map); - Map map2 = U.newLinkedHashMap(); - List list2 = U.newArrayList(); - list2.add(U.newArrayList()); + Map map2 = new LinkedHashMap<>(); + List list2 = new ArrayList<>(); + list2.add(new ArrayList()); map2.put("list", list2); U.replaceEmptyStringWithEmptyValue(map2); } @@ -1547,9 +1548,9 @@ void removeMinusesAndConvertNumbers() { U.removeMinusesAndConvertNumbers( (Map) U.fromXml("")); assertEquals("{a={b={c=+1ee}}}", result9.toString()); - Map map = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newArrayList()); + Map map = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new ArrayList()); map.put("list", list); Map result10 = U.removeMinusesAndConvertNumbers(map); assertEquals("{list=[[]]}", result10.toString()); @@ -1565,14 +1566,14 @@ void replaceFirstLevel() { assertEquals("{b=c}", result2.toString()); String result3 = U.xmlToJson("c", U.XmlToJsonMode.REMOVE_FIRST_LEVEL); assertEquals("{\n \"b\": \"c\"\n}", result3); - Map map = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newLinkedHashMap()); + Map map = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new LinkedHashMap()); map.put("list", list); U.replaceFirstLevel(map); - Map map2 = U.newLinkedHashMap(); - List list2 = U.newArrayList(); - list2.add(U.newArrayList()); + Map map2 = new LinkedHashMap<>(); + List list2 = new ArrayList<>(); + list2.add(new ArrayList()); map2.put("list", list2); U.replaceFirstLevel(map2); Map result4 = U.fromXml(""); @@ -1587,14 +1588,14 @@ void deepCopyMap() { assertEquals("{a={-self-closing=true}, #omit-xml-declaration=yes}", result.toString()); Map result2 = U.deepCopyMap(U.fromXml("c")); assertEquals("{a={b=c}, #omit-xml-declaration=yes}", result2.toString()); - Map map = U.newLinkedHashMap(); - List list = U.newArrayList(); - list.add(U.newLinkedHashMap()); + Map map = new LinkedHashMap<>(); + List list = new ArrayList<>(); + list.add(new LinkedHashMap()); map.put("list", list); U.deepCopyMap(map); - Map map2 = U.newLinkedHashMap(); - List list2 = U.newArrayList(); - list2.add(U.newArrayList()); + Map map2 = new LinkedHashMap<>(); + List list2 = new ArrayList<>(); + list2.add(new ArrayList()); map2.put("list", list2); U.deepCopyMap(map2); Map result3 = U.fromXml(""); @@ -1610,9 +1611,9 @@ void objectBuilder() { U.Builder.fromJson("{}"); builder.toXml(); U.Builder.fromXml(""); - U.Builder.fromMap(U.newLinkedHashMap()); - builder.add(U.newLinkedHashMap()); - builder.update(U.newLinkedHashMap()); + U.Builder.fromMap(new LinkedHashMap()); + builder.add(new LinkedHashMap()); + builder.update(new LinkedHashMap()); builder.set("1", "3"); builder.toString(); assertEquals("{1=3}", builder.build().toString()); @@ -1736,7 +1737,7 @@ void main() { U.of(new int[] {}); U.of(""); U.of(new LinkedHashMap<>()); - U.newLinkedHashSet(); + new LinkedHashSet<>(); } @SuppressWarnings("unchecked") diff --git a/src/test/java/com/github/underscore/StringTest.java b/src/test/java/com/github/underscore/StringTest.java index 7f789a64..60061af1 100644 --- a/src/test/java/com/github/underscore/StringTest.java +++ b/src/test/java/com/github/underscore/StringTest.java @@ -84,7 +84,7 @@ void camelCase() { @Test void explode() { assertEquals(asList("a", "b", "c"), U.explode("abc")); - assertEquals(U.newArrayList(), U.explode(null)); + assertEquals(new ArrayList(), U.explode(null)); } /*