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.
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.
|
ObjectId(int timestamp,
int machineIdentifier,
short processIdentifier,
int counter)
Creates an ObjectId using the given time, machine identifier, process identifier, and counter.
|
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.
|
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() |
static boolean |
isValid(java.lang.String hexString)
Checks if a string could be an
ObjectId . |
void |
putToByteBuffer(java.nio.ByteBuffer buffer)
Convert to bytes and put those bytes to the provided ByteBuffer.
|
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() |
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 zeropublic 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 12public 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 valuespublic 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()