From b55db89b5fb10e1fb4d678505f86fe54a82b355c Mon Sep 17 00:00:00 2001 From: "@hangga" Date: Wed, 1 Nov 2023 10:37:14 +0700 Subject: [PATCH] update --- ...or.java => UUIDPositiveLongGenerator.java} | 22 ++++--- .../uuid/UUIDLongGeneratorUnitTest.java | 63 ------------------- .../UUIDPositiveLongGeneratorUnitTest.java | 60 ++++++++++++++++++ 3 files changed, 73 insertions(+), 72 deletions(-) rename core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/{UUIDLongGenerator.java => UUIDPositiveLongGenerator.java} (68%) delete mode 100644 core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDLongGeneratorUnitTest.java create mode 100644 core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java diff --git a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDLongGenerator.java b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDPositiveLongGenerator.java similarity index 68% rename from core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDLongGenerator.java rename to core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDPositiveLongGenerator.java index 8ac71b1ce6..bc8177837c 100644 --- a/core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDLongGenerator.java +++ b/core-java-modules/core-java-uuid/src/main/java/com/baeldung/uuid/UUIDPositiveLongGenerator.java @@ -3,17 +3,21 @@ package com.baeldung.uuid; import java.nio.ByteBuffer; import java.util.UUID; -public class UUIDLongGenerator { +/** + * Methods are called by reflection in the unit test + */ +@SuppressWarnings("unused") +public class UUIDPositiveLongGenerator { public long getLeastSignificantBits(){ - return UUID.randomUUID().getLeastSignificantBits(); + return Math.abs(UUID.randomUUID().getLeastSignificantBits()); } public long getMostSignificantBits(){ - return UUID.randomUUID().getMostSignificantBits(); + return Math.abs(UUID.randomUUID().getMostSignificantBits()); } public long gethashCode(){ - return UUID.randomUUID().toString().hashCode(); + return Math.abs(UUID.randomUUID().toString().hashCode()); } public long combineByteBuffer(){ @@ -21,21 +25,21 @@ public class UUIDLongGenerator { ByteBuffer bb = ByteBuffer.wrap(new byte[16]); bb.putLong(uuid.getMostSignificantBits()); bb.putLong(uuid.getLeastSignificantBits()); - bb.rewind(); // Kembalikan posisi buffer ke awal - return bb.getLong(); + bb.rewind(); + return Math.abs(bb.getLong()); } public long combineBitwise(){ UUID uniqueUUID; uniqueUUID = UUID.randomUUID(); - return (uniqueUUID.getMostSignificantBits() << 32) | (uniqueUUID.getLeastSignificantBits() & 0xFFFFFFFFL); + return Math.abs((uniqueUUID.getMostSignificantBits() << 32) | (uniqueUUID.getLeastSignificantBits() & 0xFFFFFFFFL)); } public long combineDirect(){ UUID uniqueUUID = UUID.randomUUID(); long mostSignificantBits = uniqueUUID.getMostSignificantBits(); long leastSignificantBits = uniqueUUID.getLeastSignificantBits(); - return mostSignificantBits ^ (leastSignificantBits >> 1); + return Math.abs(mostSignificantBits ^ (leastSignificantBits >> 1)); } public long combinePermutation(){ @@ -53,6 +57,6 @@ public class UUIDLongGenerator { for (byte b : uuidBytes) { result = (result << 8) | (b & 0xFF); } - return result; + return Math.abs(result); } } diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDLongGeneratorUnitTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDLongGeneratorUnitTest.java deleted file mode 100644 index 24add0a14a..0000000000 --- a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDLongGeneratorUnitTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.uuid; - -import org.junit.jupiter.api.Test; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.text.DecimalFormat; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.IntStream; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class UUIDLongGeneratorUnitTest { - final static int n = 1000000; - UUIDLongGenerator uuidLongGenerator = new UUIDLongGenerator(); - - @Test - void whenForeachGenerateLongValue_thenCollisionsCheck() { - printTableHeader(); - for (Method method : uuidLongGenerator.getClass().getDeclaredMethods()) { - collisionAndNegativeCheck(method); - } - } - - private void printTableHeader() { - System.out.format("%-30s %-15s %-15s %-15s %-15s%n", "Approach", "collisions", "negatives", "collision", "negative"); - System.out.format("%-30s %-15s %-15s %-15s %-15s%n", "(method name)", "count", "count", "probability", "probability"); - System.out.println("--------------------------------------------------------------------------------------------"); - } - - private void printOutput(String method, int collisionsCount, int negativeCount, double collisionsProbability, double negativeProbability) { - DecimalFormat decimalFormat = new DecimalFormat("#.#####"); - System.out.format("%-30s %-15s %-15s %-15s %-15s%n", method, collisionsCount, negativeCount, decimalFormat.format(collisionsProbability), decimalFormat.format(negativeProbability)); - } - - private void collisionAndNegativeCheck(Method method) { - Set uniqueValues = new HashSet<>(); - AtomicInteger collisions = new AtomicInteger(0); - AtomicInteger negative = new AtomicInteger(0); - - IntStream.range(0, n).forEach(i -> { - try { - long uniqueValue = (long) method.invoke(uuidLongGenerator); - if (!uniqueValues.add(uniqueValue)) { - collisions.incrementAndGet(); - } - if (uniqueValue < 0) { - negative.incrementAndGet(); - } - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } - }); - - double collisionsProbability = (double) collisions.get() / n; - double negativeProbability = (double) negative.get() / n; - printOutput(method.getName(), collisions.get(), negative.get(), collisionsProbability, negativeProbability); - - assertTrue(collisionsProbability <= 0.001); // threshold = 0.001 - } -} diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java new file mode 100644 index 0000000000..2df49b20fd --- /dev/null +++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java @@ -0,0 +1,60 @@ +package com.baeldung.uuid; + +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.text.DecimalFormat; +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UUIDPositiveLongGeneratorUnitTest { + private final static int n = 1000000; + private final UUIDPositiveLongGenerator uuidLongGenerator = new UUIDPositiveLongGenerator(); + private final Logger logger = LoggerFactory.getLogger(UUIDPositiveLongGeneratorUnitTest.class); + + @Test + void whenForeachGenerateLongValue_thenCollisionsCheck() throws InvocationTargetException, IllegalAccessException { + printTableHeader(); + for (Method method : uuidLongGenerator.getClass().getDeclaredMethods()) { + collisionAndNegativeCheck(method); + } + } + + + private void printTableHeader() { + logger.info(String.format("%-30s %-15s %-15s %-15s %-15s", "Approach", "collisions", "negatives", "collision", "negative")); + logger.info(String.format("%-30s %-15s %-15s %-15s %-15s", "(method name)", "count", "count", "probability", "probability")); + logger.info("--------------------------------------------------------------------------------------------"); + } + + private void printOutput(String method, int collisionsCount, int negativeCount, double collisionsProbability, double negativeProbability) { + DecimalFormat decimalFormat = new DecimalFormat("#.#####"); + //logger.info(String.format("%-30s %-15s %-15s %-15s %-15s", method, collisionsCount, negativeCount, decimalFormat.format(collisionsProbability), decimalFormat.format(negativeProbability))); + logger.info("%-30s{} %-15s{} %-15s{} %-15s{} %-15s{}", method, collisionsCount, negativeCount, decimalFormat.format(collisionsProbability), decimalFormat.format(negativeProbability)); + } + + + private void collisionAndNegativeCheck(Method method) throws InvocationTargetException, IllegalAccessException { + Set uniqueValues = new HashSet<>(); + int collisions = 0; + int negative = 0; + for (int i = 0; i < n; i++) { + long uniqueValue = (long) method.invoke(uuidLongGenerator); + if (!uniqueValues.add(uniqueValue)) { + collisions++; + } + if (uniqueValue < 0) { + negative++; + } + } + double collisionsProbability = (double) collisions / n; + double negativeProbability = (double) negative / n; + printOutput(method.getName(), collisions, negative, collisionsProbability, negativeProbability); + assertThat(collisionsProbability).isLessThan(0.001); + } +}