update : add space after ()
This commit is contained in:
parent
a5d0a15419
commit
8f1a091a43
|
@ -12,15 +12,15 @@ public class UUIDPositiveLongGenerator {
|
||||||
return Math.abs(UUID.randomUUID().getLeastSignificantBits());
|
return Math.abs(UUID.randomUUID().getLeastSignificantBits());
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getMostSignificantBits(){
|
public long getMostSignificantBits() {
|
||||||
return Math.abs(UUID.randomUUID().getMostSignificantBits());
|
return Math.abs(UUID.randomUUID().getMostSignificantBits());
|
||||||
}
|
}
|
||||||
|
|
||||||
public long gethashCode(){
|
public long gethashCode() {
|
||||||
return Math.abs(UUID.randomUUID().toString().hashCode());
|
return Math.abs(UUID.randomUUID().toString().hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public long combineByteBuffer(){
|
public long combineByteBuffer() {
|
||||||
UUID uuid = UUID.randomUUID();
|
UUID uuid = UUID.randomUUID();
|
||||||
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
|
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
|
||||||
bb.putLong(uuid.getMostSignificantBits());
|
bb.putLong(uuid.getMostSignificantBits());
|
||||||
|
@ -29,20 +29,20 @@ public class UUIDPositiveLongGenerator {
|
||||||
return Math.abs(bb.getLong());
|
return Math.abs(bb.getLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
public long combineBitwise(){
|
public long combineBitwise() {
|
||||||
UUID uniqueUUID;
|
UUID uniqueUUID;
|
||||||
uniqueUUID = UUID.randomUUID();
|
uniqueUUID = UUID.randomUUID();
|
||||||
return Math.abs((uniqueUUID.getMostSignificantBits() << 32) | (uniqueUUID.getLeastSignificantBits() & 0xFFFFFFFFL));
|
return Math.abs((uniqueUUID.getMostSignificantBits() << 32) | (uniqueUUID.getLeastSignificantBits() & 0xFFFFFFFFL));
|
||||||
}
|
}
|
||||||
|
|
||||||
public long combineDirect(){
|
public long combineDirect() {
|
||||||
UUID uniqueUUID = UUID.randomUUID();
|
UUID uniqueUUID = UUID.randomUUID();
|
||||||
long mostSignificantBits = uniqueUUID.getMostSignificantBits();
|
long mostSignificantBits = uniqueUUID.getMostSignificantBits();
|
||||||
long leastSignificantBits = uniqueUUID.getLeastSignificantBits();
|
long leastSignificantBits = uniqueUUID.getLeastSignificantBits();
|
||||||
return Math.abs(mostSignificantBits ^ (leastSignificantBits >> 1));
|
return Math.abs(mostSignificantBits ^ (leastSignificantBits >> 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
public long combinePermutation(){
|
public long combinePermutation() {
|
||||||
UUID uuid = UUID.randomUUID();
|
UUID uuid = UUID.randomUUID();
|
||||||
long mostSigBits = uuid.getMostSignificantBits();
|
long mostSigBits = uuid.getMostSignificantBits();
|
||||||
long leastSigBits = uuid.getLeastSignificantBits();
|
long leastSigBits = uuid.getLeastSignificantBits();
|
||||||
|
|
|
@ -4,7 +4,6 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -13,7 +12,7 @@ import java.util.Set;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class UUIDPositiveLongGeneratorUnitTest {
|
public class UUIDPositiveLongGeneratorUnitTest {
|
||||||
private final static int n = 1000000;
|
private final static int N = 1000000;
|
||||||
private final static double COLLISION_THRESHOLD = 0.001;
|
private final static double COLLISION_THRESHOLD = 0.001;
|
||||||
private final UUIDPositiveLongGenerator uuidLongGenerator = new UUIDPositiveLongGenerator();
|
private final UUIDPositiveLongGenerator uuidLongGenerator = new UUIDPositiveLongGenerator();
|
||||||
private final Logger logger = LoggerFactory.getLogger(UUIDPositiveLongGeneratorUnitTest.class);
|
private final Logger logger = LoggerFactory.getLogger(UUIDPositiveLongGeneratorUnitTest.class);
|
||||||
|
@ -41,14 +40,14 @@ public class UUIDPositiveLongGeneratorUnitTest {
|
||||||
private void collisionCheck(Method method) throws Exception {
|
private void collisionCheck(Method method) throws Exception {
|
||||||
Set<Long> uniqueValues = new HashSet<>();
|
Set<Long> uniqueValues = new HashSet<>();
|
||||||
int collisions = 0;
|
int collisions = 0;
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < N; i++) {
|
||||||
long uniqueValue = (long) method.invoke(uuidLongGenerator);
|
long uniqueValue = (long) method.invoke(uuidLongGenerator);
|
||||||
assertThat(uniqueValue).isPositive();
|
assertThat(uniqueValue).isPositive();
|
||||||
if (!uniqueValues.add(uniqueValue)) {
|
if (!uniqueValues.add(uniqueValue)) {
|
||||||
collisions++;
|
collisions++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
double collisionsProbability = (double) collisions / n;
|
double collisionsProbability = (double) collisions / N;
|
||||||
printOutput(method.getName(), collisions, collisionsProbability);
|
printOutput(method.getName(), collisions, collisionsProbability);
|
||||||
assertThat(collisionsProbability).isLessThan(COLLISION_THRESHOLD);
|
assertThat(collisionsProbability).isLessThan(COLLISION_THRESHOLD);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue