More lambdas, less boilerplate.

This commit is contained in:
Gary Gregory 2019-09-14 15:25:22 -04:00
parent b29bae05ae
commit a2317f6299
34 changed files with 122 additions and 517 deletions

View File

@ -604,12 +604,7 @@ public class CollectionUtils {
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
final Transformer<E, ?> transformer = new Transformer() { final Transformer<E, ?> transformer = input -> new EquatorWrapper(equator, input);
@Override
public EquatorWrapper<?> transform(final Object input) {
return new EquatorWrapper(equator, input);
}
};
return isEqualCollection(collect(a, transformer), collect(b, transformer)); return isEqualCollection(collect(a, transformer), collect(b, transformer));
} }
@ -1782,12 +1777,7 @@ public class CollectionUtils {
final Iterable<? extends E> retain, final Iterable<? extends E> retain,
final Equator<? super E> equator) { final Equator<? super E> equator) {
final Transformer<E, EquatorWrapper<E>> transformer = new Transformer<E, EquatorWrapper<E>>() { final Transformer<E, EquatorWrapper<E>> transformer = input -> new EquatorWrapper<>(equator, input);
@Override
public EquatorWrapper<E> transform(final E input) {
return new EquatorWrapper<>(equator, input);
}
};
final Set<EquatorWrapper<E>> retainSet = final Set<EquatorWrapper<E>> retainSet =
collect(retain, transformer, new HashSet<EquatorWrapper<E>>()); collect(retain, transformer, new HashSet<EquatorWrapper<E>>());
@ -1860,12 +1850,7 @@ public class CollectionUtils {
final Iterable<? extends E> remove, final Iterable<? extends E> remove,
final Equator<? super E> equator) { final Equator<? super E> equator) {
final Transformer<E, EquatorWrapper<E>> transformer = new Transformer<E, EquatorWrapper<E>>() { final Transformer<E, EquatorWrapper<E>> transformer = input -> new EquatorWrapper<>(equator, input);
@Override
public EquatorWrapper<E> transform(final E input) {
return new EquatorWrapper<>(equator, input);
}
};
final Set<EquatorWrapper<E>> removeSet = final Set<EquatorWrapper<E>> removeSet =
collect(remove, transformer, new HashSet<EquatorWrapper<E>>()); collect(remove, transformer, new HashSet<EquatorWrapper<E>>());

View File

@ -125,12 +125,7 @@ public class SetUtils {
throw new NullPointerException("Sets must not be null."); throw new NullPointerException("Sets must not be null.");
} }
final Predicate<E> notContainedInB = new Predicate<E>() { final Predicate<E> notContainedInB = object -> !b.contains(object);
@Override
public boolean evaluate(final E object) {
return !b.contains(object);
}
};
return new SetView<E>() { return new SetView<E>() {
@Override @Override
@ -285,12 +280,7 @@ public class SetUtils {
throw new NullPointerException("Sets must not be null."); throw new NullPointerException("Sets must not be null.");
} }
final Predicate<E> containedInB = new Predicate<E>() { final Predicate<E> containedInB = object -> b.contains(object);
@Override
public boolean evaluate(final E object) {
return b.contains(object);
}
};
return new SetView<E>() { return new SetView<E>() {
@Override @Override

View File

@ -438,23 +438,18 @@ public class MultiValueMap<K, V> extends AbstractMapDecorator<K, Object> impleme
return null; return null;
} }
final K key = keyIterator.next(); final K key = keyIterator.next();
final Transformer<V, Entry<K, V>> transformer = new Transformer<V, Entry<K, V>>() { final Transformer<V, Entry<K, V>> transformer = input -> new Entry<K, V>() {
@Override @Override
public Entry<K, V> transform(final V input) { public K getKey() {
return new Entry<K, V>() { return key;
@Override }
public K getKey() { @Override
return key; public V getValue() {
} return input;
@Override }
public V getValue() { @Override
return input; public V setValue(final V value) {
} throw new UnsupportedOperationException();
@Override
public V setValue(final V value) {
throw new UnsupportedOperationException();
}
};
} }
}; };
return new TransformIterator<>(new ValuesIterator(key), transformer); return new TransformIterator<>(new ValuesIterator(key), transformer);

View File

@ -626,14 +626,7 @@ public abstract class AbstractMultiValuedMap<K, V> implements MultiValuedMap<K,
return null; return null;
} }
final K key = keyIterator.next(); final K key = keyIterator.next();
final Transformer<V, Entry<K, V>> entryTransformer = new Transformer<V, Entry<K, V>>() { final Transformer<V, Entry<K, V>> entryTransformer = input -> new MultiValuedMapEntry(key, input);
@Override
public Entry<K, V> transform(final V input) {
return new MultiValuedMapEntry(key, input);
}
};
return new TransformIterator<>(new ValuesIterator(key), entryTransformer); return new TransformIterator<>(new ValuesIterator(key), entryTransformer);
} }
}; };

View File

@ -256,12 +256,7 @@ public abstract class AbstractMultiSet<E> extends AbstractCollection<E> implemen
* @return the uniqueSet iterator * @return the uniqueSet iterator
*/ */
protected Iterator<E> createUniqueSetIterator() { protected Iterator<E> createUniqueSetIterator() {
final Transformer<Entry<E>, E> transformer = new Transformer<Entry<E>, E>() { final Transformer<Entry<E>, E> transformer = entry -> entry.getElement();
@Override
public E transform(final Entry<E> entry) {
return entry.getElement();
}
};
return IteratorUtils.transformedIterator(entrySet().iterator(), transformer); return IteratorUtils.transformedIterator(entrySet().iterator(), transformer);
} }

