/bson/src/main/org/bson/BsonTimestamp.java

https://github.com/foursquare/mongo-java-driver · Java · 116 lines · 61 code · 16 blank · 39 comment · 13 complexity · 2c7a7d45a5e45987d032c98db07a5167 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2015 MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.bson;
  17. /**
  18. * A value representing the BSON timestamp type.
  19. *
  20. * @since 3.0
  21. */
  22. public final class BsonTimestamp extends BsonValue implements Comparable<BsonTimestamp> {
  23. private final int seconds;
  24. private final int inc;
  25. /**
  26. * Construct a new instance with a null time and a 0 increment.
  27. */
  28. public BsonTimestamp() {
  29. seconds = 0;
  30. inc = 0;
  31. }
  32. /**
  33. * Construct a new instance for the given time and increment.
  34. *
  35. * @param seconds the number of seconds since the epoch
  36. * @param inc the increment.
  37. */
  38. public BsonTimestamp(final int seconds, final int inc) {
  39. this.seconds = seconds;
  40. this.inc = inc;
  41. }
  42. @Override
  43. public BsonType getBsonType() {
  44. return BsonType.TIMESTAMP;
  45. }
  46. /**
  47. * Gets the time in seconds since epoch.
  48. *
  49. * @return an int representing time in seconds since epoch
  50. */
  51. public int getTime() {
  52. return seconds;
  53. }
  54. /**
  55. * Gets the increment value.
  56. *
  57. * @return an incrementing ordinal for operations within a given second
  58. */
  59. public int getInc() {
  60. return inc;
  61. }
  62. @Override
  63. public String toString() {
  64. return "Timestamp{"
  65. + "seconds=" + seconds
  66. + ", inc=" + inc
  67. + '}';
  68. }
  69. @Override
  70. public int compareTo(final BsonTimestamp ts) {
  71. if (getTime() != ts.getTime()) {
  72. return getTime() - ts.getTime();
  73. } else {
  74. return getInc() - ts.getInc();
  75. }
  76. }
  77. @Override
  78. public boolean equals(final Object o) {
  79. if (this == o) {
  80. return true;
  81. }
  82. if (o == null || getClass() != o.getClass()) {
  83. return false;
  84. }
  85. BsonTimestamp timestamp = (BsonTimestamp) o;
  86. if (seconds != timestamp.seconds) {
  87. return false;
  88. }
  89. if (inc != timestamp.inc) {
  90. return false;
  91. }
  92. return true;
  93. }
  94. @Override
  95. public int hashCode() {
  96. int result = seconds;
  97. result = 31 * result + inc;
  98. return result;
  99. }
  100. }