ObjectId.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2020 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import Foundation
  19. import Realm
  20. /**
  21. A 12-byte (probably) unique object identifier.
  22. ObjectIds are similar to a GUID or a UUID, and can be used to uniquely identify objects without a centralized ID generator. An ObjectID consists of:
  23. 1. A 4 byte timestamp measuring the creation time of the ObjectId in seconds since the Unix epoch.
  24. 2. A 5 byte random value
  25. 3. A 3 byte counter, initialized to a random value.
  26. ObjectIds are intended to be fast to generate. Sorting by an ObjectId field will typically result in the objects being sorted in creation order.
  27. */
  28. @objc(RealmSwiftObjectId)
  29. public final class ObjectId: RLMObjectId, Decodable {
  30. // MARK: Initializers
  31. /// Creates a new zero-initialized ObjectId.
  32. public override required init() {
  33. super.init()
  34. }
  35. /// Creates a new randomly-initialized ObjectId.
  36. public override class func generate() -> ObjectId {
  37. return unsafeDowncast(super.generate(), to: ObjectId.self)
  38. }
  39. /// Creates a new ObjectId from the given 24-byte hexadecimal string.
  40. ///
  41. /// Throws if the string is not 24 characters or contains any characters other than 0-9a-fA-F.
  42. /// - Parameter string: The string to parse.
  43. public override required init(string: String) throws {
  44. try super.init(string: string)
  45. }
  46. /// Creates a new ObjectId using the given date, machine identifier, process identifier.
  47. ///
  48. /// - Parameters:
  49. /// - timestamp: A timestamp as NSDate.
  50. /// - machineId: The machine identifier.
  51. /// - processId: The process identifier.
  52. public required init(timestamp: Date, machineId: Int, processId: Int) {
  53. super.init(timestamp: timestamp,
  54. machineIdentifier: Int32(machineId),
  55. processIdentifier: Int32(processId))
  56. }
  57. /// Creates a new ObjectId from the given 24-byte hexadecimal static string.
  58. ///
  59. /// Aborts if the string is not 24 characters or contains any characters other than 0-9a-fA-F. Use the initializer which takes a String to handle invalid strings at runtime.
  60. public required init(_ str: StaticString) {
  61. try! super.init(string: str.withUTF8Buffer { String(decoding: $0, as: UTF8.self) })
  62. }
  63. /// Creates a new ObjectId by decoding from the given decoder.
  64. ///
  65. /// This initializer throws an error if reading from the decoder fails, or
  66. /// if the data read is corrupted or otherwise invalid.
  67. ///
  68. /// - Parameter decoder: The decoder to read data from.
  69. public required init(from decoder: Decoder) throws {
  70. let container = try decoder.singleValueContainer()
  71. try super.init(string: container.decode(String.self))
  72. }
  73. }
  74. extension ObjectId: Encodable {
  75. /// Encodes this ObjectId into the given encoder.
  76. ///
  77. /// This function throws an error if the given encoder is unable to encode a string.
  78. ///
  79. /// - Parameter encoder: The encoder to write data to.
  80. public func encode(to encoder: Encoder) throws {
  81. try self.stringValue.encode(to: encoder)
  82. }
  83. }
  84. extension ObjectId: Comparable {
  85. /// Returns a Boolean value indicating whether the value of the first
  86. /// argument is less than that of the second argument.
  87. ///
  88. /// - Parameters:
  89. /// - lhs: An ObjectId value to compare.
  90. /// - rhs: Another ObjectId value to compare.
  91. public static func < (lhs: ObjectId, rhs: ObjectId) -> Bool {
  92. lhs.isLessThan(rhs)
  93. }
  94. /// Returns a Boolean value indicating whether the ObjectId of the first
  95. /// argument is less than or equal to that of the second argument.
  96. ///
  97. /// - Parameters:
  98. /// - lhs: An ObjectId value to compare.
  99. /// - rhs: Another ObjectId value to compare.
  100. public static func <= (lhs: ObjectId, rhs: ObjectId) -> Bool {
  101. lhs.isLessThanOrEqual(to: rhs)
  102. }
  103. /// Returns a Boolean value indicating whether the ObjectId of the first
  104. /// argument is greater than or equal to that of the second argument.
  105. ///
  106. /// - Parameters:
  107. /// - lhs: An ObjectId value to compare.
  108. /// - rhs: Another ObjectId value to compare.
  109. public static func >= (lhs: ObjectId, rhs: ObjectId) -> Bool {
  110. lhs.isGreaterThanOrEqual(to: rhs)
  111. }
  112. /// Returns a Boolean value indicating whether the ObjectId of the first
  113. /// argument is greater than that of the second argument.
  114. ///
  115. /// - Parameters:
  116. /// - lhs: An ObjectId value to compare.
  117. /// - rhs: Another ObjectId value to compare.
  118. public static func > (lhs: ObjectId, rhs: ObjectId) -> Bool {
  119. lhs.isGreaterThan(rhs)
  120. }
  121. }