geforkt von Mirrors/Velocity
More robust check
Dieser Commit ist enthalten in:
Ursprung
01be943c3f
Commit
c26dc75c44
@ -36,16 +36,16 @@ public class CappedCollection<T> extends ForwardingCollection<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean add(T element) {
|
public boolean add(T element) {
|
||||||
Preconditions.checkState(this.delegate.size() + 1 <= upperSize, "collection is too large (%s)",
|
if (this.delegate.size() >= upperSize) {
|
||||||
this.delegate.size());
|
Preconditions.checkState(this.delegate.contains(element), "collection is too large (%s >= %s)",
|
||||||
|
this.delegate.size(), this.upperSize);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return this.delegate.add(element);
|
return this.delegate.add(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addAll(Collection<? extends T> collection) {
|
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);
|
return this.standardAddAll(collection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,4 +32,18 @@ class CappedCollectionTest {
|
|||||||
"items added to collection although it is too full");
|
"items added to collection although it is too full");
|
||||||
assertEquals(3, coll.size(), "collection grew in size unexpectedly");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren