Skip to content

Commit

Permalink
Change Validate to Preconditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Intybyte committed Sep 22, 2024
1 parent 0a7fea8 commit b038a29
Show file tree
Hide file tree
Showing 120 changed files with 564 additions and 569 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ But do try to follow our code style as best as you can.*
* Do not declare multiple fields/variables on the same line! (e.g. Don't do this: `int x, y, z;`)
* Use a Logger, try to avoid `System.out.println(...)` and `Throwable#printStacktrace()`, use `Logger#log` instead!
* Do not use Exceptions to validate data, empty catch blocks are a very bad practice, use other means like a regular expression to validate data.
* If a parameter is annotated with `@Nonnull`, you should enforce this behaviour by doing `Validate.notNull(variable, "...");` and give a meaningful message about what went wrong
* If a parameter is annotated with `@Nonnull`, you should enforce this behaviour by doing `Preconditions.checkNotNull(variable, "...");` and give a meaningful message about what went wrong
* Any `switch/case` should always have a `default:` case at the end.
* If you are working with a resource that must be closed, use a `try/with-resource`, this will automatically close the resource at the end. (e.g. `try (InputStream stream = ...) {`)
* Array designators should be placed behind the type, not the variable name. (e.g. `int[] myArray`)
Expand Down
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,6 @@
</exclusion>
</exclusions>
</dependency>
<!-- TODO: Remove this dependency -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.Server;

import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
Expand Down Expand Up @@ -227,7 +227,7 @@ public boolean isMinecraftVersion(int minecraftVersion, int patchVersion) {
* @return Whether this {@link MinecraftVersion} is newer or equal to the given {@link MinecraftVersion}
*/
public boolean isAtLeast(@Nonnull MinecraftVersion version) {
Validate.notNull(version, "A Minecraft version cannot be null!");
Preconditions.checkNotNull(version, "A Minecraft version cannot be null!");

if (this == UNKNOWN) {
return false;
Expand Down Expand Up @@ -262,7 +262,7 @@ public boolean isAtLeast(@Nonnull MinecraftVersion version) {
* @return Whether this {@link MinecraftVersion} is older than the given one
*/
public boolean isBefore(@Nonnull MinecraftVersion version) {
Validate.notNull(version, "A Minecraft version cannot be null!");
Preconditions.checkNotNull(version, "A Minecraft version cannot be null!");

if (this == UNKNOWN) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down Expand Up @@ -86,7 +86,7 @@ public interface SlimefunAddon {
* @return Whether this {@link SlimefunAddon} depends on the given {@link Plugin}
*/
default boolean hasDependency(@Nonnull String dependency) {
Validate.notNull(dependency, "The dependency cannot be null");
Preconditions.checkNotNull(dependency, "The dependency cannot be null");

// Well... it cannot depend on itself but you get the idea.
if (getJavaPlugin().getName().equalsIgnoreCase(dependency)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;

import io.github.bakedlibs.dough.common.CommonPatterns;

Expand Down Expand Up @@ -45,7 +45,7 @@ public enum SlimefunBranch {
private final boolean official;

SlimefunBranch(@Nonnull String name, boolean official) {
Validate.notNull(name, "The branch name cannot be null");
Preconditions.checkNotNull(name, "The branch name cannot be null");

this.name = name;
this.official = official;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -31,9 +31,9 @@ public class AsyncAutoEnchanterProcessEvent extends Event implements Cancellable
public AsyncAutoEnchanterProcessEvent(@Nonnull ItemStack item, @Nonnull ItemStack enchantedBook, @Nonnull BlockMenu menu) {
super(true);

Validate.notNull(item, "The item to enchant cannot be null!");
Validate.notNull(enchantedBook, "The enchanted book to enchant cannot be null!");
Validate.notNull(menu, "The menu of auto-enchanter cannot be null!");
Preconditions.checkNotNull(item, "The item to enchant cannot be null!");
Preconditions.checkNotNull(enchantedBook, "The enchanted book to enchant cannot be null!");
Preconditions.checkNotNull(menu, "The menu of auto-enchanter cannot be null!");

this.item = item;
this.enchantedBook = enchantedBook;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -32,7 +32,7 @@ public class AsyncProfileLoadEvent extends Event {
public AsyncProfileLoadEvent(@Nonnull PlayerProfile profile) {
super(true);

Validate.notNull(profile, "The Profile cannot be null");
Preconditions.checkNotNull(profile, "The Profile cannot be null");

this.uniqueId = profile.getUUID();
this.profile = profile;
Expand All @@ -56,8 +56,8 @@ public PlayerProfile getProfile() {
* The {@link PlayerProfile}
*/
public void setProfile(@Nonnull PlayerProfile profile) {
Validate.notNull(profile, "The PlayerProfile cannot be null!");
Validate.isTrue(profile.getUUID().equals(uniqueId), "Cannot inject a PlayerProfile with a different UUID");
Preconditions.checkNotNull(profile, "The PlayerProfile cannot be null!");
Preconditions.checkArgument(profile.getUUID().equals(uniqueId), "Cannot inject a PlayerProfile with a different UUID");

this.profile = profile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.block.Block;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
Expand Down Expand Up @@ -75,7 +75,7 @@ public ItemStack getItemStack() {
* The {@link ItemStack} to be placed
*/
public void setItemStack(@Nonnull ItemStack item) {
Validate.notNull(item, "The ItemStack must not be null!");
Preconditions.checkNotNull(item, "The ItemStack must not be null!");

if (!locked) {
this.placedItem = item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
Expand Down Expand Up @@ -63,7 +63,7 @@ public Vector getVelocity() {
* The {@link Vector} velocity to apply
*/
public void setVelocity(@Nonnull Vector velocity) {
Validate.notNull(velocity);
Preconditions.checkNotNull(velocity);
this.velocity = velocity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
Expand Down Expand Up @@ -84,8 +84,8 @@ public ItemStack getConsumedItem() {
* The new {@link ItemStack}
*/
public void setConsumedItem(@Nonnull ItemStack item) {
Validate.notNull(item, "The consumed Item cannot be null!");
Validate.isTrue(item.getItemMeta() instanceof PotionMeta, "The item must be a potion!");
Preconditions.checkNotNull(item, "The consumed Item cannot be null!");
Preconditions.checkArgument(item.getItemMeta() instanceof PotionMeta, "The item must be a potion!");

this.consumedItem = item;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
Expand Down Expand Up @@ -38,10 +38,10 @@ public class ExplosiveToolBreakBlocksEvent extends PlayerEvent implements Cancel
public ExplosiveToolBreakBlocksEvent(Player player, Block block, List<Block> blocks, ItemStack item, ExplosiveTool explosiveTool) {
super(player);

Validate.notNull(block, "The center block cannot be null!");
Validate.notNull(blocks, "Blocks cannot be null");
Validate.notNull(item, "Item cannot be null");
Validate.notNull(explosiveTool, "ExplosiveTool cannot be null");
Preconditions.checkNotNull(block, "The center block cannot be null!");
Preconditions.checkNotNull(blocks, "Blocks cannot be null");
Preconditions.checkNotNull(item, "Item cannot be null");
Preconditions.checkNotNull(explosiveTool, "ExplosiveTool cannot be null");

this.mainBlock = block;
this.additionalBlocks = blocks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
Expand Down Expand Up @@ -35,9 +35,9 @@ public class PlayerPreResearchEvent extends Event implements Cancellable {

@ParametersAreNonnullByDefault
public PlayerPreResearchEvent(Player p, Research research, SlimefunItem slimefunItem) {
Validate.notNull(p, "The Player cannot be null");
Validate.notNull(research, "Research cannot be null");
Validate.notNull(slimefunItem, "SlimefunItem cannot be null");
Preconditions.checkNotNull(p, "The Player cannot be null");
Preconditions.checkNotNull(research, "Research cannot be null");
Preconditions.checkNotNull(slimefunItem, "SlimefunItem cannot be null");

this.player = p;
this.research = research;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand Down Expand Up @@ -168,12 +168,12 @@ public Result useBlock() {
}

public void setUseItem(@Nonnull Result result) {
Validate.notNull(result, "Result cannot be null");
Preconditions.checkNotNull(result, "Result cannot be null");
itemResult = result;
}

public void setUseBlock(@Nonnull Result result) {
Validate.notNull(result, "Result cannot be null");
Preconditions.checkNotNull(result, "Result cannot be null");
blockResult = result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.Location;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -24,8 +24,8 @@ public class ReactorExplodeEvent extends Event {
private final Reactor reactor;

public ReactorExplodeEvent(@Nonnull Location l, @Nonnull Reactor reactor) {
Validate.notNull(l, "A Location must be provided");
Validate.notNull(reactor, "A Reactor cannot be null");
Preconditions.checkNotNull(l, "A Location must be provided");
Preconditions.checkNotNull(reactor, "A Reactor cannot be null");

this.location = l;
this.reactor = reactor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
Expand Down Expand Up @@ -30,8 +30,8 @@ public class ResearchUnlockEvent extends Event implements Cancellable {
public ResearchUnlockEvent(@Nonnull Player p, @Nonnull Research research) {
super(!Bukkit.isPrimaryThread());

Validate.notNull(p, "The Player cannot be null");
Validate.notNull(research, "Research cannot be null");
Preconditions.checkNotNull(p, "The Player cannot be null");
Preconditions.checkNotNull(research, "Research cannot be null");

this.player = p;
this.research = research;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
Expand All @@ -28,9 +28,9 @@ public class SlimefunGuideOpenEvent extends Event implements Cancellable {
private boolean cancelled;

public SlimefunGuideOpenEvent(@Nonnull Player p, @Nonnull ItemStack guide, @Nonnull SlimefunGuideMode layout) {
Validate.notNull(p, "The Player cannot be null");
Validate.notNull(guide, "Guide cannot be null");
Validate.notNull(layout, "Layout cannot be null");
Preconditions.checkNotNull(p, "The Player cannot be null");
Preconditions.checkNotNull(guide, "Guide cannot be null");
Preconditions.checkNotNull(layout, "Layout cannot be null");
this.player = p;
this.guide = guide;
this.layout = layout;
Expand Down Expand Up @@ -76,7 +76,7 @@ public SlimefunGuideMode getGuideLayout() {
* The new {@link SlimefunGuideMode}
*/
public void setGuideLayout(@Nonnull SlimefunGuideMode layout) {
Validate.notNull(layout, "You must specify a layout that is not-null!");
Preconditions.checkNotNull(layout, "You must specify a layout that is not-null!");
this.layout = layout;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
Expand Down Expand Up @@ -83,7 +83,7 @@ public SlimefunItemSpawnEvent(Location location, ItemStack itemStack, ItemSpawnR
* The {@link Location} where to drop the {@link ItemStack}
*/
public void setLocation(@Nonnull Location location) {
Validate.notNull(location, "The Location cannot be null!");
Preconditions.checkNotNull(location, "The Location cannot be null!");

this.location = location;
}
Expand All @@ -104,8 +104,8 @@ public void setLocation(@Nonnull Location location) {
* The {@link ItemStack} to drop
*/
public void setItemStack(@Nonnull ItemStack itemStack) {
Validate.notNull(itemStack, "Cannot drop null.");
Validate.isTrue(!itemStack.getType().isAir(), "Cannot drop air.");
Preconditions.checkNotNull(itemStack, "Cannot drop null.");
Preconditions.checkArgument(!itemStack.getType().isAir(), "Cannot drop air.");

this.itemStack = itemStack;
}
Expand Down
Loading

0 comments on commit b038a29

Please sign in to comment.