More lambdas, less boilerplate.
This commit is contained in:
parent
b29bae05ae
commit
a2317f6299
|
@ -604,12 +604,7 @@ public class CollectionUtils {
|
|||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
final Transformer<E, ?> transformer = new Transformer() {
|
||||
@Override
|
||||
public EquatorWrapper<?> transform(final Object input) {
|
||||
return new EquatorWrapper(equator, input);
|
||||
}
|
||||
};
|
||||
final Transformer<E, ?> transformer = input -> new EquatorWrapper(equator, input);
|
||||
|
||||
return isEqualCollection(collect(a, transformer), collect(b, transformer));
|
||||
}
|
||||
|
@ -1782,12 +1777,7 @@ public class CollectionUtils {
|
|||
final Iterable<? extends E> retain,
|
||||
final Equator<? super E> equator) {
|
||||
|
||||
final Transformer<E, EquatorWrapper<E>> transformer = new Transformer<E, EquatorWrapper<E>>() {
|
||||
@Override
|
||||
public EquatorWrapper<E> transform(final E input) {
|
||||
return new EquatorWrapper<>(equator, input);
|
||||
}
|
||||
};
|
||||
final Transformer<E, EquatorWrapper<E>> transformer = input -> new EquatorWrapper<>(equator, input);
|
||||
|
||||
final Set<EquatorWrapper<E>> retainSet =
|
||||
collect(retain, transformer, new HashSet<EquatorWrapper<E>>());
|
||||
|
@ -1860,12 +1850,7 @@ public class CollectionUtils {
|
|||
final Iterable<? extends E> remove,
|
||||
final Equator<? super E> equator) {
|
||||
|
||||
final Transformer<E, EquatorWrapper<E>> transformer = new Transformer<E, EquatorWrapper<E>>() {
|
||||
@Override
|
||||
public EquatorWrapper<E> transform(final E input) {
|
||||
return new EquatorWrapper<>(equator, input);
|
||||
}
|
||||
};
|
||||
final Transformer<E, EquatorWrapper<E>> transformer = input -> new EquatorWrapper<>(equator, input);
|
||||
|
||||
final Set<EquatorWrapper<E>> removeSet =
|
||||
collect(remove, transformer, new HashSet<EquatorWrapper<E>>());
|
||||
|
|
|
@ -125,12 +125,7 @@ public class SetUtils {
|
|||
throw new NullPointerException("Sets must not be null.");
|
||||
}
|
||||
|
||||
final Predicate<E> notContainedInB = new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E object) {
|
||||
return !b.contains(object);
|
||||
}
|
||||
};
|
||||
final Predicate<E> notContainedInB = object -> !b.contains(object);
|
||||
|
||||
return new SetView<E>() {
|
||||
@Override
|
||||
|
@ -285,12 +280,7 @@ public class SetUtils {
|
|||
throw new NullPointerException("Sets must not be null.");
|
||||
}
|
||||
|
||||
final Predicate<E> containedInB = new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E object) {
|
||||
return b.contains(object);
|
||||
}
|
||||
};
|
||||
final Predicate<E> containedInB = object -> b.contains(object);
|
||||
|
||||
return new SetView<E>() {
|
||||
@Override
|
||||
|
|
|
@ -438,23 +438,18 @@ public class MultiValueMap<K, V> extends AbstractMapDecorator<K, Object> impleme
|
|||
return null;
|
||||
}
|
||||
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
|
||||
public Entry<K, V> transform(final V input) {
|
||||
return new Entry<K, V>() {
|
||||
@Override
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
@Override
|
||||
public V getValue() {
|
||||
return input;
|
||||
}
|
||||
@Override
|
||||
public V setValue(final V value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
@Override
|
||||
public V getValue() {
|
||||
return input;
|
||||
}
|
||||
@Override
|
||||
public V setValue(final V value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
return new TransformIterator<>(new ValuesIterator(key), transformer);
|
||||
|
|
|
@ -626,14 +626,7 @@ public abstract class AbstractMultiValuedMap<K, V> implements MultiValuedMap<K,
|
|||
return null;
|
||||
}
|
||||
final K key = keyIterator.next();
|
||||
final Transformer<V, Entry<K, V>> entryTransformer = new Transformer<V, Entry<K, V>>() {
|
||||
|
||||
@Override
|
||||
public Entry<K, V> transform(final V input) {
|
||||
return new MultiValuedMapEntry(key, input);
|
||||
}
|
||||
|
||||
};
|
||||
final Transformer<V, Entry<K, V>> entryTransformer = input -> new MultiValuedMapEntry(key, input);
|
||||
return new TransformIterator<>(new ValuesIterator(key), entryTransformer);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -256,12 +256,7 @@ public abstract class AbstractMultiSet<E> extends AbstractCollection<E> implemen
|
|||
* @return the uniqueSet iterator
|
||||
*/
|
||||
protected Iterator<E> createUniqueSetIterator() {
|
||||
final Transformer<Entry<E>, E> transformer = new Transformer<Entry<E>, E>() {
|
||||
@Override
|
||||
public E transform(final Entry<E> entry) {
|
||||
return entry.getElement();
|
||||
}
|
||||
};
|
||||
final Transformer<Entry<E>, E> transformer = entry -> entry.getElement();
|
||||
return IteratorUtils.transformedIterator(entrySet().iterator(), transformer);
|
||||
}
|
||||
|
||||
|
|
|
@ -476,12 +476,7 @@ public class CollectionUtilsTest extends MockTestCase {
|
|||
@Test
|
||||
public void testSubtractWithPredicate() {
|
||||
// greater than 3
|
||||
final Predicate<Number> predicate = new Predicate<Number>() {
|
||||
@Override
|
||||
public boolean evaluate(final Number n) {
|
||||
return n.longValue() > 3L;
|
||||
}
|
||||
};
|
||||
final Predicate<Number> predicate = n -> n.longValue() > 3L;
|
||||
|
||||
final Collection<Number> col = CollectionUtils.subtract(iterableA, collectionC, predicate);
|
||||
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 StringBuffer result = new StringBuffer();
|
||||
result.append(CollectionUtils.forAllButLastDo(strings, new Closure<String>() {
|
||||
@Override
|
||||
public void execute(final String input) {
|
||||
result.append(input+";");
|
||||
}
|
||||
}));
|
||||
result.append(CollectionUtils.forAllButLastDo(strings, (Closure<String>) input -> result.append(input+";")));
|
||||
assertEquals("a;b;c", result.toString());
|
||||
|
||||
final Collection<String> oneString = Arrays.asList("a");
|
||||
final StringBuffer resultOne = new StringBuffer();
|
||||
resultOne.append(CollectionUtils.forAllButLastDo(oneString, new Closure<String>() {
|
||||
@Override
|
||||
public void execute(final String input) {
|
||||
resultOne.append(input+";");
|
||||
}
|
||||
}));
|
||||
resultOne.append(CollectionUtils.forAllButLastDo(oneString, (Closure<String>) input -> resultOne.append(input+";")));
|
||||
assertEquals("a", resultOne.toString());
|
||||
assertNull(CollectionUtils.forAllButLastDo(strings, (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>() {
|
||||
@Override
|
||||
public boolean evaluate(final Number input) {
|
||||
return input.intValue() == 2;
|
||||
}
|
||||
};
|
||||
private static Predicate<Number> EQUALS_TWO = input -> input.intValue() == 2;
|
||||
|
||||
//Up to here
|
||||
@Test
|
||||
|
@ -1296,12 +1276,7 @@ public class CollectionUtilsTest extends MockTestCase {
|
|||
assertTrue(collection.contains(2L) && !collection.contains(1));
|
||||
}
|
||||
|
||||
Transformer<Object, Integer> TRANSFORM_TO_INTEGER = new Transformer<Object, Integer>() {
|
||||
@Override
|
||||
public Integer transform(final Object input) {
|
||||
return Integer.valueOf(((Long)input).intValue());
|
||||
}
|
||||
};
|
||||
Transformer<Object, Integer> TRANSFORM_TO_INTEGER = input -> Integer.valueOf(((Long)input).intValue());
|
||||
|
||||
@Test
|
||||
public void transform1() {
|
||||
|
@ -1333,12 +1308,7 @@ public class CollectionUtilsTest extends MockTestCase {
|
|||
set.add(1L);
|
||||
set.add(2L);
|
||||
set.add(3L);
|
||||
CollectionUtils.transform(set, new Transformer<Object, Integer>() {
|
||||
@Override
|
||||
public Integer transform(final Object input) {
|
||||
return 4;
|
||||
}
|
||||
});
|
||||
CollectionUtils.transform(set, input -> 4);
|
||||
assertEquals(1, set.size());
|
||||
assertEquals(4, set.iterator().next());
|
||||
}
|
||||
|
@ -1831,20 +1801,10 @@ public class CollectionUtilsTest extends MockTestCase {
|
|||
assertFalse(CollectionUtils.matchesAll(null, null));
|
||||
assertFalse(CollectionUtils.matchesAll(collectionA, null));
|
||||
|
||||
final Predicate<Integer> lessThanFive = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer object) {
|
||||
return object < 5;
|
||||
}
|
||||
};
|
||||
final Predicate<Integer> lessThanFive = object -> object < 5;
|
||||
assertTrue(CollectionUtils.matchesAll(collectionA, lessThanFive));
|
||||
|
||||
final Predicate<Integer> lessThanFour = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer object) {
|
||||
return object < 4;
|
||||
}
|
||||
};
|
||||
final Predicate<Integer> lessThanFour = object -> object < 4;
|
||||
assertFalse(CollectionUtils.matchesAll(collectionA, lessThanFour));
|
||||
|
||||
assertTrue(CollectionUtils.matchesAll(null, lessThanFour));
|
||||
|
|
|
@ -103,12 +103,7 @@ public class FluentIterableTest {
|
|||
emptyIterable = Collections.emptyList();
|
||||
}
|
||||
|
||||
private static Predicate<Number> EVEN = new Predicate<Number>() {
|
||||
@Override
|
||||
public boolean evaluate(final Number input) {
|
||||
return input.intValue() % 2 == 0;
|
||||
}
|
||||
};
|
||||
private static Predicate<Number> EVEN = input -> input.intValue() % 2 == 0;
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
@Test
|
||||
|
@ -198,12 +193,7 @@ public class FluentIterableTest {
|
|||
|
||||
@Test
|
||||
public void filter() {
|
||||
final Predicate<Integer> smallerThan3 = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer object) {
|
||||
return object.intValue() < 3;
|
||||
}
|
||||
};
|
||||
final Predicate<Integer> smallerThan3 = object -> object.intValue() < 3;
|
||||
List<Integer> result = FluentIterable.of(iterableA).filter(smallerThan3).toList();
|
||||
assertEquals(3, result.size());
|
||||
assertEquals(Arrays.asList(1, 2, 2), result);
|
||||
|
@ -223,12 +213,7 @@ public class FluentIterableTest {
|
|||
@Test
|
||||
public void forEach() {
|
||||
final AtomicInteger sum = new AtomicInteger(0);
|
||||
final Closure<Integer> closure = new Closure<Integer>() {
|
||||
@Override
|
||||
public void execute(final Integer input) {
|
||||
sum.addAndGet(input);
|
||||
}
|
||||
};
|
||||
final Closure<Integer> closure = input -> sum.addAndGet(input);
|
||||
|
||||
FluentIterable.of(iterableA).forEach(closure);
|
||||
int expectedSum = 0;
|
||||
|
@ -315,12 +300,7 @@ public class FluentIterableTest {
|
|||
|
||||
@Test
|
||||
public void transform() {
|
||||
final Transformer<Integer, Integer> squared = new Transformer<Integer, Integer>() {
|
||||
@Override
|
||||
public Integer transform(final Integer object) {
|
||||
return object * object;
|
||||
}
|
||||
};
|
||||
final Transformer<Integer, Integer> squared = object -> object * object;
|
||||
List<Integer> result = FluentIterable.of(iterableA).transform(squared).toList();
|
||||
assertEquals(10, result.size());
|
||||
assertEquals(Arrays.asList(1, 4, 4, 9, 9, 9, 16, 16, 16, 16), result);
|
||||
|
|
|
@ -91,19 +91,9 @@ public class IterableUtilsTest {
|
|||
emptyIterable = Collections.emptyList();
|
||||
}
|
||||
|
||||
private static Predicate<Number> EQUALS_TWO = new Predicate<Number>() {
|
||||
@Override
|
||||
public boolean evaluate(final Number input) {
|
||||
return input.intValue() == 2;
|
||||
}
|
||||
};
|
||||
private static Predicate<Number> EQUALS_TWO = input -> input.intValue() == 2;
|
||||
|
||||
private static Predicate<Number> EVEN = new Predicate<Number>() {
|
||||
@Override
|
||||
public boolean evaluate(final Number input) {
|
||||
return input.intValue() % 2 == 0;
|
||||
}
|
||||
};
|
||||
private static Predicate<Number> EVEN = input -> input.intValue() % 2 == 0;
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
@Test
|
||||
|
@ -373,20 +363,10 @@ public class IterableUtilsTest {
|
|||
// expected
|
||||
}
|
||||
|
||||
final Predicate<Integer> lessThanFive = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer object) {
|
||||
return object < 5;
|
||||
}
|
||||
};
|
||||
final Predicate<Integer> lessThanFive = object -> object < 5;
|
||||
assertTrue(IterableUtils.matchesAll(iterableA, lessThanFive));
|
||||
|
||||
final Predicate<Integer> lessThanFour = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer object) {
|
||||
return object < 4;
|
||||
}
|
||||
};
|
||||
final Predicate<Integer> lessThanFour = object -> object < 4;
|
||||
assertFalse(IterableUtils.matchesAll(iterableA, lessThanFour));
|
||||
|
||||
assertTrue(IterableUtils.matchesAll(null, lessThanFour));
|
||||
|
@ -505,29 +485,18 @@ public class IterableUtilsTest {
|
|||
result = IterableUtils.toString(null);
|
||||
assertEquals("[]", result);
|
||||
|
||||
result = IterableUtils.toString(iterableA, new Transformer<Integer, String>() {
|
||||
@Override
|
||||
public String transform(final Integer input) {
|
||||
return new Integer(input * 2).toString();
|
||||
}
|
||||
});
|
||||
result = IterableUtils.toString(iterableA, input -> new Integer(input * 2).toString());
|
||||
assertEquals("[2, 4, 4, 6, 6, 6, 8, 8, 8, 8]", result);
|
||||
|
||||
result = IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
|
||||
@Override
|
||||
public String transform(final Integer input) {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}
|
||||
result = IterableUtils.toString(new ArrayList<Integer>(), input -> {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
});
|
||||
assertEquals("[]", result);
|
||||
|
||||
result = IterableUtils.toString(null, new Transformer<Integer, String>() {
|
||||
@Override
|
||||
public String transform(final Integer input) {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}
|
||||
result = IterableUtils.toString(null, input -> {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
});
|
||||
assertEquals("[]", result);
|
||||
}
|
||||
|
@ -535,12 +504,7 @@ public class IterableUtilsTest {
|
|||
@Test
|
||||
public void testToStringDelimiter() {
|
||||
|
||||
final Transformer<Integer, String> transformer = new Transformer<Integer, String>() {
|
||||
@Override
|
||||
public String transform(final Integer input) {
|
||||
return new Integer(input * 2).toString();
|
||||
}
|
||||
};
|
||||
final Transformer<Integer, String> transformer = input -> new Integer(input * 2).toString();
|
||||
|
||||
String result = IterableUtils.toString(iterableA, transformer, "", "", "");
|
||||
assertEquals("2446668888", result);
|
||||
|
@ -572,12 +536,9 @@ public class IterableUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testToStringWithNullArguments() {
|
||||
final String result = IterableUtils.toString(null, new Transformer<Integer, String>() {
|
||||
@Override
|
||||
public String transform(final Integer input) {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}
|
||||
final String result = IterableUtils.toString(null, input -> {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}, "", "(", ")");
|
||||
assertEquals("()", result);
|
||||
|
||||
|
@ -589,12 +550,9 @@ public class IterableUtilsTest {
|
|||
}
|
||||
|
||||
try {
|
||||
IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
|
||||
@Override
|
||||
public String transform(final Integer input) {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}
|
||||
IterableUtils.toString(new ArrayList<Integer>(), input -> {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}, null, "(", ")");
|
||||
fail("expecting NullPointerException");
|
||||
} catch (final NullPointerException ex) {
|
||||
|
@ -602,12 +560,9 @@ public class IterableUtilsTest {
|
|||
}
|
||||
|
||||
try {
|
||||
IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
|
||||
@Override
|
||||
public String transform(final Integer input) {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}
|
||||
IterableUtils.toString(new ArrayList<Integer>(), input -> {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}, "", null, ")");
|
||||
fail("expecting NullPointerException");
|
||||
} catch (final NullPointerException ex) {
|
||||
|
@ -615,12 +570,9 @@ public class IterableUtilsTest {
|
|||
}
|
||||
|
||||
try {
|
||||
IterableUtils.toString(new ArrayList<Integer>(), new Transformer<Integer, String>() {
|
||||
@Override
|
||||
public String transform(final Integer input) {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}
|
||||
IterableUtils.toString(new ArrayList<Integer>(), input -> {
|
||||
fail("not supposed to reach here");
|
||||
return "";
|
||||
}, "", "(", null);
|
||||
fail("expecting NullPointerException");
|
||||
} catch (final NullPointerException ex) {
|
||||
|
|
|
@ -122,12 +122,7 @@ public class ListUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testPredicatedList() {
|
||||
final Predicate<Object> predicate = new Predicate<Object>() {
|
||||
@Override
|
||||
public boolean evaluate(final Object o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
final Predicate<Object> predicate = o -> o instanceof String;
|
||||
final List<Object> list = ListUtils.predicatedList(new ArrayList<>(), predicate);
|
||||
assertTrue("returned object should be a PredicatedList", list instanceof PredicatedList);
|
||||
try {
|
||||
|
@ -452,12 +447,7 @@ public class ListUtilsTest {
|
|||
assertEquals(strings, partitionMax.get(0));
|
||||
}
|
||||
|
||||
private static Predicate<Number> EQUALS_TWO = new Predicate<Number>() {
|
||||
@Override
|
||||
public boolean evaluate(final Number input) {
|
||||
return input.intValue() == 2;
|
||||
}
|
||||
};
|
||||
private static Predicate<Number> EQUALS_TWO = input -> input.intValue() == 2;
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("boxing") // OK in test code
|
||||
|
|
|
@ -65,12 +65,7 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
|
|||
}
|
||||
|
||||
public Predicate<Object> getPredicate() {
|
||||
return new Predicate<Object>() {
|
||||
@Override
|
||||
public boolean evaluate(final Object o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
return o -> o instanceof String;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -122,14 +117,11 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
|
|||
|
||||
@Test
|
||||
public void testLazyMapTransformer() {
|
||||
final Map<Object, Object> map = MapUtils.lazyMap(new HashMap<>(), new Transformer<Object, Object>() {
|
||||
@Override
|
||||
public Object transform(final Object mapKey) {
|
||||
if (mapKey instanceof String) {
|
||||
return Integer.valueOf((String) mapKey);
|
||||
}
|
||||
return null;
|
||||
final Map<Object, Object> map = MapUtils.lazyMap(new HashMap<>(), (Transformer<Object, Object>) mapKey -> {
|
||||
if (mapKey instanceof String) {
|
||||
return Integer.valueOf((String) mapKey);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
assertEquals(0, map.size());
|
||||
|
@ -859,12 +851,7 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
|
|||
|
||||
// Now test key transform population
|
||||
final MultiValueMap<Integer, X> map = MultiValueMap.multiValueMap(new TreeMap<Integer, Collection<X>>());
|
||||
MapUtils.populateMap(map, list, new Transformer<X, Integer>() {
|
||||
@Override
|
||||
public Integer transform(final X input) {
|
||||
return input.key;
|
||||
}
|
||||
}, TransformerUtils.<X> nopTransformer());
|
||||
MapUtils.populateMap(map, list, (Transformer<X, Integer>) input -> input.key, TransformerUtils.<X> nopTransformer());
|
||||
assertEquals(list.size(), map.totalSize());
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
|
|
|
@ -92,12 +92,7 @@ public class MultiSetUtilsTest {
|
|||
*/
|
||||
@Test
|
||||
public void testPredicatedMultiSet() {
|
||||
final Predicate<String> predicate = new Predicate<String>() {
|
||||
@Override
|
||||
public boolean evaluate(final String object) {
|
||||
return object.length() == 1;
|
||||
};
|
||||
};
|
||||
final Predicate<String> predicate = object -> object.length() == 1;
|
||||
final MultiSet<String> predicated = MultiSetUtils.predicatedMultiSet(multiSet, predicate);
|
||||
assertEquals(multiSet.size(), predicated.size());
|
||||
assertEquals(multiSet.getCount("a"), predicated.getCount("a"));
|
||||
|
@ -115,12 +110,7 @@ public class MultiSetUtilsTest {
|
|||
}
|
||||
|
||||
try {
|
||||
MultiSetUtils.predicatedMultiSet(multiSet, new Predicate<String>() {
|
||||
@Override
|
||||
public boolean evaluate(final String object) {
|
||||
return object.equals("a");
|
||||
};
|
||||
});
|
||||
MultiSetUtils.predicatedMultiSet(multiSet, object -> object.equals("a"));
|
||||
fail("Predicate is violated for all elements not being 'a'");
|
||||
}
|
||||
catch (final IllegalArgumentException iae) {
|
||||
|
|
|
@ -231,12 +231,7 @@ public class SetUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testpredicatedSet() {
|
||||
final Predicate<Object> predicate = new Predicate<Object>() {
|
||||
@Override
|
||||
public boolean evaluate(final Object o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
final Predicate<Object> predicate = o -> o instanceof String;
|
||||
final Set<Object> set = SetUtils.predicatedSet(new HashSet<>(), predicate);
|
||||
assertTrue("returned object should be a PredicatedSet", set instanceof PredicatedSet);
|
||||
try {
|
||||
|
|
|
@ -41,12 +41,7 @@ public class SplitMapUtilsTest {
|
|||
private Map<String, Integer> backingMap;
|
||||
private TransformedSplitMap<String, String, String, Integer> transformedMap;
|
||||
|
||||
private final Transformer<String, Integer> stringToInt = new Transformer<String, Integer>() {
|
||||
@Override
|
||||
public Integer transform(final String input) {
|
||||
return Integer.valueOf(input);
|
||||
}
|
||||
};
|
||||
private final Transformer<String, Integer> stringToInt = input -> Integer.valueOf(input);
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
@ -84,32 +79,17 @@ public class SplitMapUtilsTest {
|
|||
// check individual operations
|
||||
int sz = map.size();
|
||||
|
||||
attemptPutOperation(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
map.clear();
|
||||
}
|
||||
});
|
||||
attemptPutOperation(() -> map.clear());
|
||||
|
||||
assertEquals(sz, map.size());
|
||||
|
||||
attemptPutOperation(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
map.put("foo", 100);
|
||||
}
|
||||
});
|
||||
attemptPutOperation(() -> map.put("foo", 100));
|
||||
|
||||
final HashMap<String, Integer> m = new HashMap<>();
|
||||
m.put("foo", 100);
|
||||
m.put("bar", 200);
|
||||
m.put("baz", 300);
|
||||
attemptPutOperation(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
map.putAll(m);
|
||||
}
|
||||
});
|
||||
attemptPutOperation(() -> map.putAll(m));
|
||||
|
||||
// equals, hashcode
|
||||
final IterableMap<String, Integer> other = SplitMapUtils.readableMap(transformedMap);
|
||||
|
@ -135,60 +115,15 @@ public class SplitMapUtilsTest {
|
|||
@SuppressWarnings("unchecked")
|
||||
public void testWritableMap() {
|
||||
final Map<String, String> map = SplitMapUtils.writableMap(transformedMap);
|
||||
attemptGetOperation(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
map.get(null);
|
||||
}
|
||||
});
|
||||
attemptGetOperation(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
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);
|
||||
}
|
||||
});
|
||||
attemptGetOperation(() -> map.get(null));
|
||||
attemptGetOperation(() -> map.entrySet());
|
||||
attemptGetOperation(() -> map.keySet());
|
||||
attemptGetOperation(() -> map.values());
|
||||
attemptGetOperation(() -> map.size());
|
||||
attemptGetOperation(() -> map.isEmpty());
|
||||
attemptGetOperation(() -> map.containsKey(null));
|
||||
attemptGetOperation(() -> map.containsValue(null));
|
||||
attemptGetOperation(() -> map.remove(null));
|
||||
|
||||
// equals, hashcode
|
||||
final Map<String, String> other = SplitMapUtils.writableMap(transformedMap);
|
||||
|
|
|
@ -247,12 +247,7 @@ public class TransformerUtilsTest {
|
|||
assertEquals("A", TransformerUtils.ifTransformer(TruePredicate.truePredicate(), a, b).transform(null));
|
||||
assertEquals("B", TransformerUtils.ifTransformer(FalsePredicate.falsePredicate(), a, b).transform(null));
|
||||
|
||||
final Predicate<Integer> lessThanFivePredicate = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer value) {
|
||||
return value < 5;
|
||||
}
|
||||
};
|
||||
final Predicate<Integer> lessThanFivePredicate = value -> value < 5;
|
||||
// if/else tests
|
||||
assertEquals("A", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(1));
|
||||
assertEquals("B", TransformerUtils.<Integer, String>ifTransformer(lessThanFivePredicate, a, b).transform(5));
|
||||
|
|
|
@ -44,12 +44,7 @@ public class PredicatedBagTest<T> extends AbstractBagTest<T> {
|
|||
//--------------------------------------------------------------------------
|
||||
|
||||
protected Predicate<T> stringPredicate() {
|
||||
return new Predicate<T>() {
|
||||
@Override
|
||||
public boolean evaluate(final T o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
return o -> o instanceof String;
|
||||
}
|
||||
|
||||
protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
|
||||
|
|
|
@ -46,12 +46,7 @@ public class PredicatedSortedBagTest<T> extends AbstractSortedBagTest<T> {
|
|||
//--------------------------------------------------------------------------
|
||||
|
||||
protected Predicate<T> stringPredicate() {
|
||||
return new Predicate<T>() {
|
||||
@Override
|
||||
public boolean evaluate(final T o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
return o -> o instanceof String;
|
||||
}
|
||||
|
||||
protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
*/
|
||||
package org.apache.commons.collections4.bag;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
||||
import org.apache.commons.collections4.Bag;
|
||||
|
@ -75,12 +73,7 @@ public class TreeBagTest<T> extends AbstractSortedBagTest<T> {
|
|||
// expected;
|
||||
}
|
||||
|
||||
final Bag<String> bag2 = new TreeBag<>(new Comparator<String>() {
|
||||
@Override
|
||||
public int compare(final String o1, final String o2) {
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
});
|
||||
final Bag<String> bag2 = new TreeBag<>((o1, o2) -> o1.compareTo(o2));
|
||||
try {
|
||||
// jdk bug: adding null to an empty TreeMap works
|
||||
// thus ensure that the bag is not empty before adding null
|
||||
|
|
|
@ -76,12 +76,7 @@ public class PredicatedCollectionTest<E> extends AbstractCollectionTest<E> {
|
|||
|
||||
//-----------------------------------------------------------------------
|
||||
protected Predicate<E> testPredicate =
|
||||
new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
o -> o instanceof String;
|
||||
|
||||
public Collection<E> makeTestCollection() {
|
||||
return decorateCollection(new ArrayList<E>(), testPredicate);
|
||||
|
|
|
@ -103,18 +103,15 @@ public class ComparatorChainTest extends AbstractComparatorTest<ComparatorChainT
|
|||
// -1 * Integer.MIN_VALUE is less than 0,
|
||||
// test that ComparatorChain handles this edge case correctly
|
||||
final ComparatorChain<Integer> chain = new ComparatorChain<>();
|
||||
chain.addComparator(new Comparator<Integer>() {
|
||||
@Override
|
||||
public int compare(final Integer a, final Integer b) {
|
||||
final int result = a.compareTo(b);
|
||||
if (result < 0) {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
if (result > 0) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
return 0;
|
||||
chain.addComparator((a, b) -> {
|
||||
final int result = a.compareTo(b);
|
||||
if (result < 0) {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
if (result > 0) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
return 0;
|
||||
}, true);
|
||||
|
||||
assertTrue(chain.compare(Integer.valueOf(4), Integer.valueOf(5)) > 0);
|
||||
|
|
|
@ -157,16 +157,13 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
|
|||
}
|
||||
|
||||
private void verifyElementsInPredicate(final String[] elements) {
|
||||
final Predicate<E> pred = new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E x) {
|
||||
for (final String element : elements) {
|
||||
if (element.equals(x)) {
|
||||
return true;
|
||||
}
|
||||
final Predicate<E> pred = x -> {
|
||||
for (final String element : elements) {
|
||||
if (element.equals(x)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
initIterator();
|
||||
iterator.setPredicate(pred);
|
||||
|
@ -199,10 +196,7 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
|
|||
* @return "filtered" iterator
|
||||
*/
|
||||
protected FilterIterator<E> makePassThroughFilter(final Iterator<E> i) {
|
||||
final Predicate<E> pred = new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E x) { return true; }
|
||||
};
|
||||
final Predicate<E> pred = x -> true;
|
||||
return new FilterIterator<>(i, pred);
|
||||
}
|
||||
|
||||
|
@ -214,10 +208,7 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
|
|||
* @return "filtered" iterator
|
||||
*/
|
||||
protected FilterIterator<E> makeBlockAllFilter(final Iterator<E> i) {
|
||||
final Predicate<E> pred = new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E x) { return false; }
|
||||
};
|
||||
final Predicate<E> pred = x -> false;
|
||||
return new FilterIterator<>(i, pred);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -70,47 +70,17 @@ public class FilterListIteratorTest {
|
|||
if (i % 6 == 0) { sixes.add(Integer.valueOf(i)); }
|
||||
}
|
||||
|
||||
truePred = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer x) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
truePred = x -> true;
|
||||
|
||||
falsePred = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer x) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
falsePred = x -> true;
|
||||
|
||||
evenPred = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer x) {
|
||||
return x % 2 == 0;
|
||||
}
|
||||
};
|
||||
evenPred = x -> x % 2 == 0;
|
||||
|
||||
oddPred = new Predicate<Integer>() {
|
||||
@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"
|
||||
}
|
||||
};
|
||||
oddPred = x -> x % 2 != 0;
|
||||
|
||||
threePred = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer x) {
|
||||
return x % 3 == 0;
|
||||
}
|
||||
};
|
||||
threePred = x -> x % 3 == 0;
|
||||
|
||||
fourPred = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer x) {
|
||||
return x % 4 == 0;
|
||||
}
|
||||
};
|
||||
fourPred = x -> x % 4 == 0;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -91,12 +91,7 @@ public class IteratorChainTest extends AbstractIteratorTest<String> {
|
|||
|
||||
public void testRemoveFromFilteredIterator() {
|
||||
|
||||
final Predicate<Integer> myPredicate = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer i) {
|
||||
return i.compareTo(Integer.valueOf(4)) < 0;
|
||||
}
|
||||
};
|
||||
final Predicate<Integer> myPredicate = i -> i.compareTo(Integer.valueOf(4)) < 0;
|
||||
|
||||
final List<Integer> list1 = new ArrayList<>();
|
||||
final List<Integer> list2 = new ArrayList<>();
|
||||
|
|
|
@ -105,12 +105,7 @@ public class LazyIteratorChainTest extends AbstractIteratorTest<String> {
|
|||
|
||||
public void testRemoveFromFilteredIterator() {
|
||||
|
||||
final Predicate<Integer> myPredicate = new Predicate<Integer>() {
|
||||
@Override
|
||||
public boolean evaluate(final Integer i) {
|
||||
return i.compareTo(Integer.valueOf(4)) < 0;
|
||||
}
|
||||
};
|
||||
final Predicate<Integer> myPredicate = i -> i.compareTo(Integer.valueOf(4)) < 0;
|
||||
|
||||
final List<Integer> list1 = new ArrayList<>();
|
||||
final List<Integer> list2 = new ArrayList<>();
|
||||
|
|
|
@ -56,12 +56,7 @@ public class PredicatedListTest<E> extends AbstractListTest<E> {
|
|||
//--------------------------------------------------------------------
|
||||
|
||||
protected Predicate<E> testPredicate =
|
||||
new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
o -> o instanceof String;
|
||||
|
||||
public List<E> makeTestList() {
|
||||
return decorateList(new ArrayList<E>(), testPredicate);
|
||||
|
|
|
@ -104,14 +104,11 @@ public class DefaultedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
|
|||
@SuppressWarnings("unchecked")
|
||||
public void testMapGet4() {
|
||||
final HashMap<K, V> base = new HashMap<>();
|
||||
final Map<K, V> map = DefaultedMap.defaultedMap(base, new Transformer<K, V>() {
|
||||
@Override
|
||||
public V transform(final K input) {
|
||||
if (input instanceof String) {
|
||||
return (V) "NULL";
|
||||
}
|
||||
return (V) "NULL_OBJECT";
|
||||
final Map<K, V> map = DefaultedMap.defaultedMap(base, (Transformer<K, V>) input -> {
|
||||
if (input instanceof String) {
|
||||
return (V) "NULL";
|
||||
}
|
||||
return (V) "NULL_OBJECT";
|
||||
});
|
||||
|
||||
assertEquals(0, map.size());
|
||||
|
|
|
@ -72,12 +72,7 @@ public class LazyMapTest<K, V> extends AbstractIterableMapTest<K, V> {
|
|||
|
||||
@Test
|
||||
public void mapGetWithTransformer() {
|
||||
final Transformer<Number, Integer> intConverter = new Transformer<Number, Integer>(){
|
||||
@Override
|
||||
public Integer transform(final Number input) {
|
||||
return input.intValue();
|
||||
}
|
||||
};
|
||||
final Transformer<Number, Integer> intConverter = input -> input.intValue();
|
||||
final Map<Long, Number> map = lazyMap(new HashMap<Long,Number>(), intConverter );
|
||||
assertEquals(0, map.size());
|
||||
final Number i1 = map.get(123L);
|
||||
|
|
|
@ -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> testPredicate = new Predicate<Object>() {
|
||||
@Override
|
||||
public boolean evaluate(final Object o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
protected static final Predicate<Object> testPredicate = o -> o instanceof String;
|
||||
|
||||
public PredicatedMapTest(final String testName) {
|
||||
super(testName);
|
||||
|
|
|
@ -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> testPredicate = new Predicate<Object>() {
|
||||
@Override
|
||||
public boolean evaluate(final Object o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
protected static final Predicate<Object> testPredicate = o -> o instanceof String;
|
||||
|
||||
public PredicatedSortedMapTest(final String testName) {
|
||||
super(testName);
|
||||
|
|
|
@ -44,12 +44,7 @@ public class PredicatedMultiSetTest<T> extends AbstractMultiSetTest<T> {
|
|||
//--------------------------------------------------------------------------
|
||||
|
||||
protected Predicate<T> stringPredicate() {
|
||||
return new Predicate<T>() {
|
||||
@Override
|
||||
public boolean evaluate(final T o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
return o -> o instanceof String;
|
||||
}
|
||||
|
||||
protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
|
||||
|
|
|
@ -73,12 +73,7 @@ public class PredicatedQueueTest<E> extends AbstractQueueTest<E> {
|
|||
//------------------------------------------------------------
|
||||
|
||||
protected Predicate<E> testPredicate =
|
||||
new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
o -> o instanceof String;
|
||||
|
||||
public Queue<E> makeTestQueue() {
|
||||
return decorateCollection(new LinkedList<E>(), testPredicate);
|
||||
|
|
|
@ -62,12 +62,7 @@ public class PredicatedNavigableSetTest<E> extends AbstractNavigableSetTest<E> {
|
|||
|
||||
//--------------------------------------------------------------------
|
||||
protected Predicate<E> testPredicate =
|
||||
new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E o) {
|
||||
return o instanceof String && ((String) o).startsWith("A");
|
||||
}
|
||||
};
|
||||
o -> o instanceof String && ((String) o).startsWith("A");
|
||||
|
||||
protected PredicatedNavigableSet<E> makeTestSet() {
|
||||
return PredicatedNavigableSet.predicatedNavigableSet(new TreeSet<E>(), testPredicate);
|
||||
|
|
|
@ -56,12 +56,7 @@ public class PredicatedSetTest<E> extends AbstractSetTest<E> {
|
|||
//--------------------------------------------------------------------
|
||||
|
||||
protected Predicate<E> testPredicate =
|
||||
new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E o) {
|
||||
return o instanceof String;
|
||||
}
|
||||
};
|
||||
o -> o instanceof String;
|
||||
|
||||
protected PredicatedSet<E> makeTestSet() {
|
||||
return decorateSet(new HashSet<E>(), testPredicate);
|
||||
|
|
|
@ -62,12 +62,7 @@ public class PredicatedSortedSetTest<E> extends AbstractSortedSetTest<E> {
|
|||
|
||||
//--------------------------------------------------------------------
|
||||
protected Predicate<E> testPredicate =
|
||||
new Predicate<E>() {
|
||||
@Override
|
||||
public boolean evaluate(final E o) {
|
||||
return o instanceof String && ((String) o).startsWith("A");
|
||||
}
|
||||
};
|
||||
o -> o instanceof String && ((String) o).startsWith("A");
|
||||
|
||||
protected PredicatedSortedSet<E> makeTestSet() {
|
||||
return PredicatedSortedSet.predicatedSortedSet(new TreeSet<E>(), testPredicate);
|
||||
|
|
|
@ -35,26 +35,11 @@ import org.apache.commons.collections4.functors.NOPTransformer;
|
|||
@SuppressWarnings("boxing")
|
||||
public class TransformedSplitMapTest extends BulkTest {
|
||||
|
||||
private final Transformer<Integer, String> intToString = new Transformer<Integer, String>() {
|
||||
@Override
|
||||
public String transform(final Integer input) {
|
||||
return String.valueOf(input);
|
||||
}
|
||||
};
|
||||
private final Transformer<Integer, String> intToString = input -> String.valueOf(input);
|
||||
|
||||
private final Transformer<Object, Class<?>> objectToClass = new Transformer<Object, Class<?>>() {
|
||||
@Override
|
||||
public java.lang.Class<?> transform(final Object input) {
|
||||
return input == null ? null : input.getClass();
|
||||
}
|
||||
};
|
||||
private final Transformer<Object, Class<?>> objectToClass = input -> input == null ? null : input.getClass();
|
||||
|
||||
private final Transformer<String, Integer> stringToInt = new Transformer<String, Integer>() {
|
||||
@Override
|
||||
public Integer transform(final String input) {
|
||||
return Integer.valueOf(input);
|
||||
}
|
||||
};
|
||||
private final Transformer<String, Integer> stringToInt = input -> Integer.valueOf(input);
|
||||
|
||||
public TransformedSplitMapTest(final String testName) {
|
||||
super(testName);
|
||||
|
|
Loading…
Reference in New Issue