Skip to content

Commit fee8e5c

Browse files
committed
more compatible id JAVA-5
1 parent 65dbf5a commit fee8e5c

File tree

3 files changed

+122
-26
lines changed

3 files changed

+122
-26
lines changed

src/main/com/mongodb/ObjectId.java

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package com.mongodb;
2020

2121
import java.util.*;
22-
import java.nio.ByteBuffer;
22+
import java.nio.*;
2323

2424
import com.mongodb.util.*;
2525

@@ -98,36 +98,48 @@ public static ObjectId massageToObjectId( Object o ){
9898
* @throws IllegalArgumentException if the string is not a valid id
9999
*/
100100
public ObjectId( String s ){
101+
this( s , false );
102+
}
103+
104+
public ObjectId( String s , boolean babble ){
101105

102106
if ( ! isValid( s ) )
103107
throw new IllegalArgumentException( "invalid ObjectId [" + s + "]" );
104108

105-
String baseString = s.substring( 0 , 16 );
106-
String incString = s.substring( 16 );
107-
108-
ByteBuffer buf = ByteBuffer.allocate(24);
109-
110-
for (int i=0; i < baseString.length() / 2; i++) {
111-
buf.put((byte) Integer.parseInt(baseString.substring(i*2, i*2 + 2), 16));
109+
if ( babble ){
110+
String baseString = s.substring( 0 , 16 );
111+
String incString = s.substring( 16 );
112+
113+
ByteBuffer buf = ByteBuffer.allocate(24);
114+
115+
for (int i=0; i < baseString.length() / 2; i++) {
116+
buf.put((byte) Integer.parseInt(baseString.substring(i*2, i*2 + 2), 16));
117+
}
118+
119+
buf.flip();
120+
121+
_base = buf.getLong();
122+
123+
buf.clear();
124+
125+
for (int i=0; i < incString.length() / 2; i++) {
126+
buf.put((byte) Integer.parseInt(incString.substring(i*2, i*2 + 2), 16));
127+
}
128+
129+
buf.flip();
130+
131+
_inc = buf.getInt();
112132
}
113-
114-
buf.flip();
115-
116-
_base = buf.getLong();
117-
118-
buf.clear();
119-
120-
for (int i=0; i < incString.length() / 2; i++) {
121-
buf.put((byte) Integer.parseInt(incString.substring(i*2, i*2 + 2), 16));
133+
else {
134+
byte b[] = new byte[12];
135+
for ( int i=0; i<b.length; i++ ){
136+
b[b.length-(i+1)] = (byte)Integer.parseInt( s.substring( i*2 , i*2 + 2) , 16 );
137+
}
138+
ByteBuffer bb = ByteBuffer.wrap( b );
139+
140+
_inc = bb.getInt();
141+
_base = bb.getLong();
122142
}
123-
124-
buf.flip();
125-
126-
_inc = buf.getInt();
127-
128-
// _base = Long.parseLong( baseString , 16 );
129-
// _inc = Integer.parseInt( incString , 16 );
130-
131143
_new = false;
132144
}
133145

@@ -176,7 +188,7 @@ public boolean equals( Object o ){
176188
_inc == other._inc;
177189
}
178190

179-
public String toString(){
191+
public String toStringBabble(){
180192
String a = Long.toHexString( _base );
181193
String b = Integer.toHexString( _inc );
182194

@@ -193,6 +205,29 @@ public String toString(){
193205
return buf.toString();
194206
}
195207

208+
public String toStringMongod(){
209+
byte b[] = new byte[12];
210+
ByteBuffer bb = ByteBuffer.wrap( b );
211+
bb.putInt( _inc );
212+
bb.putLong( _base );
213+
214+
StringBuilder buf = new StringBuilder(24);
215+
216+
for ( int i=b.length-1; i>=0; i-- ){
217+
int x = b[i] & 0xFF;
218+
String s = Integer.toHexString( x );
219+
if ( s.length() == 1 )
220+
buf.append( "0" );
221+
buf.append( s );
222+
}
223+
224+
return buf.toString();
225+
}
226+
227+
public String toString(){
228+
return toStringMongod();
229+
}
230+
196231
public int compareTo( ObjectId id ){
197232
if ( id == null )
198233
return -1;
@@ -214,6 +249,14 @@ public int compareTo( ObjectId id ){
214249
return 0;
215250
}
216251

252+
public long getBase(){
253+
return _base;
254+
}
255+
256+
public int getInc(){
257+
return _inc;
258+
}
259+
217260
final long _base;
218261
final int _inc;
219262

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ObjectIdTest.java
2+
3+
/**
4+
* Copyright (C) 2008 10gen Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.mongodb;
20+
21+
import org.testng.annotations.Test;
22+
23+
import com.mongodb.util.*;
24+
25+
public class ObjectIdTest extends TestCase {
26+
27+
@Test(groups = {"basic"})
28+
public void testTSM(){
29+
30+
ObjectId a = new ObjectId( 2667563522304714314L , -1912742877 );
31+
assertEquals( "4a26c3e2e316052523dcfd8d" , a.toStringMongod() );
32+
assertEquals( "250516e3e2c3264a8dfddc23" , a.toStringBabble() );
33+
assertEquals( "4a26c3e2e316052523dcfd8d" , a.toString() );
34+
}
35+
36+
@Test(groups = {"basic"})
37+
public void testRT1(){
38+
ObjectId a = new ObjectId();
39+
assertEquals( a.toStringBabble() , (new ObjectId( a.toStringBabble() , true ) ).toStringBabble() );
40+
assertEquals( a.toStringMongod() , (new ObjectId( a.toStringMongod() , false ) ).toStringMongod() );
41+
assertEquals( a.toStringMongod() , (new ObjectId( a.toStringMongod() ) ).toStringMongod() );
42+
assertEquals( a.toString() , (new ObjectId( a.toString() , false ) ).toString() );
43+
}
44+
45+
46+
47+
public static void main( String args[] )
48+
throws Exception {
49+
(new ObjectIdTest()).runConsole();
50+
}
51+
52+
}

testng.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<class name="com.mongodb.ReflectionTest" />
1313
<class name="com.mongodb.DBAddressTest" />
1414
<class name="com.mongodb.DBObjectTest" />
15+
<class name="com.mongodb.ObjectIdTest" />
1516

1617
<class name="com.mongodb.ErrorTest" />
1718

0 commit comments

Comments
 (0)