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 | random value | 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)
Deprecated.
Use
ObjectId(Date, int) instead |
ObjectId(int timestamp,
int counter)
Creates an ObjectId using the given time, machine identifier, process identifier, and counter.
|
ObjectId(int timestamp,
int machineIdentifier,
short processIdentifier,
int counter)
Deprecated.
Use
ObjectId(int, int) instead |
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)
Deprecated.
Use
ObjectId(int, int) instead |
boolean |
equals(java.lang.Object o) |
static ObjectId |
get()
Gets a new object id.
|
int |
getCounter()
Deprecated.
|
static int |
getCurrentCounter()
Deprecated.
|
java.util.Date |
getDate()
Gets the timestamp as a
Date instance. |
static int |
getGeneratedMachineIdentifier()
Deprecated.
|
static int |
getGeneratedProcessIdentifier()
Deprecated.
|
int |
getMachineIdentifier()
Deprecated.
|
short |
getProcessIdentifier()
Deprecated.
|
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 zero@Deprecated public ObjectId(java.util.Date date, int machineIdentifier, short processIdentifier, int counter)
ObjectId(Date, int)
insteaddate
- the datemachineIdentifier
- the machine identifierprocessIdentifier
- the process identifiercounter
- the counterjava.lang.IllegalArgumentException
- if the high order byte of machineIdentifier or counter is not zero@Deprecated public ObjectId(int timestamp, int machineIdentifier, short processIdentifier, int counter)
ObjectId(int, int)
insteadtimestamp
- 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(int timestamp, int counter)
timestamp
- the time in secondscounter
- the counterjava.lang.IllegalArgumentException
- if the high order byte of 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 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 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 static ObjectId createFromLegacyFormat(int time, int machine, int inc)
ObjectId(int, int)
insteadCreates 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 values@Deprecated public static int getCurrentCounter()
@Deprecated public static int getGeneratedMachineIdentifier()
@Deprecated public static int getGeneratedProcessIdentifier()
@Deprecated public int getMachineIdentifier()
@Deprecated public short getProcessIdentifier()
@Deprecated public int getCounter()
@Deprecated public int getTimeSecond()
@Deprecated public long getTime()
@Deprecated public java.lang.String toStringMongod()
toHexString()
toHexString()
Copyright © 2010 - 2023 Adobe. All Rights Reserved