Class ObjectId
-
- org.bson.types.ObjectId
-
- All Implemented Interfaces:
,
<ObjectId>
public final class ObjectId extends implements <
A globally unique identifier for objects.
Consists of 12 bytes, divided as follows:
ObjectID layout 0 1 2 3 4 5 6 7 8 9 10 11 time random value inc Instances of this class are immutable.
- See Also:
- Serialized Form
- MongoDB documentation
-
-
Constructor Summary
Constructors Constructor Description ObjectId()
Create a new object id.ObjectId(byte[] bytes)
Constructs a new instance from the given byte arrayObjectId(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.UseObjectId(int, int)
insteadhexString)
Constructs a new instance from a 24-byte hexadecimal string representation.buffer)
Constructs a new instance from the given ByteBufferdate)
Constructs a new instance using the given date.date, int counter)
Constructs a new instances using the given date and counter.date, int machineIdentifier, short processIdentifier, int counter)
Deprecated.UseObjectId(Date, int)
instead
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
compareTo(ObjectId other)
static ObjectId
createFromLegacyFormat(int time, int machine, int inc)
Deprecated.UseObjectId(int, int)
insteadboolean
o)
static ObjectId
get()
Gets a new object id.int
getCounter()
Deprecated.static int
getCurrentCounter()
Deprecated.getDate()
Gets the timestamp as aDate
instance.static int
getGeneratedMachineIdentifier()
Deprecated.static int
getGeneratedProcessIdentifier()
Deprecated.int
getMachineIdentifier()
Deprecated.short
getProcessIdentifier()
Deprecated.long
getTime()
Deprecated.Use #getDate insteadint
getTimeSecond()
Deprecated.Use #getTimestamp insteadint
getTimestamp()
Gets the timestamp (number of seconds since the Unix epoch).int
hashCode()
static boolean
hexString)
Checks if a string could be anObjectId
.void
buffer)
Convert to bytes and put those bytes to the provided ByteBuffer.byte[]
toByteArray()
Convert to a byte array.toHexString()
Converts this instance into a 24-byte hexadecimal string representation.toString()
toStringMongod()
Deprecated.usetoHexString()
-
-
-
Constructor Detail
-
ObjectId
public ObjectId()
Create a new object id.
-
ObjectId
public ObjectId( date)
Constructs a new instance using the given date.- Parameters:
date
- the date
-
ObjectId
public ObjectId( date, int counter)
Constructs a new instances using the given date and counter.- Parameters:
date
- the datecounter
- the counter- Throws:
- if the high order byte of counter is not zero
-
ObjectId
public ObjectId( date, int machineIdentifier, short processIdentifier, int counter)
Deprecated.UseObjectId(Date, int)
insteadConstructs a new instances using the given date, machine identifier, process identifier, and counter.- Parameters:
date
- the datemachineIdentifier
- the machine identifierprocessIdentifier
- the process identifiercounter
- the counter- Throws:
- if the high order byte of machineIdentifier or counter is not zero
-
ObjectId
public ObjectId(int timestamp, int machineIdentifier, short processIdentifier, int counter)
Deprecated.UseObjectId(int, int)
insteadCreates an ObjectId using the given time, machine identifier, process identifier, and counter.- Parameters:
timestamp
- the time in secondsmachineIdentifier
- the machine identifierprocessIdentifier
- the process identifiercounter
- the counter- Throws:
- if the high order byte of machineIdentifier or counter is not zero
-
ObjectId
public ObjectId(int timestamp, int counter)
Creates an ObjectId using the given time, machine identifier, process identifier, and counter.- Parameters:
timestamp
- the time in secondscounter
- the counter- Throws:
- if the high order byte of counter is not zero
-
ObjectId
public ObjectId( hexString)
Constructs a new instance from a 24-byte hexadecimal string representation.- Parameters:
hexString
- the string to convert- Throws:
- if the string is not a valid hex string representation of an ObjectId
-
ObjectId
public ObjectId(byte[] bytes)
Constructs a new instance from the given byte array- Parameters:
bytes
- the byte array- Throws:
- if array is null or not of length 12
-
ObjectId
public ObjectId( buffer)
Constructs a new instance from the given ByteBuffer- Parameters:
buffer
- the ByteBuffer- Throws:
- if the buffer is null or does not have at least 12 bytes remaining
- Since:
- 3.4
-
-
Method Detail
-
get
public static ObjectId get()
Gets a new object id.- Returns:
- the new id
-
isValid
public static boolean isValid( hexString)
Checks if a string could be anObjectId
.- Parameters:
hexString
- a potential ObjectId as a String.- Returns:
- whether the string could be an object id
- Throws:
- if hexString is null
-
toByteArray
public byte[] toByteArray()
Convert to a byte array. Note that the numbers are stored in big-endian order.- Returns:
- the byte array
-
putToByteBuffer
public void putToByteBuffer( buffer)
Convert to bytes and put those bytes to the provided ByteBuffer. Note that the numbers are stored in big-endian order.- Parameters:
buffer
- the ByteBuffer- Throws:
- if the buffer is null or does not have at least 12 bytes remaining
- Since:
- 3.4
-
getTimestamp
public int getTimestamp()
Gets the timestamp (number of seconds since the Unix epoch).- Returns:
- the timestamp
-
getDate
public getDate()
Gets the timestamp as aDate
instance.- Returns:
- the Date
-
toHexString
public toHexString()
Converts this instance into a 24-byte hexadecimal string representation.- Returns:
- a string representation of the ObjectId in hexadecimal format
-
equals
public boolean equals( o)
- Overrides:
in class
-
hashCode
public int hashCode()
- Overrides:
in class
-
toString
public toString()
- Overrides:
in class
-
createFromLegacyFormat
public static ObjectId createFromLegacyFormat(int time, int machine, int inc)
Deprecated.UseObjectId(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 , 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.
- Parameters:
time
- time in secondsmachine
- machine IDinc
- incremental value- Returns:
- a new
ObjectId
created from the given values - Since:
- 2.12.0
-
getCurrentCounter
public static int getCurrentCounter()
Deprecated.Gets the current value of the auto-incrementing counter.- Returns:
- the current counter value.
-
getGeneratedMachineIdentifier
public static int getGeneratedMachineIdentifier()
Deprecated.Gets the generated machine identifier.- Returns:
- an int representing the machine identifier
-
getGeneratedProcessIdentifier
public static int getGeneratedProcessIdentifier()
Deprecated.Gets the generated process identifier.- Returns:
- the process id
-
getMachineIdentifier
public int getMachineIdentifier()
Deprecated.Gets the machine identifier.- Returns:
- the machine identifier
-
getProcessIdentifier
public short getProcessIdentifier()
Deprecated.Gets the process identifier.- Returns:
- the process identifier
-
getCounter
public int getCounter()
Deprecated.Gets the counter.- Returns:
- the counter
-
getTimeSecond
public int getTimeSecond()
Deprecated.Use #getTimestamp insteadGets the time of this ID, in seconds.- Returns:
- the time component of this ID in seconds
-
getTime
public long getTime()
Deprecated.Use #getDate insteadGets the time of this instance, in milliseconds.- Returns:
- the time component of this ID in milliseconds
-
toStringMongod
public toStringMongod()
Deprecated.usetoHexString()
- Returns:
- a string representation of the ObjectId in hexadecimal format
- See Also:
toHexString()
-
-