public 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[] b)
Constructs an ObjectId given its 12-byte binary representation.
|
ObjectId(Date time)
Constructs a new instance using the given date.
|
ObjectId(Date time,
int inc)
Constructs a new instances using the given date and counter.
|
ObjectId(Date time,
int machine,
int inc)
Deprecated.
ObjectId 's constructed this way do not conform to the
ObjectId specification. Please
use ObjectId(byte[]) or createFromLegacyFormat(int, int, int) instead. |
ObjectId(int time,
int machine,
int inc)
Deprecated.
ObjectId 's constructed this way do not conform to
the ObjectId specification.
Please use ObjectId(byte[]) or
createFromLegacyFormat(int, int, int) instead. |
ObjectId(String s)
Creates a new instance from a string.
|
ObjectId(String s,
boolean babble)
Deprecated.
'babble' format is deprecated. Please use
ObjectId(String) instead. |
Modifier and Type | Method and Description |
---|---|
static int |
_flip(int x)
Deprecated.
This method is NOT a part of public API and will be dropped in 3.x versions.
|
int |
_inc()
Deprecated.
Please use
toByteArray() instead. |
int |
_machine()
Deprecated.
Please use
toByteArray() instead. |
int |
_time()
Deprecated.
Please use
getTimestamp() ()} instead. |
static String |
babbleToMongod(String b)
Deprecated.
This method is NOT a part of public API and will be dropped in 3.x versions.
|
int |
compareTo(ObjectId id) |
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.
|
static int |
getCurrentCounter()
Gets the current value of the auto-incrementing counter.
|
static int |
getCurrentInc()
Deprecated.
Please use
getCurrentCounter() instead. |
Date |
getDate()
Gets the timestamp as a
Date instance. |
static int |
getGenMachineId()
Deprecated.
|
int |
getInc()
Deprecated.
Please use the
toByteArray() instead. |
int |
getMachine()
Deprecated.
Please use
#toByteArray() instead. |
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() |
boolean |
isNew()
Deprecated.
'new' flag breaks the immutability of the
ObjectId class
and will be dropped in 3.x versions of the driver |
static boolean |
isValid(String s)
Checks if a string could be an
ObjectId . |
static ObjectId |
massageToObjectId(Object o)
Deprecated.
This method is NOT a part of public API and will be dropped in 3.x versions.
|
void |
notNew()
Deprecated.
'new' flag breaks the immutability of the
ObjectId class
and will be dropped in 3.x versions of the driver |
byte[] |
toByteArray()
Convert to a byte array.
|
String |
toHexString()
Converts this instance into a 24-byte hexadecimal string representation.
|
String |
toString() |
String |
toStringBabble()
Deprecated.
'babble' format is deprecated and will be removed in 3.0. Please use
toHexString() instead. |
String |
toStringMongod()
Deprecated.
use
toHexString() |
public ObjectId(Date time)
time
- the datepublic ObjectId(Date time, int inc)
time
- the dateinc
- the counterIllegalArgumentException
- if the high order byte of counter is not zero@Deprecated public ObjectId(Date time, int machine, int inc)
ObjectId
's constructed this way do not conform to the
ObjectId specification. Please
use ObjectId(byte[])
or createFromLegacyFormat(int, int, int)
instead.time
- the datemachine
- the machine identifierinc
- the countercreateFromLegacyFormat(int, int, int)
public ObjectId(String s)
s
- the string to convertIllegalArgumentException
- if the string is not a valid id@Deprecated public ObjectId(String s, boolean babble)
ObjectId(String)
instead.ObjectId
from a string.s
- the string representation of ObjectId. Can contains only [0-9]|[a-f]|[A-F] characters.babble
- if true
- convert to 'babble' objectId formatpublic ObjectId(byte[] b)
b
- a byte array of length 12@Deprecated public ObjectId(int time, int machine, int inc)
ObjectId
's constructed this way do not conform to
the ObjectId specification.
Please use ObjectId(byte[])
or
createFromLegacyFormat(int, int, int)
instead.time
- time in secondsmachine
- machine IDinc
- incremental valuecreateFromLegacyFormat(int, int, int)
public ObjectId()
public static ObjectId get()
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 static boolean isValid(String s)
ObjectId
.s
- a potential ObjectId as a String.IllegalArgumentException
- if hexString is null@Deprecated public static ObjectId massageToObjectId(Object o)
ObjectId
, if possible. Strings will be converted into ObjectId
s, if possible, and
ObjectId
s will be cast and returned. Passing in null
returns null
.o
- the object to convertObjectId
if it can be massaged, null otherwise@Deprecated public String toStringBabble()
toHexString()
instead.public String toHexString()
@Deprecated public String toStringMongod()
toHexString()
toHexString()
public byte[] toByteArray()
@Deprecated public static String babbleToMongod(String b)
public int compareTo(ObjectId id)
compareTo
in interface Comparable<ObjectId>
public int getTimestamp()
public Date getDate()
Date
instance.@Deprecated public long getTime()
@Deprecated public int getTimeSecond()
@Deprecated public int getInc()
toByteArray()
instead.@Deprecated public int _time()
getTimestamp()
()} instead.@Deprecated public int getMachine()
#toByteArray()
instead.createFromLegacyFormat(int, int, int)
@Deprecated public int _machine()
toByteArray()
instead.createFromLegacyFormat(int, int, int)
@Deprecated public int _inc()
toByteArray()
instead.createFromLegacyFormat(int, int, int)
@Deprecated public boolean isNew()
ObjectId
class
and will be dropped in 3.x versions of the driver@Deprecated public void notNew()
ObjectId
class
and will be dropped in 3.x versions of the driver@Deprecated public static int getGenMachineId()
createFromLegacyFormat(int, int, int)
public static int getCurrentCounter()
@Deprecated public static int getCurrentInc()
getCurrentCounter()
instead.@Deprecated public static int _flip(int x)
Copyright © 2023. All rights reserved.