Skip to content

Commit

Permalink
fix: don't throw on null IP in builder
Browse files Browse the repository at this point in the history
  • Loading branch information
qixils committed Jan 6, 2024
1 parent 1c17754 commit 5ed5a39
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.annotation.CheckReturnValue;
import java.net.InetAddress;
Expand All @@ -20,28 +21,27 @@ interface CrowdControlBuilder<B extends CrowdControlBuilder<B>> {
*
* @param IP IP to connect to
* @return this builder
* @throws IllegalArgumentException the IP was null or blank
* @since 3.9.0
*/
@ApiStatus.AvailableSince("3.9.0")
@CheckReturnValue
@Contract("_ -> this")
@NotNull
B ip(@NotNull InetAddress IP);
B ip(@Nullable InetAddress IP);

/**
* Sets the IP that the Crowd Control client will connect to.
*
* @param IP IP to connect to
* @return this builder
* @throws IllegalArgumentException the IP was null or blank
* @throws IllegalArgumentException the IP was invalid
* @since 3.9.0
*/
@ApiStatus.AvailableSince("3.9.0")
@CheckReturnValue
@Contract("_ -> this")
@NotNull
B ip(@NotNull String IP) throws IllegalArgumentException;
B ip(@Nullable String IP) throws IllegalArgumentException;

/**
* Sets the port that will be used by the Crowd Control client or server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.CheckReturnValue;
import java.net.InetAddress;
Expand All @@ -19,6 +22,9 @@
@SuppressWarnings("unchecked")
@ApiStatus.AvailableSince("3.0.0")
abstract class CrowdControlBuilderBase<B extends CrowdControlBuilderBase<B>> implements CrowdControlBuilder<B> {

protected final Logger logger = LoggerFactory.getLogger("CrowdControl/Builder");

/**
* A function (usually a constructor) that creates a new {@link SocketManager}
* given a {@link CrowdControl} instance.
Expand Down Expand Up @@ -73,14 +79,13 @@ abstract class CrowdControlBuilderBase<B extends CrowdControlBuilderBase<B>> imp
*
* @param IP IP to connect to
* @return this builder
* @throws IllegalArgumentException the IP was null or blank
* @since 3.9.0
*/
@ApiStatus.AvailableSince("3.9.0")
@CheckReturnValue
@Contract("_ -> this")
public @NotNull B ip(@NotNull InetAddress IP) throws IllegalArgumentException {
this.IP = ExceptionUtil.validateNotNull(IP, "IP");
public @NotNull B ip(@Nullable InetAddress IP) throws IllegalArgumentException {
this.IP = IP;
return (B) this;
}

Expand All @@ -89,16 +94,17 @@ abstract class CrowdControlBuilderBase<B extends CrowdControlBuilderBase<B>> imp
*
* @param IP IP to connect to
* @return this builder
* @throws IllegalArgumentException the IP was null or blank
* @throws IllegalArgumentException the IP was invalid
* @since 3.9.0
*/
@ApiStatus.AvailableSince("3.9.0")
@CheckReturnValue
@Contract("_ -> this")
public @NotNull B ip(@NotNull String IP) throws IllegalArgumentException {
ExceptionUtil.validateNotNull(IP, "IP");
if (IP.isEmpty()) {
throw new IllegalArgumentException("IP cannot be blank");
public @NotNull B ip(@Nullable String IP) throws IllegalArgumentException {
if (IP == null || IP.isEmpty()) {
logger.warn("Received null IP; ignoring");
this.IP = null;
return (B) this;
}
try {
this.IP = InetAddress.getByName(IP);
Expand Down

0 comments on commit 5ed5a39

Please sign in to comment.