public final class ObjectId extends java.lang.Object implements java.lang.Comparable<ObjectId>, java.io.Serializable
A globally unique identifier for objects.
Consists of 12 bytes, divided as follows:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
time | machine | pid | inc |
Instances of this class are immutable.
Modifier and Type | Field and Description |
---|---|
private int |
counter |
private static char[] |
HEX_CHARS |
(package private) static Logger |
LOGGER |
private static int |
LOW_ORDER_THREE_BYTES |
private static int |
MACHINE_IDENTIFIER |
private int |
machineIdentifier |
private static java.util.concurrent.atomic.AtomicInteger |
NEXT_COUNTER |
private static short |
PROCESS_IDENTIFIER |
private short |
processIdentifier |
private static long |
serialVersionUID |
private int |
timestamp |
Modifier | Constructor and Description |
---|---|
|
ObjectId()
Create a new object id.
|
|
ObjectId(byte[] bytes)
Constructs a new instance from the given byte array
|
|
ObjectId(java.nio.ByteBuffer buffer)
Constructs a new instance from the given ByteBuffer
|
|
ObjectId(java.util.Date date)
Constructs a new instance using the given date.
|
|
ObjectId(java.util.Date date,
int counter)
Constructs a new instances using the given date and counter.
|
|
ObjectId(java.util.Date date,
int machineIdentifier,
short processIdentifier,
int counter)
Constructs a new instances using the given date, machine identifier, process identifier, and counter.
|
(package private) |
ObjectId(int timestamp,
int machineAndProcessIdentifier,
int counter)
Creates an ObjectId
|
|
ObjectId(int timestamp,
int machineIdentifier,
short processIdentifier,
int counter)
Creates an ObjectId using the given time, machine identifier, process identifier, and counter.
|
private |
ObjectId(int timestamp,
int machineIdentifier,
short processIdentifier,
int counter,
boolean checkCounter) |
|
ObjectId(java.lang.String hexString)
Constructs a new instance from a 24-byte hexadecimal string representation.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(ObjectId other) |
static ObjectId |
createFromLegacyFormat(int time,
int machine,
int inc)
Creates an ObjectId using time, machine and inc values.
|
private static int |
createMachineIdentifier() |
private static short |
createProcessIdentifier() |
private static int |
dateToTimestampSeconds(java.util.Date time) |
boolean |
equals(java.lang.Object o) |
static ObjectId |
get()
Gets a new object id.
|
int |
getCounter()
Gets the counter.
|
static int |
getCurrentCounter()
Gets the current value of the auto-incrementing counter.
|
java.util.Date |
getDate()
Gets the timestamp as a
Date instance. |
static int |
getGeneratedMachineIdentifier()
Gets the generated machine identifier.
|
static int |
getGeneratedProcessIdentifier()
Gets the generated process identifier.
|
int |
getMachineIdentifier()
Gets the machine identifier.
|
short |
getProcessIdentifier()
Gets the process identifier.
|
long |
getTime()
Deprecated.
Use #getDate instead
|
int |
getTimeSecond()
Deprecated.
Use #getTimestamp instead
|
int |
getTimestamp()
Gets the timestamp (number of seconds since the Unix epoch).
|
int |
hashCode() |
private static byte |
int0(int x) |
private static byte |
int1(int x) |
private static byte |
int2(int x) |
private static byte |
int3(int x) |
static boolean |
isValid(java.lang.String hexString)
Checks if a string could be an
ObjectId . |
private static byte[] |
legacyToBytes(int timestamp,
int machineAndProcessIdentifier,
int counter) |
private static int |
makeInt(byte b3,
byte b2,
byte b1,
byte b0) |
private static byte[] |
parseHexString(java.lang.String s) |
void |
putToByteBuffer(java.nio.ByteBuffer buffer)
Convert to bytes and put those bytes to the provided ByteBuffer.
|
private static byte |
short0(short x) |
private static byte |
short1(short x) |
byte[] |
toByteArray()
Convert to a byte array.
|
java.lang.String |
toHexString()
Converts this instance into a 24-byte hexadecimal string representation.
|
java.lang.String |
toString() |
java.lang.String |
toStringMongod()
Deprecated.
use
toHexString() |
private static final long serialVersionUID
static final Logger LOGGER
private static final int LOW_ORDER_THREE_BYTES
private static final int MACHINE_IDENTIFIER
private static final short PROCESS_IDENTIFIER
private static final java.util.concurrent.atomic.AtomicInteger NEXT_COUNTER
private static final char[] HEX_CHARS
private final int timestamp
private final int machineIdentifier
private final short processIdentifier
private final int counter
public ObjectId()
public ObjectId(java.util.Date date)
date
- the datepublic ObjectId(java.util.Date date, int counter)
date
- the datecounter
- the counterjava.lang.IllegalArgumentException
- if the high order byte of counter is not zeropublic ObjectId(java.util.Date date, int machineIdentifier, short processIdentifier, int counter)
date
- the datemachineIdentifier
- the machine identifierprocessIdentifier
- the process identifiercounter
- the counterjava.lang.IllegalArgumentException
- if the high order byte of machineIdentifier or counter is not zeropublic ObjectId(int timestamp, int machineIdentifier, short processIdentifier, int counter)
timestamp
- the time in secondsmachineIdentifier
- the machine identifierprocessIdentifier
- the process identifiercounter
- the counterjava.lang.IllegalArgumentException
- if the high order byte of machineIdentifier or counter is not zeroprivate ObjectId(int timestamp, int machineIdentifier, short processIdentifier, int counter, boolean checkCounter)
public ObjectId(java.lang.String hexString)
hexString
- the string to convertjava.lang.IllegalArgumentException
- if the string is not a valid hex string representation of an ObjectIdpublic ObjectId(byte[] bytes)
bytes
- the byte arrayjava.lang.IllegalArgumentException
- if array is null or not of length 12ObjectId(int timestamp, int machineAndProcessIdentifier, int counter)
timestamp
- time in secondsmachineAndProcessIdentifier
- machine and process identifiercounter
- incremental valuepublic ObjectId(java.nio.ByteBuffer buffer)
buffer
- the ByteBufferjava.lang.IllegalArgumentException
- if the buffer is null or does not have at least 12 bytes remainingpublic static ObjectId get()
public static boolean isValid(java.lang.String hexString)
ObjectId
.hexString
- a potential ObjectId as a String.java.lang.IllegalArgumentException
- if hexString is nullpublic static int getGeneratedMachineIdentifier()
public static int getGeneratedProcessIdentifier()
public static int getCurrentCounter()
public static ObjectId createFromLegacyFormat(int time, int machine, int inc)
Creates an ObjectId using time, machine and inc values. The Java driver used to create all ObjectIds this way, but it does not match the ObjectId specification, which requires four values, not three. This major release of the Java driver conforms to the specification, but still supports clients that are relying on the behavior of the previous major release by providing this explicit factory method that takes three parameters instead of four.
Ordinary users of the driver will not need this method. It's only for those that have written there own BSON decoders.
NOTE: This will not break any application that use ObjectIds. The 12-byte representation will be round-trippable from old to new driver releases.
time
- time in secondsmachine
- machine IDinc
- incremental valueObjectId
created from the given valuesprivate static byte[] legacyToBytes(int timestamp, int machineAndProcessIdentifier, int counter)
public byte[] toByteArray()
public void putToByteBuffer(java.nio.ByteBuffer buffer)
buffer
- the ByteBufferjava.lang.IllegalArgumentException
- if the buffer is null or does not have at least 12 bytes remainingpublic int getTimestamp()
public int getMachineIdentifier()
public short getProcessIdentifier()
public int getCounter()
public java.util.Date getDate()
Date
instance.public java.lang.String toHexString()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public int compareTo(ObjectId other)
compareTo
in interface java.lang.Comparable<ObjectId>
public java.lang.String toString()
toString
in class java.lang.Object
@Deprecated public int getTimeSecond()
@Deprecated public long getTime()
@Deprecated public java.lang.String toStringMongod()
toHexString()
toHexString()
private static int createMachineIdentifier()
private static short createProcessIdentifier()
private static byte[] parseHexString(java.lang.String s)
private static int dateToTimestampSeconds(java.util.Date time)
private static int makeInt(byte b3, byte b2, byte b1, byte b0)
private static byte int3(int x)
private static byte int2(int x)
private static byte int1(int x)
private static byte int0(int x)
private static byte short1(short x)
private static byte short0(short x)