13
0
geforkt von Mirrors/Velocity

More robust check

Dieser Commit ist enthalten in:
Andrew Steinborn 2019-05-10 04:44:01 -04:00
Ursprung 01be943c3f
Commit c26dc75c44
2 geänderte Dateien mit 19 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -36,16 +36,16 @@ public class CappedCollection<T> extends ForwardingCollection<T> {
@Override
public boolean add(T element) {
Preconditions.checkState(this.delegate.size() + 1 <= upperSize, "collection is too large (%s)",
this.delegate.size());
if (this.delegate.size() >= upperSize) {
Preconditions.checkState(this.delegate.contains(element), "collection is too large (%s >= %s)",
this.delegate.size(), this.upperSize);
return false;
}
return this.delegate.add(element);
}
@Override
public boolean addAll(Collection<? extends T> collection) {
Preconditions.checkState(this.delegate.size() + collection.size() <= upperSize,
"collection would grow too large (%s + %s > %s)",
this.delegate.size(), collection.size(), upperSize);
return this.standardAddAll(collection);
}
}

Datei anzeigen

@ -32,4 +32,18 @@ class CappedCollectionTest {
"items added to collection although it is too full");
assertEquals(3, coll.size(), "collection grew in size unexpectedly");
}
@Test
void handlesSetBehaviorCorrectly() {
Set<String> doesFill1 = ImmutableSet.of("coffee", "tea");
Set<String> doesFill2 = ImmutableSet.of("coffee", "chocolate");
Set<String> overfill = ImmutableSet.of("coffee", "Coke", "Pepsi");
Collection<String> coll = CappedCollection.newCappedSet(3);
assertTrue(coll.addAll(doesFill1), "did not add items");
assertTrue(coll.addAll(doesFill2), "did not add items");
assertThrows(IllegalStateException.class, () -> coll.addAll(overfill),
"items added to collection although it is too full");
assertEquals(3, coll.size(), "collection grew in size unexpectedly");
}
}