-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
pojos/src/main/java/dev/qixils/crowdcontrol/util/InetUtils.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,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); | ||
} | ||
} | ||
} |