forked from PaperMC/Velocity
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Do not track plugin channels registered per-player on the pro…
…xy (PaperMC#591)" This reverts commit f62d759.
- Loading branch information
1 parent
8b43b06
commit e6b181a
Showing
7 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
proxy/src/main/java/com/velocitypowered/proxy/util/collect/CappedSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (C) 2019-2023 Velocity Contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.velocitypowered.proxy.util.collect; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ForwardingSet; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* An unsynchronized collection that puts an upper bound on the size of the collection. | ||
*/ | ||
public final class CappedSet<T> extends ForwardingSet<T> { | ||
|
||
private final Set<T> delegate; | ||
private final int upperSize; | ||
|
||
private CappedSet(Set<T> delegate, int upperSize) { | ||
this.delegate = delegate; | ||
this.upperSize = upperSize; | ||
} | ||
|
||
/** | ||
* Creates a capped collection backed by a {@link HashSet}. | ||
* | ||
* @param maxSize the maximum size of the collection | ||
* @param <T> the type of elements in the collection | ||
* @return the new collection | ||
*/ | ||
public static <T> Set<T> create(int maxSize) { | ||
return new CappedSet<>(new HashSet<>(), maxSize); | ||
} | ||
|
||
@Override | ||
protected Set<T> delegate() { | ||
return delegate; | ||
} | ||
|
||
@Override | ||
public boolean add(T element) { | ||
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) { | ||
return this.standardAddAll(collection); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
proxy/src/test/java/com/velocitypowered/proxy/util/collect/CappedSetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (C) 2019-2021 Velocity Contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.velocitypowered.proxy.util.collect; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import java.util.Collection; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CappedSetTest { | ||
|
||
@Test | ||
void basicVerification() { | ||
Collection<String> coll = CappedSet.create(1); | ||
assertTrue(coll.add("coffee"), "did not add single item"); | ||
assertThrows(IllegalStateException.class, () -> coll.add("tea"), | ||
"item was added to collection although it is too full"); | ||
assertEquals(1, coll.size(), "collection grew in size unexpectedly"); | ||
} | ||
|
||
@Test | ||
void testAddAll() { | ||
Set<String> doesFill1 = ImmutableSet.of("coffee", "tea"); | ||
Set<String> doesFill2 = ImmutableSet.of("chocolate"); | ||
Set<String> overfill = ImmutableSet.of("Coke", "Pepsi"); | ||
|
||
Collection<String> coll = CappedSet.create(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"); | ||
} | ||
|
||
@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 = CappedSet.create(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"); | ||
|
||
assertFalse(coll.addAll(doesFill1), "added items?!?"); | ||
|
||
assertEquals(3, coll.size(), "collection grew in size unexpectedly"); | ||
} | ||
} |