File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 27
27
* <b>time</b> is seconds since epoch
28
28
* <b>inc<b> is an ordinal
29
29
*/
30
- public class BSONTimestamp implements Serializable {
30
+ public class BSONTimestamp implements Comparable < BSONTimestamp >, Serializable {
31
31
32
32
private static final long serialVersionUID = -3268482672267936464L ;
33
33
@@ -60,6 +60,16 @@ public String toString(){
60
60
return "TS time:" + _time + " inc:" + _inc ;
61
61
}
62
62
63
+ @ Override
64
+ public int compareTo (BSONTimestamp ts ) {
65
+ if (getTime () != ts .getTime ()) {
66
+ return getTime () - ts .getTime ();
67
+ }
68
+ else {
69
+ return getInc () - ts .getInc ();
70
+ }
71
+ }
72
+
63
73
@ Override
64
74
public boolean equals (Object obj ) {
65
75
if (obj == this )
@@ -73,4 +83,5 @@ public boolean equals(Object obj) {
73
83
74
84
final int _inc ;
75
85
final Date _time ;
86
+
76
87
}
Original file line number Diff line number Diff line change
1
+ package org .bson ;
2
+
3
+ import org .bson .types .BSONTimestamp ;
4
+ import org .testng .Assert ;
5
+ import org .testng .annotations .Test ;
6
+
7
+ public class BSONTimestampTest extends Assert {
8
+
9
+ @ Test
10
+ public void testComparable (){
11
+
12
+ int currTime = (int )(System .currentTimeMillis () / 1000 );
13
+
14
+ BSONTimestamp t1 = new BSONTimestamp (currTime , 1 );
15
+ BSONTimestamp t2 = new BSONTimestamp (currTime , 1 );
16
+
17
+ assertEquals (0 , t1 .compareTo (t2 ));
18
+
19
+ t2 = new BSONTimestamp (currTime , 2 );
20
+
21
+ assertTrue (t1 .compareTo (t2 ) < 0 );
22
+ assertTrue (t2 .compareTo (t1 ) > 0 );
23
+
24
+ t2 = new BSONTimestamp (currTime + 1 , 1 );
25
+
26
+ assertTrue (t1 .compareTo (t2 ) < 0 );
27
+ assertTrue (t2 .compareTo (t1 ) > 0 );
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments