Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
Remove code flaws detected by PMD.

PMD Rules:

* UnnecessaryModifier
* DCN_NULLPOINTER_EXCEPTION
* DMI_RANDOM_USED_ONLY_ONCE
* EmptyCatchBlock
* RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE
  • Loading branch information
fabiolimace committed Jul 7, 2024
1 parent 02d6c28 commit 84fd314
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/github/f4b6a3/uuid/codec/UuidCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface UuidCodec<T> {
* @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.
Expand All @@ -58,5 +58,5 @@ public interface UuidCodec<T> {
* @return a UUID
* @throws InvalidUuidException if the argument is invalid
*/
public UUID decode(T type);
UUID decode(T type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public abstract class BaseNCodec implements UuidCodec<String> {
* }</pre>
*/
@FunctionalInterface
public static interface CustomDivider {
public interface CustomDivider {
/**
* Divide a number by x.
*
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,22 @@ 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.
*
* @param length the length
* @return an array
*/
public byte[] nextBytes(int length);
byte[] nextBytes(int length);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private long getHardwareAddress() {
return toNumber(nic.getHardwareAddress());
}
} catch (SocketException e) {
// do nothing
return NodeIdFunction.getMulticastRandom();
}

return NodeIdFunction.getMulticastRandom();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static int getJavaVersion() {
return 8;
}

} catch (NullPointerException | NumberFormatException | IndexOutOfBoundsException e) {
} catch (NumberFormatException | IndexOutOfBoundsException e) {
return 8;
}
}
Expand Down
28 changes: 12 additions & 16 deletions src/main/java/com/github/f4b6a3/uuid/util/internal/NetworkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,7 +113,7 @@ public static synchronized String mac(NetworkInterface nic) {
return mac;
}
} catch (SocketException | NullPointerException e) {
// do nothing
return null;
}

// not found
Expand All @@ -134,16 +134,12 @@ public static synchronized String ip(NetworkInterface nic) {
return ip;
}

try {
if (nic != null) {
Enumeration<InetAddress> ips = nic.getInetAddresses();
if (ips.hasMoreElements()) {
ip = ips.nextElement().getHostAddress();
return ip;
}
if (nic != null) {
Enumeration<InetAddress> ips = nic.getInetAddresses();
if (ips.hasMoreElements()) {
ip = ips.nextElement().getHostAddress();
return ip;
}
} catch (NullPointerException e) {
// do nothing
}

// not found
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down

0 comments on commit 84fd314

Please sign in to comment.