public final class ObjectId extends Object implements Comparable<ObjectId>, 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.
Constructor and Description |
---|
ObjectId()
Create a new object id.
|
ObjectId(byte[] bytes)
Constructs a new instance from the given byte array
|
ObjectId(ByteBuffer buffer)
Constructs a new instance from the given ByteBuffer
|
ObjectId(Date date)
Constructs a new instance using the given date.
|
ObjectId(Date date,
int counter)
Constructs a new instances using the given date and counter.
|
ObjectId(Date date,
int machineIdentifier,
short processIdentifier,
int counter)
Constructs a new instances using the given date, machine identifier, process identifier, and counter.
|
ObjectId(int timestamp,
int machineIdentifier,
short processIdentifier,
int counter)
Creates an ObjectId using the given time, machine identifier, process identifier, and counter.
|
ObjectId(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.
|
boolean |
equals(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.
|
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() |
static boolean |
isValid(String hexString)
Checks if a string could be an
ObjectId . |
void |
putToByteBuffer(ByteBuffer buffer)
Convert to bytes and put those bytes to the provided ByteBuffer.
|
byte[] |
toByteArray()
Convert to a byte array.
|
String |
toHexString()
Converts this instance into a 24-byte hexadecimal string representation.
|
String |
toString() |
String |
toStringMongod()
Deprecated.
use
toHexString() |
public ObjectId()
public ObjectId(Date date)
date
- the datepublic ObjectId(Date date, int counter)
date
- the datecounter
- the counterIllegalArgumentException
- if the high order byte of counter is not zeropublic ObjectId(Date date, int machineIdentifier, short processIdentifier, int counter)
date
- the datemachineIdentifier
- the machine identifierprocessIdentifier
- the process identifiercounter
- the counterIllegalArgumentException
- 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 counterIllegalArgumentException
- if the high order byte of machineIdentifier or counter is not zeropublic ObjectId(String hexString)
hexString
- the string to convertIllegalArgumentException
- if the string is not a valid hex string representation of an ObjectIdpublic ObjectId(byte[] bytes)
bytes
- the byte arrayIllegalArgumentException
- if array is null or not of length 12public ObjectId(ByteBuffer buffer)
buffer
- the ByteBufferIllegalArgumentException
- if the buffer is null or does not have at least 12 bytes remainingpublic static ObjectId get()
public static boolean isValid(String hexString)
ObjectId
.hexString
- a potential ObjectId as a String.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 valuespublic byte[] toByteArray()
public void putToByteBuffer(ByteBuffer buffer)
buffer
- the ByteBufferIllegalArgumentException
- 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 Date getDate()
Date
instance.public String toHexString()
public int compareTo(ObjectId other)
compareTo
in interface Comparable<ObjectId>
@Deprecated public int getTimeSecond()
@Deprecated public long getTime()
@Deprecated public String toStringMongod()
toHexString()
toHexString()