类 ObjectId
- 所有已实现的接口:
Serializable,Comparable<ObjectId>
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.
- 另请参阅:
-
构造器概要
构造器构造器说明ObjectId()Create a new object id.ObjectId(byte[] bytes) Constructs a new instance from the given byte arrayObjectId(int timestamp, int machineIdentifier, int counter) Creates an ObjectId using the given time, machine identifier, and counter.Constructs a new instance from a 24-byte hexadecimal string representation.Constructs a new instance using the given date.Constructs a new instances using the given date and counter.Constructs a new instances using the given date, machine identifier, and counter. -
方法概要
修饰符和类型方法说明intstatic ObjectIdcreateFromLegacyFormat(int time, int machine, int inc) Creates an ObjectId using time, machine and inc values.booleanstatic ObjectIdget()Gets a new object id.intGets the counter.static intGets the current value of the auto-incrementing counter.getDate()Gets the timestamp as aDateinstance.static intGets the generated machine identifier.intGets the machine identifier.shortGets the process identifier.longgetTime()已过时。Use #getDate insteadint已过时。Use #getTimestamp insteadintGets the timestamp (number of seconds since the Unix epoch).inthashCode()static booleanChecks if a string could be anObjectId.byte[]Convert to a byte array.Converts this instance into a 24-byte hexadecimal string representation.toString()已过时。usetoHexString()
-
构造器详细资料
-
ObjectId
public ObjectId()Create a new object id. -
ObjectId
Constructs a new instance using the given date.- 参数:
date- the date
-
ObjectId
Constructs a new instances using the given date and counter.- 参数:
date- the datecounter- the counter- 抛出:
IllegalArgumentException- if the high order byte of counter is not zero
-
ObjectId
Constructs a new instances using the given date, machine identifier, and counter.- 参数:
date- the datemachineIdentifier- the machine identifiercounter- the counter- 抛出:
IllegalArgumentException- if the high order byte of machineIdentifier or counter is not zero
-
ObjectId
public ObjectId(int timestamp, int machineIdentifier, int counter) Creates an ObjectId using the given time, machine identifier, and counter.- 参数:
timestamp- the time in secondsmachineIdentifier- the machine identifiercounter- the counter- 抛出:
IllegalArgumentException- if the high order byte of machineIdentifier or counter is not zero
-
ObjectId
Constructs a new instance from a 24-byte hexadecimal string representation.- 参数:
hexString- the string to convert- 抛出:
IllegalArgumentException- 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- 参数:
bytes- the byte array- 抛出:
IllegalArgumentException- if array is null or not of length 12
-
-
方法详细资料
-
get
Gets a new object id.- 返回:
- the new id
-
isValid
Checks if a string could be anObjectId.- 参数:
hexString- a potential ObjectId as a String.- 返回:
- whether the string could be an object id
- 抛出:
IllegalArgumentException- if hexString is null
-
getGeneratedMachineIdentifier
public static int getGeneratedMachineIdentifier()Gets the generated machine identifier.- 返回:
- an int representing the machine identifier
-
getCurrentCounter
public static int getCurrentCounter()Gets the current value of the auto-incrementing counter.- 返回:
- the current counter value.
-
createFromLegacyFormat
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 value- 返回:
- a new
ObjectIdcreated from the given values - 从以下版本开始:
- 2.12.0
-
toByteArray
public byte[] toByteArray()Convert to a byte array. Note that the numbers are stored in big-endian order.- 返回:
- the byte array
-
getTimestamp
public int getTimestamp()Gets the timestamp (number of seconds since the Unix epoch).- 返回:
- the timestamp
-
getMachineIdentifier
public int getMachineIdentifier()Gets the machine identifier.- 返回:
- the machine identifier
-
getProcessIdentifier
public short getProcessIdentifier()Gets the process identifier.- 返回:
- the process identifier
-
getCounter
public int getCounter()Gets the counter.- 返回:
- the counter
-
getDate
Gets the timestamp as aDateinstance.- 返回:
- the Date
-
toHexString
Converts this instance into a 24-byte hexadecimal string representation.- 返回:
- a string representation of the ObjectId in hexadecimal format
-
equals
-
hashCode
public int hashCode() -
compareTo
- 指定者:
compareTo在接口中Comparable<ObjectId>
-
toString
-
getTimeSecond
已过时。Use #getTimestamp insteadGets the time of this ID, in seconds.- 返回:
- the time component of this ID in seconds
-
getTime
已过时。Use #getDate insteadGets the time of this instance, in milliseconds.- 返回:
- the time component of this ID in milliseconds
-
toStringMongod
已过时。usetoHexString()- 返回:
- a string representation of the ObjectId in hexadecimal format
- 另请参阅:
-