Skip to content

Commit 246c691

Browse files
author
kristina
committed
flag support
1 parent 7eb4121 commit 246c691

2 files changed

Lines changed: 73 additions & 8 deletions

File tree

src/main/com/mongodb/Bytes.java

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.nio.*;
66
import java.nio.charset.*;
7+
import java.util.regex.Pattern;
8+
import java.util.Arrays;
79

810
/**
911
* <type><name>0<data>
@@ -106,15 +108,67 @@ public static boolean cameFromDB( DBObject o ){
106108
}
107109

108110
public static int patternFlags( String flags ){
109-
if ( flags.length() == 0 )
110-
return 0;
111-
throw new RuntimeException( "can't convert flags yet" );
111+
flags = flags.toLowerCase();
112+
Flag f[] = Flag.values();
113+
int fint = 0;
114+
int count = 0;
115+
116+
for( Flag flag : f ) {
117+
if( flags.indexOf( flag.flagChar ) >= 0 ) {
118+
fint |= flag.javaFlag;
119+
count++;
120+
if( flag.unsupported != null )
121+
_warnUnsupported( flag.unsupported );
122+
}
123+
}
124+
125+
if( count < flags.length() )
126+
throw new RuntimeException( "some flags could not be converted: "+flags );
127+
128+
return fint;
112129
}
113130

114131
public static String patternFlags( int flags ){
115-
if ( flags == 0 )
116-
return "";
117-
throw new RuntimeException( "can't convert flags yet" );
132+
Flag f[] = Flag.values();
133+
byte b[] = new byte[ f.length ];
134+
int count = 0;
135+
136+
for( Flag flag : f ) {
137+
if( ( flags & flag.javaFlag ) > 0 ) {
138+
b[ count++ ] = (byte)flag.flagChar;
139+
flags -= flag.javaFlag;
140+
}
141+
}
142+
143+
if( flags > 0 )
144+
throw new RuntimeException( "some flags could not be recognized." );
145+
146+
Arrays.sort( b );
147+
return new String( b, 0, count );
148+
}
149+
150+
private static enum Flag {
151+
CANON_EQ( Pattern.CANON_EQ, 'c', "PATTERN.CANON_EQ" ),
152+
DOTALL( Pattern.DOTALL, 'd', "Pattern.DOTALL" ),
153+
CASE_INSENSITIVE( Pattern.CASE_INSENSITIVE, 'i', null ),
154+
UNIX_LINES( Pattern.UNIX_LINES, 'l', "Pattern.UNIX_LINES" ),
155+
MULTILINE(Pattern.MULTILINE, 'm', null ),
156+
LITERAL( Pattern.LITERAL, 't', "Pattern.LITERAL" ),
157+
UNICODE_CASE( Pattern.UNICODE_CASE, 'u', "Pattern.UNICODE_CASE" ),
158+
COMMENTS( Pattern.COMMENTS, 'x', null );
159+
160+
public final int javaFlag;
161+
public final char flagChar;
162+
public final String unsupported;
163+
Flag( int f, char ch, String u ) {
164+
javaFlag = f;
165+
flagChar = ch;
166+
unsupported = u;
167+
}
168+
}
169+
170+
private static void _warnUnsupported( String flag ) {
171+
System.out.println( "flag " + flag + " not supported by db." );
118172
}
119173

120174
static final String NO_REF_HACK = "_____nodbref_____";

src/test/com/mongodb/ByteTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,24 @@ public void testPatternFlags() {
346346
threw = false;
347347

348348
try {
349-
Bytes.patternFlags( 6 );
349+
Bytes.patternFlags( 257 );
350350
}
351351
catch( RuntimeException e ) {
352352
threw = true;
353353
}
354354
assertEquals( threw, true );
355+
356+
Pattern lotsoflags = Pattern.compile( "foo", Pattern.CANON_EQ &
357+
Pattern.DOTALL &
358+
Pattern.CASE_INSENSITIVE &
359+
Pattern.UNIX_LINES &
360+
Pattern.MULTILINE &
361+
Pattern.LITERAL &
362+
Pattern.UNICODE_CASE &
363+
Pattern.COMMENTS );
364+
365+
int check = Bytes.patternFlags( Bytes.patternFlags( lotsoflags.flags() ) );
366+
assertEquals( lotsoflags.flags(), check );
355367
}
356368

357369
@Test(groups = {"basic"})
@@ -373,7 +385,6 @@ public void testPattern() {
373385

374386
encoder.done();
375387
decoder.done();
376-
377388
}
378389

379390
final DBBase _db;

0 commit comments

Comments
 (0)