Skip to content
Permalink
Browse files
COLLECTIONS-788 - java8 improvements: (#228)
* Replace Collections.sort() with List.sort()
* Replace lambda with method reference
* Replace Loop with Collection.removeIf()
* Use lambda

Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
  • Loading branch information
arturobernalg and garydgregory committed Mar 4, 2022
1 parent dd69e59 commit 35a6778db8b8b2abab7e41a5b11098df1405de15
Showing 6 changed files with 16 additions and 35 deletions.
@@ -213,7 +213,7 @@ public void filter() {
@Test
public void forEach() {
final AtomicInteger sum = new AtomicInteger(0);
final Closure<Integer> closure = input -> sum.addAndGet(input);
final Closure<Integer> closure = sum::addAndGet;

FluentIterable.of(iterableA).forEach(closure);
int expectedSum = 0;
@@ -167,9 +167,7 @@ public void testInvertEmptyMap() {
@Test
public void testInvertMapNull() {
final Map<String, String> nullMap = null;
final Exception exception = assertThrows(NullPointerException.class, () -> {
MapUtils.invertMap(nullMap);
});
final Exception exception = assertThrows(NullPointerException.class, () -> MapUtils.invertMap(nullMap));
final String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains("map"));
}
@@ -1015,30 +1013,22 @@ public void testTransformedSortedMap() {

@Test
public void testUnmodifiableMap() {
final Exception exception = assertThrows(UnsupportedOperationException.class, () -> {
MapUtils.unmodifiableMap(new HashMap<>()).clear();
});
final Exception exception = assertThrows(UnsupportedOperationException.class, () -> MapUtils.unmodifiableMap(new HashMap<>()).clear());
}

@Test
public void testUnmodifiableSortedMap() {
final Exception exception = assertThrows(UnsupportedOperationException.class, () -> {
MapUtils.unmodifiableSortedMap(new TreeMap<>()).clear();
});
final Exception exception = assertThrows(UnsupportedOperationException.class, () -> MapUtils.unmodifiableSortedMap(new TreeMap<>()).clear());
}

@Test
public void testFixedSizeMap() {
final Exception exception = assertThrows(IllegalArgumentException.class, () -> {
MapUtils.fixedSizeMap(new HashMap<>()).put(new Object(), new Object());
});
final Exception exception = assertThrows(IllegalArgumentException.class, () -> MapUtils.fixedSizeMap(new HashMap<>()).put(new Object(), new Object()));
}

@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<Long, Long>()).put(1L, 1L));
}

@Test
@@ -79,7 +79,7 @@ public void testReadableMap() {
// check individual operations
int sz = map.size();

attemptPutOperation(() -> map.clear());
attemptPutOperation(map::clear);

assertEquals(sz, map.size());

@@ -116,11 +116,11 @@ public void testAlreadyReadableMap() {
public void testWritableMap() {
final Map<String, String> map = SplitMapUtils.writableMap(transformedMap);
attemptGetOperation(() -> map.get(null));
attemptGetOperation(() -> map.entrySet());
attemptGetOperation(() -> map.keySet());
attemptGetOperation(() -> map.values());
attemptGetOperation(() -> map.size());
attemptGetOperation(() -> map.isEmpty());
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));
@@ -963,7 +963,7 @@ public void testCollectionRemoveIf() {
final int size = getCollection().size();
final int targetCount = Collections.frequency(elements, target);

final Predicate<E> filter = e -> target.equals(e);
final Predicate<E> filter = target::equals;

assertTrue("Full collection removeIf should work", getCollection().removeIf(filter));
getConfirmed().removeIf(filter);
@@ -700,12 +700,7 @@ public void run() {
}
}
synchronized (map) {
for (final Iterator<Map.Entry<Object, Thread>> iter = map.entrySet().iterator(); iter.hasNext();) {
final Map.Entry<Object, Thread> entry = iter.next();
if (entry.getValue() == this) {
iter.remove();
}
}
map.entrySet().removeIf(entry -> entry.getValue() == this);
}
} catch (final InterruptedException e) {
fail("Unexpected InterruptedException");
@@ -866,11 +861,7 @@ public void run() {
}
}
synchronized (map) {
for (final Iterator<Thread> iter = map.values().iterator(); iter.hasNext();) {
if (iter.next() == this) {
iter.remove();
}
}
map.values().removeIf(thread1 -> thread1 == this);
}
} catch (final InterruptedException e) {
fail("Unexpected InterruptedException");
@@ -261,7 +261,7 @@ public void testPurgeValues() throws Exception {
public void testCustomPurge() {
final List<Integer> expiredValues = new ArrayList<>();
@SuppressWarnings("unchecked")
final Consumer<Integer> consumer = (Consumer<Integer> & Serializable) v -> expiredValues.add(v);
final Consumer<Integer> consumer = (Consumer<Integer> & Serializable) expiredValues::add;
final Map<Integer, Integer> map = new ReferenceMap<Integer, Integer>(ReferenceStrength.WEAK, ReferenceStrength.HARD, false) {
private static final long serialVersionUID = 1L;

0 comments on commit 35a6778

Please sign in to comment.