diff --git a/src/main/java/com/github/f4b6a3/uuid/codec/UuidCodec.java b/src/main/java/com/github/f4b6a3/uuid/codec/UuidCodec.java index 892c6544..ed249a8f 100644 --- a/src/main/java/com/github/f4b6a3/uuid/codec/UuidCodec.java +++ b/src/main/java/com/github/f4b6a3/uuid/codec/UuidCodec.java @@ -49,7 +49,7 @@ public interface UuidCodec { * @return a generic type * @throws InvalidUuidException if the argument is invalid */ - public T encode(UUID uuid); + T encode(UUID uuid); /** * Get a UUID from a generic type. @@ -58,5 +58,5 @@ public interface UuidCodec { * @return a UUID * @throws InvalidUuidException if the argument is invalid */ - public UUID decode(T type); + UUID decode(T type); } \ No newline at end of file diff --git a/src/main/java/com/github/f4b6a3/uuid/codec/base/BaseNCodec.java b/src/main/java/com/github/f4b6a3/uuid/codec/base/BaseNCodec.java index e2e265db..7f43db08 100644 --- a/src/main/java/com/github/f4b6a3/uuid/codec/base/BaseNCodec.java +++ b/src/main/java/com/github/f4b6a3/uuid/codec/base/BaseNCodec.java @@ -71,7 +71,7 @@ public abstract class BaseNCodec implements UuidCodec { * } */ @FunctionalInterface - public static interface CustomDivider { + public interface CustomDivider { /** * Divide a number by x. * @@ -80,7 +80,7 @@ public static interface CustomDivider { * @param x the divisor * @return a pair of longs */ - public long[] divide(long x); + long[] divide(long x); } /** diff --git a/src/main/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactory.java b/src/main/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactory.java index 3c7ae502..aa6139f4 100644 --- a/src/main/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactory.java +++ b/src/main/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactory.java @@ -170,14 +170,14 @@ public B withRandomFunction(LongSupplier randomFunction) { /** * Interface for random generator. */ - protected static interface IRandom { + protected interface IRandom { /** * Return a random number. * * @return a number */ - public long nextLong(); + long nextLong(); /** * Return a random array of bytes. @@ -185,7 +185,7 @@ protected static interface IRandom { * @param length the length * @return an array */ - public byte[] nextBytes(int length); + byte[] nextBytes(int length); } /** diff --git a/src/main/java/com/github/f4b6a3/uuid/factory/function/ClockSeqFunction.java b/src/main/java/com/github/f4b6a3/uuid/factory/function/ClockSeqFunction.java index 1e6158dd..039269c2 100644 --- a/src/main/java/com/github/f4b6a3/uuid/factory/function/ClockSeqFunction.java +++ b/src/main/java/com/github/f4b6a3/uuid/factory/function/ClockSeqFunction.java @@ -50,7 +50,7 @@ public interface ClockSeqFunction extends LongUnaryOperator { * * @return a number in the range 0 to 16383 (2^14-1) */ - public static long getRandom() { + static long getRandom() { return toExpectedRange(RandomUtil.newSecureRandom().nextLong()); } @@ -63,7 +63,7 @@ public static long getRandom() { * @param clockseq a clock sequence * @return a number in the range 0 to 16383 (2^14-1). */ - public static long toExpectedRange(final long clockseq) { + static long toExpectedRange(final long clockseq) { return clockseq & 0x0000000000003fffL; } } diff --git a/src/main/java/com/github/f4b6a3/uuid/factory/function/NodeIdFunction.java b/src/main/java/com/github/f4b6a3/uuid/factory/function/NodeIdFunction.java index 728f8dc2..e5c60eef 100644 --- a/src/main/java/com/github/f4b6a3/uuid/factory/function/NodeIdFunction.java +++ b/src/main/java/com/github/f4b6a3/uuid/factory/function/NodeIdFunction.java @@ -47,7 +47,7 @@ public interface NodeIdFunction extends LongSupplier { * * @return a number in the range 0 to 2^48-1. */ - public static long getRandom() { + static long getRandom() { return toExpectedRange(RandomUtil.newSecureRandom().nextLong()); } @@ -56,7 +56,7 @@ public static long getRandom() { * * @return a number in the range 0 to 2^48-1. */ - public static long getMulticastRandom() { + static long getMulticastRandom() { return toMulticast(getRandom()); } @@ -69,7 +69,7 @@ public static long getMulticastRandom() { * @param nodeid the node identifier * @return a number in the range 0 to 2^48-1. */ - public static long toExpectedRange(final long nodeid) { + static long toExpectedRange(final long nodeid) { return nodeid & 0x0000_ffffffffffffL; } @@ -82,7 +82,7 @@ public static long toExpectedRange(final long nodeid) { * @param nodeid the node identifier * @return a node identifier with the multicast bit set */ - public static long toMulticast(long nodeid) { + static long toMulticast(long nodeid) { return (nodeid & 0x0000_ffffffffffffL) | 0x0000_010000000000L; } @@ -92,7 +92,7 @@ public static long toMulticast(long nodeid) { * @param nodeid a node identifier * @return true if the node identifier is multicast */ - public static boolean isMulticast(long nodeid) { + static boolean isMulticast(long nodeid) { return (nodeid & 0x0000_010000000000L) == 0x0000_010000000000L; } } diff --git a/src/main/java/com/github/f4b6a3/uuid/factory/function/TimeFunction.java b/src/main/java/com/github/f4b6a3/uuid/factory/function/TimeFunction.java index 412b3aa1..300996c7 100644 --- a/src/main/java/com/github/f4b6a3/uuid/factory/function/TimeFunction.java +++ b/src/main/java/com/github/f4b6a3/uuid/factory/function/TimeFunction.java @@ -60,7 +60,7 @@ public interface TimeFunction extends LongSupplier { * @param instant an instant * @return a number of 100-nanoseconds since 1970-01-01 (Unix epoch) */ - public static long toUnixTimestamp(final Instant instant) { + static long toUnixTimestamp(final Instant instant) { return UuidTime.toUnixTimestamp(instant); } @@ -73,7 +73,7 @@ public static long toUnixTimestamp(final Instant instant) { * @param timestamp a number of 100-nanoseconds since 1970-01-01 (Unix epoch) * @return a number in the range 0 to 2^60-1. */ - public static long toExpectedRange(final long timestamp) { + static long toExpectedRange(final long timestamp) { return timestamp & 0x0_fffffffffffffffL; } } diff --git a/src/main/java/com/github/f4b6a3/uuid/factory/function/impl/MacNodeIdFunction.java b/src/main/java/com/github/f4b6a3/uuid/factory/function/impl/MacNodeIdFunction.java index 5a9d7878..41af0952 100644 --- a/src/main/java/com/github/f4b6a3/uuid/factory/function/impl/MacNodeIdFunction.java +++ b/src/main/java/com/github/f4b6a3/uuid/factory/function/impl/MacNodeIdFunction.java @@ -65,7 +65,7 @@ private long getHardwareAddress() { return toNumber(nic.getHardwareAddress()); } } catch (SocketException e) { - // do nothing + return NodeIdFunction.getMulticastRandom(); } return NodeIdFunction.getMulticastRandom(); diff --git a/src/main/java/com/github/f4b6a3/uuid/factory/standard/DceSecurityFactory.java b/src/main/java/com/github/f4b6a3/uuid/factory/standard/DceSecurityFactory.java index c98fefe4..55d01893 100644 --- a/src/main/java/com/github/f4b6a3/uuid/factory/standard/DceSecurityFactory.java +++ b/src/main/java/com/github/f4b6a3/uuid/factory/standard/DceSecurityFactory.java @@ -55,7 +55,7 @@ public DceSecurityFactory() { private DceSecurityFactory(Builder builder) { super(UuidVersion.VERSION_DCE_SECURITY, builder); - this.localDomain = builder != null ? builder.localDomain : 0; + this.localDomain = builder.localDomain; this.counter = new AtomicInteger(); } diff --git a/src/main/java/com/github/f4b6a3/uuid/util/internal/JavaVersionUtil.java b/src/main/java/com/github/f4b6a3/uuid/util/internal/JavaVersionUtil.java index 3e08bb0e..057682ad 100644 --- a/src/main/java/com/github/f4b6a3/uuid/util/internal/JavaVersionUtil.java +++ b/src/main/java/com/github/f4b6a3/uuid/util/internal/JavaVersionUtil.java @@ -48,7 +48,7 @@ public static int getJavaVersion() { return 8; } - } catch (NullPointerException | NumberFormatException | IndexOutOfBoundsException e) { + } catch (NumberFormatException | IndexOutOfBoundsException e) { return 8; } } diff --git a/src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java b/src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java index 855d377c..7aaf63ee 100644 --- a/src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java +++ b/src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java @@ -80,8 +80,8 @@ public static synchronized String hostname() { if (hostname != null && !hostname.isEmpty()) { return hostname; } - } catch (UnknownHostException | NullPointerException e) { - // do nothing + } catch (UnknownHostException e) { + return null; } // not found @@ -113,7 +113,7 @@ public static synchronized String mac(NetworkInterface nic) { return mac; } } catch (SocketException | NullPointerException e) { - // do nothing + return null; } // not found @@ -134,16 +134,12 @@ public static synchronized String ip(NetworkInterface nic) { return ip; } - try { - if (nic != null) { - Enumeration ips = nic.getInetAddresses(); - if (ips.hasMoreElements()) { - ip = ips.nextElement().getHostAddress(); - return ip; - } + if (nic != null) { + Enumeration ips = nic.getInetAddresses(); + if (ips.hasMoreElements()) { + ip = ips.nextElement().getHostAddress(); + return ip; } - } catch (NullPointerException e) { - // do nothing } // not found @@ -210,8 +206,8 @@ public static synchronized NetworkInterface nic() { } } - } catch (UnknownHostException | SocketException | NullPointerException e) { - // do nothing + } catch (UnknownHostException | SocketException e) { + return null; } // NIC not found @@ -232,8 +228,8 @@ private static synchronized boolean acceptable(NetworkInterface nic) { return true; } } - } catch (SocketException | NullPointerException e) { - // do nothing + } catch (SocketException e) { + return false; } return false; diff --git a/src/main/java/com/github/f4b6a3/uuid/util/internal/RandomUtil.java b/src/main/java/com/github/f4b6a3/uuid/util/internal/RandomUtil.java index 23a41da1..45cedd82 100644 --- a/src/main/java/com/github/f4b6a3/uuid/util/internal/RandomUtil.java +++ b/src/main/java/com/github/f4b6a3/uuid/util/internal/RandomUtil.java @@ -121,6 +121,7 @@ public static SecureRandom newSecureRandom() { private static class SecureRandomPool { + private static final Random random = new Random(); private static final int POOL_SIZE = processors(); private static final Random[] POOL = new Random[POOL_SIZE]; private static final ReentrantLock lock = new ReentrantLock(); @@ -140,7 +141,7 @@ public static byte[] nextBytes(final int length) { // every now and then if (bytes.length > 0 && bytes[0x00] == 0) { // delete a random item from the pool - delete((new Random()).nextInt(POOL_SIZE)); + delete(random.nextInt(POOL_SIZE)); } return bytes;