Skip to content

Commit 42c6215

Browse files
authored
Fix #57: Add UUIDUtil.maxUUID() (and nilUUID()) (#64)
1 parent 041cb79 commit 42c6215

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

release-notes/VERSION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ Project: java-uuid-generator
44
Releases
55
============================================================================
66

7-
4.1.0 (not yet released)
7+
4.1.0 (07-Jan-2023)
88

99
#41: Add support for Proposed type v6 (reordered timestamp)
1010
(contributed by Hal H)
1111
#46: Add support for Proposed type v7 (epoch-based time uuid)
1212
(contributed by Hal H)
1313
#55: Add `Main-Class` manifest to make jar invoke `Jug` class
14+
#57: Add constants for "Nil UUID", "Max UUID" (from draft "new UUID" spec) in `UUIDUtil`
1415
- Fix a minor issue with argument validation for `Jug` tool class
1516
- Update junit dependency (via oss-parent:41)
1617
- Update slf4j-api to 1.7.36

src/main/java/com/fasterxml/uuid/impl/UUIDUtil.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ public class UUIDUtil
1616
// similarly, clock sequence and variant are multiplexed
1717
public final static int BYTE_OFFSET_CLOCK_SEQUENCE = 8;
1818
public final static int BYTE_OFFSET_VARIATION = 8;
19-
19+
20+
/**
21+
* @since 4.1
22+
*/
23+
private final static UUID NIL_UUID = new UUID(0L, 0L);
24+
25+
/**
26+
* @since 4.1
27+
*/
28+
private final static UUID MAX_UUID = new UUID(-1L, -1L);
29+
2030
/*
2131
/**********************************************************************
2232
/* Construction (can instantiate, although usually not necessary)
@@ -27,6 +37,38 @@ public class UUIDUtil
2737
// via static methods
2838
public UUIDUtil() { }
2939

40+
/*
41+
/**********************************************************************
42+
/* Static UUID instances
43+
/**********************************************************************
44+
*/
45+
46+
/**
47+
* Accessor for so-call "Nil UUID" (see
48+
* <a href="https://www.rfc-editor.org/rfc/rfc4122#section-4.1.7">RFC 4122/4.1.7</a>;
49+
* one that is all zeroes.
50+
*
51+
* @since 4.1
52+
*
53+
* @return "Nil" UUID instance
54+
*/
55+
public static UUID nilUUID() {
56+
return NIL_UUID;
57+
}
58+
59+
/**
60+
* Accessor for so-call "Max UUID" (see
61+
* <a href="https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-04.html#name-max-uuid">UUID 6 draft</a>;
62+
* one that is all one bits
63+
*
64+
* @since 4.1
65+
*
66+
* @return "Nil" UUID instance
67+
*/
68+
public static UUID maxUUID() {
69+
return MAX_UUID;
70+
}
71+
3072
/*
3173
/**********************************************************************
3274
/* Factory methods

src/test/java/com/fasterxml/uuid/UUIDTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*/
3535
public class UUIDTest extends TestCase
3636
{
37-
final static UUID nullUUID = new UUID(0L, 0L);
37+
final static UUID nullUUID = UUIDUtil.nilUUID();
3838

3939
public UUIDTest(java.lang.String testName)
4040
{
@@ -51,10 +51,11 @@ public static void main(String[] args)
5151
{
5252
TestRunner.run(suite());
5353
}
54-
54+
5555
/**************************************************************************
5656
* Begin constructor tests
5757
*************************************************************************/
58+
5859
/**
5960
* Test of UUID() constructor, of class com.fasterxml.uuid.UUID.
6061
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fasterxml.uuid.impl;
2+
3+
import java.util.UUID;
4+
5+
import junit.framework.TestCase;
6+
7+
/**
8+
* Test class focusing on verifying functionality provided by
9+
* {@link UUIDUtil}.
10+
*<p>
11+
* NOTE: some of {@code UUIDUtil} testing is via main
12+
* {@link com.fasterxml.uuid.UUIDTest}.
13+
*/
14+
public class UUIDUtilTest extends TestCase
15+
{
16+
public void testNilUUID() {
17+
UUID nil = UUIDUtil.nilUUID();
18+
// Should be all zeroes:
19+
assertEquals(0L, nil.getMostSignificantBits());
20+
assertEquals(0L, nil.getLeastSignificantBits());
21+
}
22+
23+
public void testMaxUUID() {
24+
UUID max = UUIDUtil.maxUUID();
25+
// Should be all ones:
26+
assertEquals(~0, max.getMostSignificantBits());
27+
assertEquals(~0, max.getLeastSignificantBits());
28+
}
29+
}

0 commit comments

Comments
 (0)