Permalink
Browse files

more compatible id JAVA-5

  • Loading branch information...
1 parent 65dbf5a commit fee8e5c5d8ce4562727dbe30fd6107b282a3f5de @erh erh committed Jun 3, 2009
Showing with 122 additions and 26 deletions.
  1. +69 −26 src/main/com/mongodb/ObjectId.java
  2. +52 −0 src/test/com/mongodb/ObjectIdTest.java
  3. +1 −0 testng.xml
@@ -19,7 +19,7 @@
package com.mongodb;
import java.util.*;
-import java.nio.ByteBuffer;
+import java.nio.*;
import com.mongodb.util.*;
@@ -98,36 +98,48 @@ public static ObjectId massageToObjectId( Object o ){
* @throws IllegalArgumentException if the string is not a valid id
*/
public ObjectId( String s ){
+ this( s , false );
+ }
+
+ public ObjectId( String s , boolean babble ){
if ( ! isValid( s ) )
throw new IllegalArgumentException( "invalid ObjectId [" + s + "]" );
- String baseString = s.substring( 0 , 16 );
- String incString = s.substring( 16 );
-
- ByteBuffer buf = ByteBuffer.allocate(24);
-
- for (int i=0; i < baseString.length() / 2; i++) {
- buf.put((byte) Integer.parseInt(baseString.substring(i*2, i*2 + 2), 16));
+ if ( babble ){
+ String baseString = s.substring( 0 , 16 );
+ String incString = s.substring( 16 );
+
+ ByteBuffer buf = ByteBuffer.allocate(24);
+
+ for (int i=0; i < baseString.length() / 2; i++) {
+ buf.put((byte) Integer.parseInt(baseString.substring(i*2, i*2 + 2), 16));
+ }
+
+ buf.flip();
+
+ _base = buf.getLong();
+
+ buf.clear();
+
+ for (int i=0; i < incString.length() / 2; i++) {
+ buf.put((byte) Integer.parseInt(incString.substring(i*2, i*2 + 2), 16));
+ }
+
+ buf.flip();
+
+ _inc = buf.getInt();
}
-
- buf.flip();
-
- _base = buf.getLong();
-
- buf.clear();
-
- for (int i=0; i < incString.length() / 2; i++) {
- buf.put((byte) Integer.parseInt(incString.substring(i*2, i*2 + 2), 16));
+ else {
+ byte b[] = new byte[12];
+ for ( int i=0; i<b.length; i++ ){
+ b[b.length-(i+1)] = (byte)Integer.parseInt( s.substring( i*2 , i*2 + 2) , 16 );
+ }
+ ByteBuffer bb = ByteBuffer.wrap( b );
+
+ _inc = bb.getInt();
+ _base = bb.getLong();
}
-
- buf.flip();
-
- _inc = buf.getInt();
-
-// _base = Long.parseLong( baseString , 16 );
-// _inc = Integer.parseInt( incString , 16 );
-
_new = false;
}
@@ -176,7 +188,7 @@ public boolean equals( Object o ){
_inc == other._inc;
}
- public String toString(){
+ public String toStringBabble(){
String a = Long.toHexString( _base );
String b = Integer.toHexString( _inc );
@@ -193,6 +205,29 @@ public String toString(){
return buf.toString();
}
+ public String toStringMongod(){
+ byte b[] = new byte[12];
+ ByteBuffer bb = ByteBuffer.wrap( b );
+ bb.putInt( _inc );
+ bb.putLong( _base );
+
+ StringBuilder buf = new StringBuilder(24);
+
+ for ( int i=b.length-1; i>=0; i-- ){
+ int x = b[i] & 0xFF;
+ String s = Integer.toHexString( x );
+ if ( s.length() == 1 )
+ buf.append( "0" );
+ buf.append( s );
+ }
+
+ return buf.toString();
+ }
+
+ public String toString(){
+ return toStringMongod();
+ }
+
public int compareTo( ObjectId id ){
if ( id == null )
return -1;
@@ -214,6 +249,14 @@ public int compareTo( ObjectId id ){
return 0;
}
+ public long getBase(){
+ return _base;
+ }
+
+ public int getInc(){
+ return _inc;
+ }
+
final long _base;
final int _inc;
@@ -0,0 +1,52 @@
+// ObjectIdTest.java
+
+/**
+ * Copyright (C) 2008 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.mongodb;
+
+import org.testng.annotations.Test;
+
+import com.mongodb.util.*;
+
+public class ObjectIdTest extends TestCase {
+
+ @Test(groups = {"basic"})
+ public void testTSM(){
+
+ ObjectId a = new ObjectId( 2667563522304714314L , -1912742877 );
+ assertEquals( "4a26c3e2e316052523dcfd8d" , a.toStringMongod() );
+ assertEquals( "250516e3e2c3264a8dfddc23" , a.toStringBabble() );
+ assertEquals( "4a26c3e2e316052523dcfd8d" , a.toString() );
+ }
+
+ @Test(groups = {"basic"})
+ public void testRT1(){
+ ObjectId a = new ObjectId();
+ assertEquals( a.toStringBabble() , (new ObjectId( a.toStringBabble() , true ) ).toStringBabble() );
+ assertEquals( a.toStringMongod() , (new ObjectId( a.toStringMongod() , false ) ).toStringMongod() );
+ assertEquals( a.toStringMongod() , (new ObjectId( a.toStringMongod() ) ).toStringMongod() );
+ assertEquals( a.toString() , (new ObjectId( a.toString() , false ) ).toString() );
+ }
+
+
+
+ public static void main( String args[] )
+ throws Exception {
+ (new ObjectIdTest()).runConsole();
+ }
+
+}
View
@@ -12,6 +12,7 @@
<class name="com.mongodb.ReflectionTest" />
<class name="com.mongodb.DBAddressTest" />
<class name="com.mongodb.DBObjectTest" />
+ <class name="com.mongodb.ObjectIdTest" />
<class name="com.mongodb.ErrorTest" />

0 comments on commit fee8e5c

Please sign in to comment.