Skip to content

Commit

Permalink
feat: InetUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
qixils committed Jan 6, 2024
1 parent c72d4e1 commit 2eefc59
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pojos/src/main/java/dev/qixils/crowdcontrol/util/InetUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.qixils.crowdcontrol.util;

import org.jetbrains.annotations.NotNull;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* Utilities for {@link InetAddress}.
*/
public class InetUtils {

/**
* Safely gets the {@code localhost} address.
*
* @return localhost
*/
@NotNull
public static InetAddress getLocalHost() {
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
return InetAddress.getLoopbackAddress();
}
}

/**
* Wrapper for {@link InetAddress#getByName(String)} which throws an unchecked exception.
*
* @param ip IP address
* @return inet address
*/
@NotNull
public static InetAddress getByName(@NotNull String ip) throws IllegalArgumentException {
try {
return InetAddress.getByName(ip);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Invalid IP address " + ip, e);
}
}
}

0 comments on commit 2eefc59

Please sign in to comment.