Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-06 00:00:47 +01:00
More robust check
Dieser Commit ist enthalten in:
Ursprung
01be943c3f
Commit
c26dc75c44
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren