Skip to content

Commit c602fe1

Browse files
Added support for Iterable (List/Collection/Set) from just List.
Removed unused variables, and ignored checked exceptions.
1 parent b9591e1 commit c602fe1

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

src/main/org/bson/BSONEncoder.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
package org.bson;
44

5-
import org.bson.io.*;
6-
import org.bson.types.*;
75
import static org.bson.BSON.*;
86

7+
import java.nio.*;
8+
import java.nio.charset.*;
99
import java.util.*;
10-
import java.util.regex.*;
1110
import java.util.concurrent.atomic.*;
11+
import java.util.regex.*;
1212

13-
import java.nio.*;
14-
import java.nio.charset.*;
13+
import org.bson.io.*;
14+
import org.bson.types.*;
1515

1616
/**
1717
* this is meant to be pooled or cached
1818
* there is some per instance memory for string conversion, etc...
1919
*/
20+
@SuppressWarnings("unchecked")
2021
public class BSONEncoder {
2122

2223
static final boolean DEBUG = false;
@@ -124,7 +125,7 @@ int putObject( String name , BSONObject o ){
124125
return _buf.getPosition() - start;
125126
}
126127

127-
protected void _putObjectField( String name , Object val ){
128+
protected void _putObjectField( String name , Object val ){
128129

129130
if ( name.equals( "_transientFields" ) )
130131
return;
@@ -157,14 +158,14 @@ else if ( val instanceof Pattern )
157158
putPattern(name, (Pattern)val );
158159
else if ( val instanceof Map )
159160
putMap( name , (Map)val );
160-
else if ( val instanceof List )
161-
putList( name , (List)val );
161+
else if ( val instanceof Iterable)
162+
putIterable( name , (Iterable)val );
162163
else if ( val instanceof byte[] )
163164
putBinary( name , (byte[])val );
164165
else if ( val instanceof Binary )
165166
putBinary( name , (Binary)val );
166167
else if ( val.getClass().isArray() )
167-
putList( name , Arrays.asList( (Object[])val ) );
168+
putIterable( name , Arrays.asList( (Object[])val ) );
168169

169170
else if (val instanceof Symbol) {
170171
putSymbol(name, (Symbol) val);
@@ -184,13 +185,17 @@ else if ( putSpecial( name , val ) ){
184185

185186
}
186187

187-
private void putList( String name , List l ){
188+
private void putIterable( String name , Iterable l ){
188189
_put( ARRAY , name );
189190
final int sizePos = _buf.getPosition();
190191
_buf.writeInt( 0 );
191192

192-
for ( int i=0; i<l.size(); i++ )
193-
_putObjectField( String.valueOf( i ) , l.get( i ) );
193+
int i=0;
194+
for ( Object obj: l ) {
195+
_putObjectField( String.valueOf( i ) , obj );
196+
i++;
197+
}
198+
194199

195200
_buf.write( EOO );
196201
_buf.writeInt( sizePos , _buf.getPosition() - sizePos );
@@ -233,13 +238,11 @@ protected void putCodeWScope( String name , CodeWScope code ){
233238
}
234239

235240
protected void putBoolean( String name , Boolean b ){
236-
int start = _buf.getPosition();
237241
_put( BOOLEAN , name );
238242
_buf.write( b ? (byte)0x1 : (byte)0x0 );
239243
}
240244

241245
protected void putDate( String name , Date d ){
242-
int start = _buf.getPosition();
243246
_put( DATE , name );
244247
_buf.writeLong( d.getTime() );
245248
}

0 commit comments

Comments
 (0)