View File

@ -476,12 +476,7 @@ public class CollectionUtilsTest extends MockTestCase {
@Test @Test
public void testSubtractWithPredicate() { public void testSubtractWithPredicate() {
// greater than 3 // greater than 3
final Predicate<Number> predicate = new Predicate<Number>() { final Predicate<Number> predicate = n -> n.longValue() > 3L;
@Override
public boolean evaluate(final Number n) {
return n.longValue() > 3L;
}
};
final Collection<Number> col = CollectionUtils.subtract(iterableA, collectionC, predicate); final Collection<Number> col = CollectionUtils.subtract(iterableA, collectionC, predicate);
final Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col); final Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col);
@ -733,22 +728,12 @@ public class CollectionUtilsTest extends MockTestCase {
final Collection<String> strings = Arrays.asList("a", "b", "c"); final Collection<String> strings = Arrays.asList("a", "b", "c");
final StringBuffer result = new StringBuffer(); final StringBuffer result = new StringBuffer();
result.append(CollectionUtils.forAllButLastDo(strings, new Closure<String>() { result.append(CollectionUtils.forAllButLastDo(strings, (Closure<String>) input -> result.append(input+";")));
@Override
public void execute(final String input) {
result.append(input+";");
}
}));
assertEquals("a;b;c", result.toString()); assertEquals("a;b;c", result.toString());
final Collection<String> oneString = Arrays.asList("a"); final Collection<String> oneString = Arrays.asList("a");
final StringBuffer resultOne = new StringBuffer(); final StringBuffer resultOne = new StringBuffer();
resultOne.append(CollectionUtils.forAllButLastDo(oneString, new Closure<String>() { resultOne.append(CollectionUtils.forAllButLastDo(oneString, (Closure<String>) input -> resultOne.append(input+";")));
@Override
public void execute(final String input) {
resultOne.append(input+";");
}
}));
assertEquals("a", resultOne.toString()); assertEquals("a", resultOne.toString());
assertNull(CollectionUtils.forAllButLastDo(strings, (Closure<String>) null)); // do not remove cast assertNull(CollectionUtils.forAllButLastDo(strings, (Closure<String>) null)); // do not remove cast
assertNull(CollectionUtils.forAllButLastDo((Collection<String>) null, (Closure<String>) null)); // do not remove cast assertNull(CollectionUtils.forAllButLastDo((Collection<String>) null, (Closure<String>) null)); // do not remove cast
@ -1108,12 +1093,7 @@ public class CollectionUtilsTest extends MockTestCase {
} }
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
private static Predicate<Number> EQUALS_TWO = new Predicate<Number>() { private static Predicate<Number> EQUALS_TWO = input -> input.intValue() == 2;
@Override
public boolean evaluate(final Number input) {
return input.intValue() == 2;
}
};
//Up to here //Up to here
@Test @Test
@ -1296,12 +1276,7 @@ public class CollectionUtilsTest extends MockTestCase {
assertTrue(collection.contains(2L) && !collection.contains(1)); assertTrue(collection.contains(2L) && !collection.contains(1));
} }
Transformer<Object, Integer> TRANSFORM_TO_INTEGER = new Transformer<Object, Integer>() { Transformer<Object, Integer> TRANSFORM_TO_INTEGER = input -> Integer.valueOf(((Long)input).intValue());
@Override
public Integer transform(final Object input) {
return Integer.valueOf(((Long)input).intValue());
}
};
@Test @Test
public void transform1() { public void transform1() {
@ -1333,12 +1308,7 @@ public class CollectionUtilsTest extends MockTestCase {
set.add(1L); set.add(1L);
set.add(2L); set.add(2L);
set.add(3L); set.add(3L);
CollectionUtils.transform(set, new Transformer<Object, Integer>() { CollectionUtils.transform(set, input -> 4);
@Override
public Integer transform(final Object input) {
return 4;
}
});
assertEquals(1, set.size()); assertEquals(1, set.size());
assertEquals(4, set.iterator().next()); assertEquals(4, set.iterator().next());
} }
@ -1831,20 +1801,10 @@ public class CollectionUtilsTest extends MockTestCase {
assertFalse(CollectionUtils.matchesAll(null, null)); assertFalse(CollectionUtils.matchesAll(null, null));
assertFalse(CollectionUtils.matchesAll(collectionA, null)); assertFalse(CollectionUtils.matchesAll(collectionA, null));
final Predicate<Integer> lessThanFive = new Predicate<Integer>() { final Predicate<Integer> lessThanFive = object -> object < 5;
@Override
public boolean evaluate(final Integer object) {
return object < 5;
}
};
assertTrue(CollectionUtils.matchesAll(collectionA, lessThanFive)); assertTrue(CollectionUtils.matchesAll(collectionA, lessThanFive));
final Predicate<Integer> lessThanFour = new Predicate<Integer>() { final Predicate<Integer> lessThanFour = object -> object < 4;
@Override
public boolean evaluate(final Integer object) {
return object < 4;
}
};
assertFalse(CollectionUtils.matchesAll(collectionA, lessThanFour)); assertFalse(CollectionUtils.matchesAll(collectionA, lessThanFour));
assertTrue(CollectionUtils.matchesAll(null, lessThanFour)); assertTrue(CollectionUtils.matchesAll(null, lessThanFour));

View File

@ -103,12 +103,7 @@ public class FluentIterableTest {
emptyIterable = Collections.emptyList(); emptyIterable = Collections.emptyList();
} }
private static Predicate<Number> EVEN = new Predicate<Number>() { private static Predicate<Number> EVEN = input -> input.intValue() % 2 == 0;
@Override
public boolean evaluate(final Number input) {
return input.intValue() % 2 == 0;
}
};
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@Test @Test
@ -198,12 +193,7 @@ public class FluentIterableTest {
@Test @Test
public void filter() { public void filter() {
final Predicate<Integer> smallerThan3 = new Predicate<Integer>() { final Predicate<Integer> smallerThan3 = object -> object.intValue() < 3;
@Override
public boolean evaluate(final Integer object) {
return object.intValue() < 3;
}
};
List<Integer> result = FluentIterable.of(iterableA).filter(smallerThan3).toList(); List<Integer> result = FluentIterable.of(iterableA).filter(smallerThan3).toList();
assertEquals(3, result.size()); assertEquals(3, result.size());
assertEquals(Arrays.asList(1, 2, 2), result); assertEquals(Arrays.asList(1, 2, 2), result);
@ -223,12 +213,7 @@ public class FluentIterableTest {
@Test @Test
public void forEach() { public void forEach() {
final AtomicInteger sum = new AtomicInteger(0); final AtomicInteger sum = new AtomicInteger(0);
final Closure<Integer> closure = new Closure<Integer>() { final Closure<Integer> closure = input -> sum.addAndGet(input);
@Override
public void execute(final Integer input) {
sum.addAndGet(input);
}
};
FluentIterable.of(iterableA).forEach(closure); FluentIterable.of(iterableA).forEach(closure);
int expectedSum = 0; int expectedSum = 0;
@ -315,12 +300,7 @@ public class FluentIterableTest {
@Test @Test
public void transform() { public void transform() {
final Transformer<Integer, Integer> squared = new Transformer<Integer, Integer>() { final Transformer<Integer, Integer> squared = object -> object * object;
@Override
public Integer transform(final Integer object) {
return object * object;
}
};
List<Integer> result = FluentIterable.of(iterableA).transform(squared).toList(); List<Integer> result = FluentIterable.of(iterableA).transform(squared).toList();
assertEquals(10, result.size()); assertEquals(10, result.size());
assertEquals(Arrays.asList(1, 4, 4, 9, 9, 9, 16, 16, 16, 16), result); assertEquals(Arrays.asList(1, 4, 4, 9, 9, 9, 16, 16, 16, 16), result);

View File

@ -91,19 +91,9 @@ public class IterableUtilsTest {
emptyIterable = Collections.emptyList(); emptyIterable = Collections.emptyList();
} }
private static Predicate<Number> EQUALS_TWO = new Predicate<Number>() { private static Predicate<Number> EQUALS_TWO = input -> input.intValue() == 2;
@Override
public boolean evaluate(final Number input) {
return input.intValue() == 2;
}
};
private static Predicate<Number> EVEN = new Predicate<Number>() { private static Predicate<Number> EVEN = input -> input.intValue() % 2 == 0;
@Override
public boolean evaluate(final Number input) {
return input.intValue() % 2 == 0;
}
};
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@Test @Test
@ -373,20 +363,10 @@ public class IterableUtilsTest {
// expected // expected
} }
final Predicate<Integer> lessThanFive = new Predicate<Integer>() { final Predicate<Integer> lessThanFive = object -> object < 5;
@Override
public boolean evaluate(final Integer object) {
return object < 5;
}
};
assertTrue(IterableUtils.matchesAll(iterableA, lessThanFive)); assertTrue(IterableUtils.matchesAll(iterableA, lessThanFive));
final Predicate<Integer> lessThanFour = new Predicate<Integer>() { final Predicate<Integer> lessThanFour = object -> object < 4;
@Override
public boolean evaluate(final Integer object) {
return object < 4;
}
};
assertFalse(IterableUtils.matchesAll(iterableA, lessThanFour)); assertFalse(IterableUtils.matchesAll(iterableA, lessThanFour));
assertTrue(IterableUtils.matchesAll(null, lessThanFour)); assertTrue(IterableUtils.matchesAll(null, lessThanFour));
@ -505,29 +485,18 @@ public class IterableUtilsTest {
result = IterableUtils.toString(null); result = IterableUtils.toString(null);
assertEquals("[]", result); assertEquals("[]", result);
result = IterableUtils.toString(iterableA, new Transformer<Integer, String>() { result = IterableUtils.toString(iterableA, input -> new Integer(input * 2).toString());
@Override
public String transform(final Integer input) {
return new Integer(input * 2).toString();
}
});
assertEquals("[2, 4, 4, 6, 6, 6, 8, 8, 8, 8]", result); assertEquals("[2, 4, 4, 6, 6, 6, 8, 8, 8, 8]", result);
result = IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() { result = IterableUtils.toString(new ArrayList<Integer>(), input -> {
@Override fail("not supposed to reach here");
public String transform(final Integer input) { return "";
fail("not supposed to reach here");
return "";
}
}); });
assertEquals("[]", result); assertEquals("[]", result);
result = IterableUtils.toString(null, new Transformer<Integer, String>() { result = IterableUtils.toString(null, input -> {
@Override fail("not supposed to reach here");
public String transform(final Integer input) { return "";
fail("not supposed to reach here");
return "";
}
}); });
assertEquals("[]", result); assertEquals("[]", result);
} }
@ -535,12 +504,7 @@ public class IterableUtilsTest {
@Test @Test
public void testToStringDelimiter() { public void testToStringDelimiter() {
final Transformer<Integer, String> transformer = new Transformer<Integer, String>() { final Transformer<Integer, String> transformer = input -> new Integer(input * 2).toString();
@Override
public String transform(final Integer input) {
return new Integer(input * 2).toString();
}
};
String result = IterableUtils.toString(iterableA, transformer, "", "", ""); String result = IterableUtils.toString(iterableA, transformer, "", "", "");
assertEquals("2446668888", result); assertEquals("2446668888", result);
@ -572,12 +536,9 @@ public class IterableUtilsTest {
@Test @Test
public void testToStringWithNullArguments() { public void testToStringWithNullArguments() {
final String result = IterableUtils.toString(null, new Transformer<Integer, String>() { final String result = IterableUtils.toString(null, input -> {
@Override fail("not supposed to reach here");
public String transform(final Integer input) { return "";
fail("not supposed to reach here");
return "";
}
}, "", "(", ")"); }, "", "(", ")");
assertEquals("()", result); assertEquals("()", result);
@ -589,12 +550,9 @@ public class IterableUtilsTest {
} }
try { try {
IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() { IterableUtils.toString(new ArrayList<Integer>(), input -> {
@Override fail("not supposed to reach here");
public String transform(final Integer input) { return "";
fail("not supposed to reach here");
return "";
}
}, null, "(", ")"); }, null, "(", ")");
fail("expecting NullPointerException"); fail("expecting NullPointerException");
} catch (final NullPointerException ex) { } catch (final NullPointerException ex) {
@ -602,12 +560,9 @@ public class IterableUtilsTest {
} }
try { try {
IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() { IterableUtils.toString(new ArrayList<Integer>(), input -> {
@Override fail("not supposed to reach here");
public String transform(final Integer input) { return "";
fail("not supposed to reach here");
return "";
}
}, "", null, ")"); }, "", null, ")");
fail("expecting NullPointerException"); fail("expecting NullPointerException");
} catch (final NullPointerException ex) { } catch (final NullPointerException ex) {
@ -615,12 +570,9 @@ public class IterableUtilsTest {
} }
try { try {
IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() { IterableUtils.toString(new ArrayList<Integer>(), input -> {
@Override fail("not supposed to reach here");
public String transform(final Integer input) { return "";
fail("not supposed to reach here");
return "";
}
}, "", "(", null); }, "", "(", null);
fail("expecting NullPointerException"); fail("expecting NullPointerException");
} catch (final NullPointerException ex) { } catch (final NullPointerException ex) {

View File

@ -122,12 +122,7 @@ public class ListUtilsTest {
@Test @Test
public void testPredicatedList() { public void testPredicatedList() {
final Predicate<Object> predicate = new Predicate<Object>() { final Predicate<Object> predicate = o -> o instanceof String;
@Override
public boolean evaluate(final Object o) {
return o instanceof String;
}
};
final List<Object> list = ListUtils.predicatedList(new ArrayList<>(), predicate); final List<Object> list = ListUtils.predicatedList(new ArrayList<>(), predicate);
assertTrue("returned object should be a PredicatedList", list instanceof PredicatedList); assertTrue("returned object should be a PredicatedList", list instanceof PredicatedList);
try { try {
@ -452,12 +447,7 @@ public class ListUtilsTest {
assertEquals(strings, partitionMax.get(0)); assertEquals(strings, partitionMax.get(0));
} }
private static Predicate<Number> EQUALS_TWO = new Predicate<Number>() { private static Predicate<Number> EQUALS_TWO = input -> input.intValue() == 2;
@Override
public boolean evaluate(final Number input) {
return input.intValue() == 2;
}
};
@Test @Test
@SuppressWarnings("boxing") // OK in test code @SuppressWarnings("boxing") // OK in test code

View File

@ -65,12 +65,7 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
} }
public Predicate<Object> getPredicate() { public Predicate<Object> getPredicate() {
return new Predicate<Object>() { return o -> o instanceof String;
@Override
public boolean evaluate(final Object o) {
return o instanceof String;
}
};
} }
@Test @Test
@ -122,14 +117,11 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
@Test @Test
public void testLazyMapTransformer() { public void testLazyMapTransformer() {
final Map<Object, Object> map = MapUtils.lazyMap(new HashMap<>(), new Transformer<Object, Object>() { final Map<Object, Object> map = MapUtils.lazyMap(new HashMap<>(), (Transformer<Object, Object>) mapKey -> {
@Override if (mapKey instanceof String) {
public Object transform(final Object mapKey) { return Integer.valueOf((String) mapKey);
if (mapKey instanceof String) {
return Integer.valueOf((String) mapKey);
}
return null;
} }
return null;
}); });
assertEquals(0, map.size()); assertEquals(0, map.size());
@ -859,12 +851,7 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
// Now test key transform population // 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<Integer, Collection<X>>());
MapUtils.populateMap(map, list, new Transformer<X, Integer>() { MapUtils.populateMap(map, list, (Transformer<X, Integer>) input -> input.key, TransformerUtils.<X> nopTransformer());
@Override
public Integer transform(final X input) {
return input.key;
}
}, TransformerUtils.<X> nopTransformer());
assertEquals(list.size(), map.totalSize()); assertEquals(list.size(), map.totalSize());
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {

View File

@ -92,12 +92,7 @@ public class MultiSetUtilsTest {
*/ */
@Test @Test
public void testPredicatedMultiSet() { public void testPredicatedMultiSet() {
final Predicate<String> predicate = new Predicate<String>() { final Predicate<String> predicate = object -> object.length() == 1;
@Override
public boolean evaluate(final String object) {
return object.length() == 1;
};
};
final MultiSet<String> predicated = MultiSetUtils.predicatedMultiSet(multiSet, predicate); final MultiSet<String> predicated = MultiSetUtils.predicatedMultiSet(multiSet, predicate);
assertEquals(multiSet.size(), predicated.size()); assertEquals(multiSet.size(), predicated.size());
assertEquals(multiSet.getCount("a"), predicated.getCount("a")); assertEquals(multiSet.getCount("a"), predicated.getCount("a"));
@ -115,12 +110,7 @@ public class MultiSetUtilsTest {
} }
try { try {
MultiSetUtils.predicatedMultiSet(multiSet, new Predicate<String>() { MultiSetUtils.predicatedMultiSet(multiSet, object -> object.equals("a"));
@Override
public boolean evaluate(final String object) {
return object.equals("a");
};
});
fail("Predicate is violated for all elements not being 'a'"); fail("Predicate is violated for all elements not being 'a'");
} }
catch (final IllegalArgumentException iae) { catch (final IllegalArgumentException iae) {

View File

@ -231,12 +231,7 @@ public class SetUtilsTest {
@Test @Test
public void testpredicatedSet() { public void testpredicatedSet() {
final Predicate<Object> predicate = new Predicate<Object>() { final Predicate<Object> predicate = o -> o instanceof String;
@Override
public boolean evaluate(final Object o) {
return o instanceof String;
}
};
final Set<Object> set = SetUtils.predicatedSet(new HashSet<>(), predicate); final Set<Object> set = SetUtils.predicatedSet(new HashSet<>(), predicate);
assertTrue("returned object should be a PredicatedSet", set instanceof PredicatedSet); assertTrue("returned object should be a PredicatedSet", set instanceof PredicatedSet);
try { try {

View File

@ -41,12 +41,7 @@ public class SplitMapUtilsTest {
private Map<String, Integer> backingMap; private Map<String, Integer> backingMap;
private TransformedSplitMap<String, String, String, Integer> transformedMap; private TransformedSplitMap<String, String, String, Integer> transformedMap;
private final Transformer<String, Integer> stringToInt = new Transformer<String, Integer>() { private final Transformer<String, Integer> stringToInt = input -> Integer.valueOf(input);
@Override
public Integer transform(final String input) {
return Integer.valueOf(input);
}
};
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -84,32 +79,17 @@ public class SplitMapUtilsTest {
// check individual operations // check individual operations
int sz = map.size(); int sz = map.size();
attemptPutOperation(new Runnable() { attemptPutOperation(() -> map.clear());
@Override
public void run() {
map.clear();
}
});
assertEquals(sz, map.size()); assertEquals(sz, map.size());
attemptPutOperation(new Runnable() { attemptPutOperation(() -> map.put("foo", 100));
@Override
public void run() {
map.put("foo", 100);
}
});
final HashMap<String, Integer> m = new HashMap<>(); final HashMap<String, Integer> m = new HashMap<>();
m.put("foo", 100); m.put("foo", 100);
m.put("bar", 200); m.put("bar", 200);
m.put("baz", 300); m.put("baz", 300);
attemptPutOperation(new Runnable() { attemptPutOperation(() -> map.putAll(m));
@Override
public void run() {
map.putAll(m);
}
});
// equals, hashcode // equals, hashcode
final IterableMap<String, Integer> other = SplitMapUtils.readableMap(transformedMap); final IterableMap<String, Integer> other = SplitMapUtils.readableMap(transformedMap);
@ -135,60 +115,15 @@ public class SplitMapUtilsTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testWritableMap() { public void testWritableMap() {
final Map<String, String> map = SplitMapUtils.writableMap(transformedMap); final Map<String, String> map = SplitMapUtils.writableMap(transformedMap);
attemptGetOperation(new Runnable() { attemptGetOperation(() -> map.get(null));
@Override attemptGetOperation(() -> map.entrySet());
public void run() { attemptGetOperation(() -> map.keySet());
map.get(null); attemptGetOperation(() -> map.values());
} attemptGetOperation(() -> map.size());
}); attemptGetOperation(() -> map.isEmpty());
attemptGetOperation(new Runnable() { attemptGetOperation(() -> map.containsKey(null));
@Override attemptGetOperation(() -> map.containsValue(null));
public void run() { attemptGetOperation(() -> map.remove(null));
map.entrySet();
}
});
attemptGetOperation(new Runnable() {
@Override
public void run() {
map.keySet();
}
});
attemptGetOperation(new Runnable() {
@Override
public void run() {
map.values();
}
});
attemptGetOperation(new Runnable() {
@Override
public void run() {
map.size();
}
});
attemptGetOperation(new Runnable() {
@Override
public void run() {
map.isEmpty();
}
});
attemptGetOperation(new Runnable() {
@Override
public void run() {
map.containsKey(null);
}
});
attemptGetOperation(new Runnable() {
@Override
public void run() {
map.containsValue(null);
}
});
attemptGetOperation(new Runnable() {
@Override
public void run() {
map.remove(null);
}
});
// equals, hashcode // equals, hashcode
final Map<String, String> other = SplitMapUtils.writableMap(transformedMap); final Map<String, String> other = SplitMapUtils.writableMap(transformedMap);

View File

@ -247,12 +247,7 @@ public class TransformerUtilsTest {
assertEquals("A", TransformerUtils.ifTransformer(TruePredicate.truePredicate(), a, b).transform(null)); assertEquals("A", TransformerUtils.ifTransformer(TruePredicate.truePredicate(), a, b).transform(null));
assertEquals("B", TransformerUtils.ifTransformer(FalsePredicate.falsePredicate(), a, b).transform(null)); assertEquals("B", TransformerUtils.ifTransformer(FalsePredicate.falsePredicate(), a, b).transform(null));
final Predicate<Integer> lessThanFivePredicate = new Predicate<Integer>() { final Predicate<Integer> lessThanFivePredicate = value -> value < 5;
@Override
public boolean evaluate(final Integer value) {
return value < 5;
}
};
// if/else tests // if/else tests
assertEquals("A", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(1)); assertEquals("A", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(1));
assertEquals("B", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(5)); assertEquals("B", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(5));

View File

@ -44,12 +44,7 @@ public class PredicatedBagTest<T> extends AbstractBagTest<T> {
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
protected Predicate<T> stringPredicate() { protected Predicate<T> stringPredicate() {
return new Predicate<T>() { return o -> o instanceof String;
@Override
public boolean evaluate(final T o) {
return o instanceof String;
}
};
} }
protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate(); protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();

View File

@ -46,12 +46,7 @@ public class PredicatedSortedBagTest<T> extends AbstractSortedBagTest<T> {
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
protected Predicate<T> stringPredicate() { protected Predicate<T> stringPredicate() {
return new Predicate<T>() { return o -> o instanceof String;
@Override
public boolean evaluate(final T o) {
return o instanceof String;
}
};
} }
protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate(); protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();

View File

@ -16,8 +16,6 @@
*/ */
package org.apache.commons.collections4.bag; package org.apache.commons.collections4.bag;
import java.util.Comparator;
import junit.framework.Test; import junit.framework.Test;
import org.apache.commons.collections4.Bag; import org.apache.commons.collections4.Bag;
@ -75,12 +73,7 @@ public class TreeBagTest<T> extends AbstractSortedBagTest<T> {
// expected; // expected;
} }
final Bag<String> bag2 = new TreeBag<>(new Comparator<String>() { final Bag<String> bag2 = new TreeBag<>((o1, o2) -> o1.compareTo(o2));
@Override
public int compare(final String o1, final String o2) {
return o1.compareTo(o2);
}
});
try { try {
// jdk bug: adding null to an empty TreeMap works // jdk bug: adding null to an empty TreeMap works
// thus ensure that the bag is not empty before adding null // thus ensure that the bag is not empty before adding null

View File

@ -76,12 +76,7 @@ public class PredicatedCollectionTest<E> extends AbstractCollectionTest<E> {
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
protected Predicate<E> testPredicate = protected Predicate<E> testPredicate =
new Predicate<E>() { o -> o instanceof String;
@Override
public boolean evaluate(final E o) {
return o instanceof String;
}
};
public Collection<E> makeTestCollection() { public Collection<E> makeTestCollection() {
return decorateCollection(new ArrayList<E>(), testPredicate); return decorateCollection(new ArrayList<E>(), testPredicate);

View File

@ -103,18 +103,15 @@ public class ComparatorChainTest extends AbstractComparatorTest<ComparatorChainT
// -1 * Integer.MIN_VALUE is less than 0, // -1 * Integer.MIN_VALUE is less than 0,
// test that ComparatorChain handles this edge case correctly // test that ComparatorChain handles this edge case correctly
final ComparatorChain<Integer> chain = new ComparatorChain<>(); final ComparatorChain<Integer> chain = new ComparatorChain<>();
chain.addComparator(new Comparator<Integer>() { chain.addComparator((a, b) -> {
@Override final int result = a.compareTo(b);
public int compare(final Integer a, final Integer b) { if (result < 0) {
final int result = a.compareTo(b); return Integer.MIN_VALUE;
if (result < 0) {
return Integer.MIN_VALUE;
}
if (result > 0) {
return Integer.MAX_VALUE;
}
return 0;
} }
if (result > 0) {
return Integer.MAX_VALUE;
}
return 0;
}, true); }, true);
assertTrue(chain.compare(Integer.valueOf(4), Integer.valueOf(5)) > 0); assertTrue(chain.compare(Integer.valueOf(4), Integer.valueOf(5)) > 0);

View File

@ -157,16 +157,13 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
} }
private void verifyElementsInPredicate(final String[] elements) { private void verifyElementsInPredicate(final String[] elements) {
final Predicate<E> pred = new Predicate<E>() { final Predicate<E> pred = x -> {
@Override for (final String element : elements) {
public boolean evaluate(final E x) { if (element.equals(x)) {
for (final String element : elements) { return true;
if (element.equals(x)) {
return true;
}
} }
return false;
} }
return false;
}; };
initIterator(); initIterator();
iterator.setPredicate(pred); iterator.setPredicate(pred);
@ -199,10 +196,7 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
* @return "filtered" iterator * @return "filtered" iterator
*/ */
protected FilterIterator<E> makePassThroughFilter(final Iterator<E> i) { protected FilterIterator<E> makePassThroughFilter(final Iterator<E> i) {
final Predicate<E> pred = new Predicate<E>() { final Predicate<E> pred = x -> true;
@Override
public boolean evaluate(final E x) { return true; }
};
return new FilterIterator<>(i, pred); return new FilterIterator<>(i, pred);
} }
@ -214,10 +208,7 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
* @return "filtered" iterator * @return "filtered" iterator
*/ */
protected FilterIterator<E> makeBlockAllFilter(final Iterator<E> i) { protected FilterIterator<E> makeBlockAllFilter(final Iterator<E> i) {
final Predicate<E> pred = new Predicate<E>() { final Predicate<E> pred = x -> false;
@Override
public boolean evaluate(final E x) { return false; }
};
return new FilterIterator<>(i, pred); return new FilterIterator<>(i, pred);
} }
} }

View File

@ -70,47 +70,17 @@ public class FilterListIteratorTest {
if (i % 6 == 0) { sixes.add(Integer.valueOf(i)); } if (i % 6 == 0) { sixes.add(Integer.valueOf(i)); }
} }
truePred = new Predicate<Integer>() { truePred = x -> true;
@Override
public boolean evaluate(final Integer x) {
return true;
}
};
falsePred = new Predicate<Integer>() { falsePred = x -> true;
@Override
public boolean evaluate(final Integer x) {
return true;
}
};
evenPred = new Predicate<Integer>() { evenPred = x -> x % 2 == 0;
@Override
public boolean evaluate(final Integer x) {
return x % 2 == 0;
}
};
oddPred = new Predicate<Integer>() { oddPred = x -> x % 2 != 0;
@Override
public boolean evaluate(final Integer x) {
return x % 2 != 0; //works for all numbers, not just >= 0 as is the case for "x % 2 == 1"
}
};
threePred = new Predicate<Integer>() { threePred = x -> x % 3 == 0;
@Override
public boolean evaluate(final Integer x) {
return x % 3 == 0;
}
};
fourPred = new Predicate<Integer>() { fourPred = x -> x % 4 == 0;
@Override
public boolean evaluate(final Integer x) {
return x % 4 == 0;
}
};
} }

View File

@ -91,12 +91,7 @@ public class IteratorChainTest extends AbstractIteratorTest<String> {
public void testRemoveFromFilteredIterator() { public void testRemoveFromFilteredIterator() {
final Predicate<Integer> myPredicate = new Predicate<Integer>() { final Predicate<Integer> myPredicate = i -> i.compareTo(Integer.valueOf(4)) < 0;
@Override
public boolean evaluate(final Integer i) {
return i.compareTo(Integer.valueOf(4)) < 0;
}
};
final List<Integer> list1 = new ArrayList<>(); final List<Integer> list1 = new ArrayList<>();
final List<Integer> list2 = new ArrayList<>(); final List<Integer> list2 = new ArrayList<>();

View File

@ -105,12 +105,7 @@ public class LazyIteratorChainTest extends AbstractIteratorTest<String> {
public void testRemoveFromFilteredIterator() { public void testRemoveFromFilteredIterator() {
final Predicate<Integer> myPredicate = new Predicate<Integer>() { final Predicate<Integer> myPredicate = i -> i.compareTo(Integer.valueOf(4)) < 0;
@Override
public boolean evaluate(final Integer i) {
return i.compareTo(Integer.valueOf(4)) < 0;
}
};
final List<Integer> list1 = new ArrayList<>(); final List<Integer> list1 = new ArrayList<>();
final List<Integer> list2 = new ArrayList<>(); final List<Integer> list2 = new ArrayList<>();

View File

@ -56,12 +56,7 @@ public class PredicatedListTest<E> extends AbstractListTest<E> {
//-------------------------------------------------------------------- //--------------------------------------------------------------------
protected Predicate<E> testPredicate = protected Predicate<E> testPredicate =
new Predicate<E>() { o -> o instanceof String;
@Override
public boolean evaluate(final E o) {
return o instanceof String;
}
};
public List<E> makeTestList() { public List<E> makeTestList() {
return decorateList(new ArrayList<E>(), testPredicate); return decorateList(new ArrayList<E>(), testPredicate);

View File

@ -104,14 +104,11 @@ public class DefaultedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testMapGet4() { public void testMapGet4() {
final HashMap<K, V> base = new HashMap<>(); final HashMap<K, V> base = new HashMap<>();
final Map<K, V> map = DefaultedMap.defaultedMap(base, new Transformer<K, V>() { final Map<K, V> map = DefaultedMap.defaultedMap(base, (Transformer<K, V>) input -> {
@Override if (input instanceof String) {
public V transform(final K input) { return (V) "NULL";
if (input instanceof String) {
return (V) "NULL";
}
return (V) "NULL_OBJECT";
} }
return (V) "NULL_OBJECT";
}); });
assertEquals(0, map.size()); assertEquals(0, map.size());

View File

@ -72,12 +72,7 @@ public class LazyMapTest<K, V> extends AbstractIterableMapTest<K, V> {
@Test @Test
public void mapGetWithTransformer() { public void mapGetWithTransformer() {
final Transformer<Number, Integer> intConverter = new Transformer<Number, Integer>(){ final Transformer<Number, Integer> intConverter = input -> input.intValue();
@Override
public Integer transform(final Number input) {
return input.intValue();
}
};
final Map<Long, Number> map = lazyMap(new HashMap<Long,Number>(), intConverter ); final Map<Long, Number> map = lazyMap(new HashMap<Long,Number>(), intConverter );
assertEquals(0, map.size()); assertEquals(0, map.size());
final Number i1 = map.get(123L); final Number i1 = map.get(123L);

View File

@ -34,12 +34,7 @@ public class PredicatedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
protected static final Predicate<Object> truePredicate = TruePredicate.<Object>truePredicate(); protected static final Predicate<Object> truePredicate = TruePredicate.<Object>truePredicate();
protected static final Predicate<Object> testPredicate = new Predicate<Object>() { protected static final Predicate<Object> testPredicate = o -> o instanceof String;
@Override
public boolean evaluate(final Object o) {
return o instanceof String;
}
};
public PredicatedMapTest(final String testName) { public PredicatedMapTest(final String testName) {
super(testName); super(testName);

View File

@ -36,12 +36,7 @@ public class PredicatedSortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
protected static final Predicate<Object> truePredicate = TruePredicate.truePredicate(); protected static final Predicate<Object> truePredicate = TruePredicate.truePredicate();
protected static final Predicate<Object> testPredicate = new Predicate<Object>() { protected static final Predicate<Object> testPredicate = o -> o instanceof String;
@Override
public boolean evaluate(final Object o) {
return o instanceof String;
}
};
public PredicatedSortedMapTest(final String testName) { public PredicatedSortedMapTest(final String testName) {
super(testName); super(testName);

View File

@ -44,12 +44,7 @@ public class PredicatedMultiSetTest<T> extends AbstractMultiSetTest<T> {
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
protected Predicate<T> stringPredicate() { protected Predicate<T> stringPredicate() {
return new Predicate<T>() { return o -> o instanceof String;
@Override
public boolean evaluate(final T o) {
return o instanceof String;
}
};
} }
protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate(); protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();

View File

@ -73,12 +73,7 @@ public class PredicatedQueueTest<E> extends AbstractQueueTest<E> {
//------------------------------------------------------------ //------------------------------------------------------------
protected Predicate<E> testPredicate = protected Predicate<E> testPredicate =
new Predicate<E>() { o -> o instanceof String;
@Override
public boolean evaluate(final E o) {
return o instanceof String;
}
};
public Queue<E> makeTestQueue() { public Queue<E> makeTestQueue() {
return decorateCollection(new LinkedList<E>(), testPredicate); return decorateCollection(new LinkedList<E>(), testPredicate);

View File

@ -62,12 +62,7 @@ public class PredicatedNavigableSetTest<E> extends AbstractNavigableSetTest<E> {
//-------------------------------------------------------------------- //--------------------------------------------------------------------
protected Predicate<E> testPredicate = protected Predicate<E> testPredicate =
new Predicate<E>() { o -> o instanceof String && ((String) o).startsWith("A");
@Override
public boolean evaluate(final E o) {
return o instanceof String && ((String) o).startsWith("A");
}
};
protected PredicatedNavigableSet<E> makeTestSet() { protected PredicatedNavigableSet<E> makeTestSet() {
return PredicatedNavigableSet.predicatedNavigableSet(new TreeSet<E>(), testPredicate); return PredicatedNavigableSet.predicatedNavigableSet(new TreeSet<E>(), testPredicate);

View File

@ -56,12 +56,7 @@ public class PredicatedSetTest<E> extends AbstractSetTest<E> {
//-------------------------------------------------------------------- //--------------------------------------------------------------------
protected Predicate<E> testPredicate = protected Predicate<E> testPredicate =
new Predicate<E>() { o -> o instanceof String;
@Override
public boolean evaluate(final E o) {
return o instanceof String;
}
};
protected PredicatedSet<E> makeTestSet() { protected PredicatedSet<E> makeTestSet() {
return decorateSet(new HashSet<E>(), testPredicate); return decorateSet(new HashSet<E>(), testPredicate);

View File

@ -62,12 +62,7 @@ public class PredicatedSortedSetTest<E> extends AbstractSortedSetTest<E> {
//-------------------------------------------------------------------- //--------------------------------------------------------------------
protected Predicate<E> testPredicate = protected Predicate<E> testPredicate =
new Predicate<E>() { o -> o instanceof String && ((String) o).startsWith("A");
@Override
public boolean evaluate(final E o) {
return o instanceof String && ((String) o).startsWith("A");
}
};
protected PredicatedSortedSet<E> makeTestSet() { protected PredicatedSortedSet<E> makeTestSet() {
return PredicatedSortedSet.predicatedSortedSet(new TreeSet<E>(), testPredicate); return PredicatedSortedSet.predicatedSortedSet(new TreeSet<E>(), testPredicate);

View File

@ -35,26 +35,11 @@ import org.apache.commons.collections4.functors.NOPTransformer;
@SuppressWarnings("boxing") @SuppressWarnings("boxing")
public class TransformedSplitMapTest extends BulkTest { public class TransformedSplitMapTest extends BulkTest {
private final Transformer<Integer, String> intToString = new Transformer<Integer, String>() { private final Transformer<Integer, String> intToString = input -> String.valueOf(input);
@Override
public String transform(final Integer input) {
return String.valueOf(input);
}
};
private final Transformer<Object, Class<?>> objectToClass = new Transformer<Object, Class<?>>() { private final Transformer<Object, Class<?>> objectToClass = input -> input == null ? null : input.getClass();
@Override
public java.lang.Class<?> transform(final Object input) {
return input == null ? null : input.getClass();
}
};
private final Transformer<String, Integer> stringToInt = new Transformer<String, Integer>() { private final Transformer<String, Integer> stringToInt = input -> Integer.valueOf(input);
@Override
public Integer transform(final String input) {
return Integer.valueOf(input);
}
};
public TransformedSplitMapTest(final String testName) { public TransformedSplitMapTest(final String testName) {
super(testName); super(testName);