Skip to content

Commit 7130715

Browse files
committed
feat: V7 add Precision12 for higher precision
1 parent 58849f9 commit 7130715

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

  • src/main/java/io/github/honhimw/uuid/gen

src/main/java/io/github/honhimw/uuid/gen/V7.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,15 @@ public Instant now() {
176176
}
177177

178178
public ClockSequenceV7 withAdditionalPrecision() {
179-
this.precision = new Precision(usableBits(), 12);
179+
this.precision = new Precision12(usableBits());
180+
return this;
181+
}
182+
183+
public ClockSequenceV7 withAdditionalPrecision(int bits) {
184+
if (bits < 0 || bits > 20) {
185+
throw new IllegalArgumentException("Timestamp addition precision out of range [0..20].");
186+
}
187+
this.precision = new Precision(usableBits(), bits);
180188
return this;
181189
}
182190

@@ -219,19 +227,19 @@ private boolean advance(long seconds, int nanos) {
219227
}
220228

221229
private static class Precision {
222-
private final int bits;
223-
private final int factor;
224-
private final long mask;
225-
private final long shift;
230+
final int bits;
231+
final int factor;
232+
final long mask;
233+
final long shift;
226234

227-
private Precision(int usableBits, int bits) {
235+
Precision(int usableBits, int bits) {
228236
this.bits = bits;
229237
this.factor = (int) (999_999 / (bits >= 1 ? (2L << (bits - 1)) : 1)) + 1;
230238
this.mask = -1L >>> (Long.SIZE - usableBits + bits);
231239
this.shift = usableBits - bits;
232240
}
233241

234-
private long apply(long value, ReseedingTimestamp timestamp) {
242+
long apply(long value, ReseedingTimestamp timestamp) {
235243
if (this.bits == 0) {
236244
return value;
237245
}
@@ -241,4 +249,20 @@ private long apply(long value, ReseedingTimestamp timestamp) {
241249

242250
}
243251

252+
private static class Precision12 extends Precision {
253+
Precision12(int usableBits) {
254+
super(usableBits, 12);
255+
}
256+
257+
@Override
258+
long apply(long value, ReseedingTimestamp timestamp) {
259+
long addition = timestamp.nanos % 1_000_000 * 2000 / 488_281;
260+
if (addition > 4081) {
261+
System.out.println(addition);
262+
}
263+
return value & this.mask | (addition << this.shift);
264+
}
265+
266+
}
267+
244268
}

0 commit comments

Comments
 (0)