#1182: Consolidate Preconditions use and minor cleanup

By: Doc <nachito94@msn.com>
This commit is contained in:
CraftBukkit/Spigot
2023-06-12 19:41:02 +10:00
parent 5ff68bfbcb
commit ff78bf30f6
72 changed files with 695 additions and 855 deletions

View File

@@ -268,9 +268,7 @@ public final class CraftMagicNumbers implements UnsafeValues {
@Override
public Advancement loadAdvancement(NamespacedKey key, String advancement) {
if (Bukkit.getAdvancement(key) != null) {
throw new IllegalArgumentException("Advancement " + key + " already exists.");
}
Preconditions.checkArgument(Bukkit.getAdvancement(key) == null, "Advancement %s already exists", key);
MinecraftKey minecraftkey = CraftNamespacedKey.toMinecraft(key);
JsonElement jsonelement = AdvancementDataWorld.GSON.fromJson(advancement, JsonElement.class);

View File

@@ -1,5 +1,6 @@
package org.bukkit.craftbukkit.util;
import com.google.common.base.Preconditions;
import java.util.HashSet;
import java.util.List;
import net.minecraft.server.MinecraftServer;
@@ -16,9 +17,7 @@ public class LazyPlayerSet extends LazyHashSet<Player> {
@Override
HashSet<Player> makeReference() {
if (reference != null) {
throw new IllegalStateException("Reference already created!");
}
Preconditions.checkState(reference == null, "Reference already created!");
List<EntityPlayer> players = server.getPlayerList().players;
HashSet<Player> reference = new HashSet<Player>(players.size());
for (EntityPlayer player : players) {

View File

@@ -1,5 +1,6 @@
package org.bukkit.craftbukkit.util;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
@@ -273,9 +274,7 @@ public class UnsafeList<E> extends AbstractList<E> implements List<E>, RandomAcc
@Override
public void remove() {
if (lastRet < 0) {
throw new IllegalStateException();
}
Preconditions.checkState(lastRet >= 0, "");
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();

View File

@@ -1,5 +1,6 @@
package org.bukkit.craftbukkit.util;
import com.google.common.base.Preconditions;
import java.util.concurrent.ExecutionException;
public abstract class Waitable<T> implements Runnable {
@@ -15,9 +16,7 @@ public abstract class Waitable<T> implements Runnable {
@Override
public final void run() {
synchronized (this) {
if (status != Status.WAITING) {
throw new IllegalStateException("Invalid state " + status);
}
Preconditions.checkState(status == Status.WAITING, "Invalid state %s", status);
status = Status.RUNNING;
}
try {

View File

@@ -1,23 +1,23 @@
package org.bukkit.craftbukkit.util;
import com.google.common.base.Preconditions;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.apache.commons.lang.Validate;
public final class WeakCollection<T> implements Collection<T> {
static final Object NO_VALUE = new Object();
private final Collection<WeakReference<T>> collection;
public WeakCollection() {
collection = new ArrayList<WeakReference<T>>();
collection = new ArrayList<>();
}
@Override
public boolean add(T value) {
Validate.notNull(value, "Cannot add null value");
Preconditions.checkArgument(value != null, "Cannot add null value");
return collection.add(new WeakReference<T>(value));
}
@@ -26,7 +26,7 @@ public final class WeakCollection<T> implements Collection<T> {
Collection<WeakReference<T>> values = this.collection;
boolean ret = false;
for (T value : collection) {
Validate.notNull(value, "Cannot add null value");
Preconditions.checkArgument(value != null, "Cannot add null value");
ret |= values.add(new WeakReference<T>(value));
}
return ret;
@@ -103,9 +103,7 @@ public final class WeakCollection<T> implements Collection<T> {
@Override
public void remove() throws IllegalStateException {
if (value != NO_VALUE) {
throw new IllegalStateException("No last element");
}
Preconditions.checkState(value == NO_VALUE, "No last element");
value = null;
it.remove();