Skip to content

Commit

Permalink
Use diamonds
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Aug 24, 2023
1 parent ea78167 commit af6fff1
Show file tree
Hide file tree
Showing 83 changed files with 188 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static <K, V, C extends Collection<V>> MultiValueMap<K, V> multiValueMap(
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public MultiValueMap() {
this(new HashMap<K, V>(), new ReflectionFactory(ArrayList.class));
this(new HashMap<>(), new ReflectionFactory(ArrayList.class));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public ArrayListValuedHashMap(final int initialListCapacity) {
* @param initialListCapacity the initial capacity used for value collections
*/
public ArrayListValuedHashMap(final int initialMapCapacity, final int initialListCapacity) {
super(new HashMap<K, ArrayList<V>>(initialMapCapacity));
super(new HashMap<>(initialMapCapacity));
this.initialListCapacity = initialListCapacity;
}

Expand Down Expand Up @@ -135,7 +135,7 @@ private void writeObject(final ObjectOutputStream oos) throws IOException {

private void readObject(final ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
setMap(new HashMap<K, ArrayList<V>>());
setMap(new HashMap<>());
doReadObject(ois);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public HashSetValuedHashMap(final int initialSetCapacity) {
* @param initialSetCapacity the initial capacity used for value collections
*/
public HashSetValuedHashMap(final int initialMapCapacity, final int initialSetCapacity) {
super(new HashMap<K, HashSet<V>>(initialMapCapacity));
super(new HashMap<>(initialMapCapacity));
this.initialSetCapacity = initialSetCapacity;
}

Expand Down Expand Up @@ -124,7 +124,7 @@ private void writeObject(final ObjectOutputStream oos) throws IOException {

private void readObject(final ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
setMap(new HashMap<K, HashSet<V>>());
setMap(new HashMap<>());
doReadObject(ois);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void testSwitchClosure() {
assertEquals(1, c.count);

assertEquals(NOPClosure.INSTANCE, ClosureUtils.<String>switchClosure(new Predicate[0], new Closure[0]));
assertEquals(NOPClosure.INSTANCE, ClosureUtils.<String>switchClosure(new HashMap<Predicate<String>, Closure<String>>()));
assertEquals(NOPClosure.INSTANCE, ClosureUtils.<String>switchClosure(new HashMap<>()));
map.clear();
map.put(null, null);
assertEquals(NOPClosure.INSTANCE, ClosureUtils.switchClosure(map));
Expand Down Expand Up @@ -309,7 +309,7 @@ public void testSwitchMapClosure() {
assertEquals(0, b.count);
assertEquals(1, c.count);

assertEquals(NOPClosure.INSTANCE, ClosureUtils.switchMapClosure(new HashMap<String, Closure<String>>()));
assertEquals(NOPClosure.INSTANCE, ClosureUtils.switchMapClosure(new HashMap<>()));

assertThrows(NullPointerException.class, () -> ClosureUtils.switchMapClosure(null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,12 @@ public void collect() {
assertCollectResult(collection);

ArrayList<Number> list;
list = CollectionUtils.collect(collectionA, transformer, new ArrayList<Number>());
list = CollectionUtils.collect(collectionA, transformer, new ArrayList<>());
assertEquals(list.size(), collectionA.size());
assertCollectResult(list);

Iterator<Integer> iterator = null;
list = CollectionUtils.collect(iterator, transformer, new ArrayList<Number>());
list = CollectionUtils.collect(iterator, transformer, new ArrayList<>());

iterator = iterableA.iterator();
list = CollectionUtils.collect(iterator, transformer, list);
Expand Down Expand Up @@ -1023,7 +1023,7 @@ private <T> void next(final Iterator<T> iterator, final T t) {
@Test
public void predicatedCollection() {
final Predicate<Object> predicate = PredicateUtils.instanceofPredicate(Integer.class);
final Collection<Number> collection = CollectionUtils.predicatedCollection(new ArrayList<Number>(), predicate);
final Collection<Number> collection = CollectionUtils.predicatedCollection(new ArrayList<>(), predicate);
assertTrue(collection instanceof PredicatedCollection, "returned object should be a PredicatedCollection");
}

Expand All @@ -1047,7 +1047,7 @@ public void select() {
// Ensure that the collection is the input type or a super type
final Collection<Integer> output1 = CollectionUtils.select(list, EQUALS_TWO);
final Collection<Number> output2 = CollectionUtils.<Number>select(list, EQUALS_TWO);
final HashSet<Number> output3 = CollectionUtils.select(list, EQUALS_TWO, new HashSet<Number>());
final HashSet<Number> output3 = CollectionUtils.select(list, EQUALS_TWO, new HashSet<>());
assertTrue(CollectionUtils.isEqualCollection(output1, output3));
assertEquals(4, list.size());
assertEquals(1, output1.size());
Expand All @@ -1063,7 +1063,7 @@ public void selectRejected() {
list.add(4L);
final Collection<Long> output1 = CollectionUtils.selectRejected(list, EQUALS_TWO);
final Collection<? extends Number> output2 = CollectionUtils.selectRejected(list, EQUALS_TWO);
final HashSet<Number> output3 = CollectionUtils.selectRejected(list, EQUALS_TWO, new HashSet<Number>());
final HashSet<Number> output3 = CollectionUtils.selectRejected(list, EQUALS_TWO, new HashSet<>());
assertTrue(CollectionUtils.isEqualCollection(output1, output2));
assertTrue(CollectionUtils.isEqualCollection(output1, output3));
assertEquals(4, list.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public void testToString() {
String result = IterableUtils.toString(iterableA);
assertEquals("[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]", result);

result = IterableUtils.toString(new ArrayList<Integer>());
result = IterableUtils.toString(new ArrayList<>());
assertEquals("[]", result);

result = IterableUtils.toString(null);
Expand All @@ -441,7 +441,7 @@ public void testToString() {
result = IterableUtils.toString(iterableA, input -> Integer.toString(input * 2));
assertEquals("[2, 4, 4, 6, 6, 6, 8, 8, 8, 8]", result);

result = IterableUtils.toString(new ArrayList<Integer>(), input -> {
result = IterableUtils.toString(new ArrayList<>(), input -> {
fail("not supposed to reach here");
return "";
});
Expand Down Expand Up @@ -480,10 +480,10 @@ public void testToStringDelimiter() {
result = IterableUtils.toString(iterableA, transformer, ",,", "((", "))");
assertEquals("((2,,4,,4,,6,,6,,6,,8,,8,,8,,8))", result);

result = IterableUtils.toString(new ArrayList<Integer>(), transformer, "", "(", ")");
result = IterableUtils.toString(new ArrayList<>(), transformer, "", "(", ")");
assertEquals("()", result);

result = IterableUtils.toString(new ArrayList<Integer>(), transformer, "", "", "");
result = IterableUtils.toString(new ArrayList<>(), transformer, "", "", "");
assertEquals("", result);
}

Expand All @@ -495,20 +495,20 @@ public void testToStringWithNullArguments() {
}, "", "(", ")");
assertEquals("()", result);
assertAll(
() -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<Integer>(), null, "", "(", ")"),
() -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), null, "", "(", ")"),
"expecting NullPointerException"),
() -> assertThrows(NullPointerException.class, () ->
IterableUtils.toString(new ArrayList<Integer>(), input -> {
IterableUtils.toString(new ArrayList<>(), input -> {
fail("not supposed to reach here");
return "";
}, null, "(", ")"),
"expecting NullPointerException"),
() -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<Integer>(), input -> {
() -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), input -> {
fail("not supposed to reach here");
return "";
}, "", null, ")"),
"expecting NullPointerException"),
() -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<Integer>(), input -> {
() -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), input -> {
fail("not supposed to reach here");
return "";
}, "", "(", null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void testIntersectNonEmptyWithEmptyList() {

@Test
public void testLazyFactoryList() {
final List<Integer> list = ListUtils.lazyList(new ArrayList<Integer>(), new Factory<Integer>() {
final List<Integer> list = ListUtils.lazyList(new ArrayList<>(), new Factory<Integer>() {

private int index;

Expand Down Expand Up @@ -406,7 +406,7 @@ public void testSelect() {
// Ensure that the collection is the input type or a super type
final List<Integer> output1 = ListUtils.select(list, EQUALS_TWO);
final List<Number> output2 = ListUtils.<Number>select(list, EQUALS_TWO);
final HashSet<Number> output3 = CollectionUtils.select(list, EQUALS_TWO, new HashSet<Number>());
final HashSet<Number> output3 = CollectionUtils.select(list, EQUALS_TWO, new HashSet<>());
assertTrue(CollectionUtils.isEqualCollection(output1, output3));
assertEquals(4, list.size());
assertEquals(1, output1.size());
Expand All @@ -423,7 +423,7 @@ public void testSelectRejected() {
list.add(4L);
final List<Long> output1 = ListUtils.selectRejected(list, EQUALS_TWO);
final List<? extends Number> output2 = ListUtils.selectRejected(list, EQUALS_TWO);
final HashSet<Number> output3 = CollectionUtils.selectRejected(list, EQUALS_TWO, new HashSet<Number>());
final HashSet<Number> output3 = CollectionUtils.selectRejected(list, EQUALS_TWO, new HashSet<>());
assertTrue(CollectionUtils.isEqualCollection(output1, output2));
assertTrue(CollectionUtils.isEqualCollection(output1, output3));
assertEquals(4, list.size());
Expand Down
25 changes: 12 additions & 13 deletions src/test/java/org/apache/commons/collections4/MapUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.text.NumberFormat;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -167,11 +166,11 @@ public void testPutAll_Map_array() {
() -> assertThrows(NullPointerException.class, () -> MapUtils.putAll(null, new Object[0]))
);

Map<String, String> test = MapUtils.putAll(new HashMap<String, String>(), org.apache.commons.lang3.ArrayUtils.EMPTY_STRING_ARRAY);
Map<String, String> test = MapUtils.putAll(new HashMap<>(), org.apache.commons.lang3.ArrayUtils.EMPTY_STRING_ARRAY);
assertEquals(0, test.size());

// sub array
test = MapUtils.putAll(new HashMap<String, String>(), new String[][] {
test = MapUtils.putAll(new HashMap<>(), new String[][] {
{"RED", "#FF0000"},
{"GREEN", "#00FF00"},
{"BLUE", "#0000FF"}
Expand All @@ -184,25 +183,25 @@ public void testPutAll_Map_array() {
assertEquals("#0000FF", test.get("BLUE"));
assertEquals(3, test.size());
assertAll(
() -> assertThrows(IllegalArgumentException.class, () -> MapUtils.putAll(new HashMap<String, String>(), new String[][]{
() -> assertThrows(IllegalArgumentException.class, () -> MapUtils.putAll(new HashMap<>(), new String[][]{
{"RED", "#FF0000"},
null,
{"BLUE", "#0000FF"}
})),
() -> assertThrows(IllegalArgumentException.class, () -> MapUtils.putAll(new HashMap<String, String>(), new String[][]{
() -> assertThrows(IllegalArgumentException.class, () -> MapUtils.putAll(new HashMap<>(), new String[][]{
{"RED", "#FF0000"},
{"GREEN"},
{"BLUE", "#0000FF"}
})),
() -> assertThrows(IllegalArgumentException.class, () -> MapUtils.putAll(new HashMap<String, String>(), new String[][]{
() -> assertThrows(IllegalArgumentException.class, () -> MapUtils.putAll(new HashMap<>(), new String[][]{
{"RED", "#FF0000"},
{},
{"BLUE", "#0000FF"}
}))
);

// flat array
test = MapUtils.putAll(new HashMap<String, String>(), new String[] {
test = MapUtils.putAll(new HashMap<>(), new String[] {
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF"
Expand All @@ -215,7 +214,7 @@ public void testPutAll_Map_array() {
assertEquals("#0000FF", test.get("BLUE"));
assertEquals(3, test.size());

test = MapUtils.putAll(new HashMap<String, String>(), new String[] {
test = MapUtils.putAll(new HashMap<>(), new String[] {
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF",
Expand All @@ -229,11 +228,11 @@ public void testPutAll_Map_array() {
assertEquals("#0000FF", test.get("BLUE"));
assertEquals(3, test.size());

test = MapUtils.putAll(new HashMap<String, String>(), null);
test = MapUtils.putAll(new HashMap<>(), null);
assertEquals(0, test.size());

// map entry
test = MapUtils.putAll(new HashMap<String, String>(), new Object[] {
test = MapUtils.putAll(new HashMap<>(), new Object[] {
new DefaultMapEntry<>("RED", "#FF0000"),
new DefaultMapEntry<>("GREEN", "#00FF00"),
new DefaultMapEntry<>("BLUE", "#0000FF")
Expand All @@ -247,7 +246,7 @@ public void testPutAll_Map_array() {
assertEquals(3, test.size());

// key value
test = MapUtils.putAll(new HashMap<String, String>(), new Object[] {
test = MapUtils.putAll(new HashMap<>(), new Object[] {
new DefaultKeyValue<>("RED", "#FF0000"),
new DefaultKeyValue<>("GREEN", "#00FF00"),
new DefaultKeyValue<>("BLUE", "#0000FF")
Expand Down Expand Up @@ -826,7 +825,7 @@ public void testPopulateMultiMap() {
list.add(new X(5, "x5"));

// Now test key transform population
final MultiValueMap<Integer, X> map = MultiValueMap.multiValueMap(new TreeMap<Integer, Collection<X>>());
final MultiValueMap<Integer, X> map = MultiValueMap.multiValueMap(new TreeMap<>());
MapUtils.populateMap(map, list, (Transformer<X, Integer>) input -> input.key, TransformerUtils.<X>nopTransformer());
assertEquals(list.size(), map.totalSize());

Expand Down Expand Up @@ -991,7 +990,7 @@ public void testFixedSizeMap() {

@Test
public void testFixedSizeSortedMap() {
final Exception exception = assertThrows(IllegalArgumentException.class, () -> MapUtils.fixedSizeSortedMap(new TreeMap<Long, Long>()).put(1L, 1L));
final Exception exception = assertThrows(IllegalArgumentException.class, () -> MapUtils.fixedSizeSortedMap(new TreeMap<>()).put(1L, 1L));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public void testSwitchTransformer() {
assertEquals("C", TransformerUtils.switchTransformer(map).transform("WELL"));

assertEquals(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(new Predicate[0], new Transformer[0]));
assertEquals(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(new HashMap<Predicate<Object>, Transformer<Object, Object>>()));
assertEquals(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(new HashMap<>()));
map = new HashMap<>();
map.put(null, null);
assertEquals(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchTransformer(map));
Expand Down Expand Up @@ -263,7 +263,7 @@ public void testSwitchMapTransformer() {
map.put(null, c);
assertEquals("C", TransformerUtils.switchMapTransformer(map).transform("WELL"));

assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(new HashMap<Object, Transformer<Object, Object>>()));
assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(new HashMap<>()));
map = new HashMap<>();
map.put(null, null);
assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(map));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public CollectionBagTest() {

@Override
public Bag<T> makeObject() {
return CollectionBag.collectionBag(new HashBag<T>());
return CollectionBag.collectionBag(new HashBag<>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public boolean isNullSupported() {

@Override
public Bag<T> makeObject() {
return CollectionSortedBag.collectionSortedBag(new TreeBag<T>());
return CollectionSortedBag.collectionSortedBag(new TreeBag<>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected Bag<T> decorateBag(final HashBag<T> bag, final Predicate<T> predicate)

@Override
public Bag<T> makeObject() {
return decorateBag(new HashBag<T>(), truePredicate);
return decorateBag(new HashBag<>(), truePredicate);
}

protected Bag<T> makeTestBag() {
return decorateBag(new HashBag<T>(), stringPredicate());
return decorateBag(new HashBag<>(), stringPredicate());
}

@Override
Expand Down Expand Up @@ -104,7 +104,7 @@ public void testIllegalDecorate() {

assertThrows(IllegalArgumentException.class, () -> decorateBag((HashBag<T>) elements, stringPredicate()));

assertThrows(NullPointerException.class, () -> decorateBag(new HashBag<T>(), null));
assertThrows(NullPointerException.class, () -> decorateBag(new HashBag<>(), null));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,27 @@ protected SortedBag<T> decorateBag(final SortedBag<T> bag, final Predicate<T> pr

@Override
public SortedBag<T> makeObject() {
return decorateBag(new TreeBag<T>(), truePredicate);
return decorateBag(new TreeBag<>(), truePredicate);
}

protected SortedBag<T> makeTestBag() {
return decorateBag(new TreeBag<T>(), stringPredicate());
return decorateBag(new TreeBag<>(), stringPredicate());
}

@Test
public void testDecorate() {
final SortedBag<T> bag = decorateBag(new TreeBag<T>(), stringPredicate());
final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
((PredicatedSortedBag<T>) bag).decorated();

assertThrows(NullPointerException.class, () -> decorateBag(new TreeBag<T>(), null));
assertThrows(NullPointerException.class, () -> decorateBag(new TreeBag<>(), null));

assertThrows(NullPointerException.class, () -> decorateBag(nullBag, stringPredicate()));
}

@Test
@SuppressWarnings("unchecked")
public void testSortOrder() {
final SortedBag<T> bag = decorateBag(new TreeBag<T>(), stringPredicate());
final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
final String one = "one";
final String two = "two";
final String three = "three";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public SynchronizedBagTest() {

@Override
public Bag<T> makeObject() {
return SynchronizedBag.synchronizedBag(new HashBag<T>());
return SynchronizedBag.synchronizedBag(new HashBag<>());
}

@Override
Expand Down
Loading

0 comments on commit af6fff1

Please sign in to comment.