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 2727 * <b>time</b> is seconds since epoch
2828 * <b>inc<b> is an ordinal
2929 */
30- public class BSONTimestamp implements Serializable {
30+ public class BSONTimestamp implements Comparable < BSONTimestamp >, Serializable {
3131
3232 private static final long serialVersionUID = -3268482672267936464L ;
3333
@@ -60,6 +60,16 @@ public String toString(){
6060 return "TS time:" + _time + " inc:" + _inc ;
6161 }
6262
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+
6373 @ Override
6474 public boolean equals (Object obj ) {
6575 if (obj == this )
@@ -73,4 +83,5 @@ public boolean equals(Object obj) {
7383
7484 final int _inc ;
7585 final Date _time ;
86+
7687}
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