3
0
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:
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 @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);
} }
} }

Datei anzeigen

@ -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");
}
} }