31
31
import org .bson .types .ObjectId ;
32
32
import org .bson .types .Symbol ;
33
33
34
+ import java .util .Arrays ;
34
35
import java .util .Collections ;
35
36
import java .util .Date ;
36
- import java .util .HashMap ;
37
37
import java .util .List ;
38
38
import java .util .Map ;
39
- import java .util .Set ;
40
39
41
40
/**
42
41
* <p>A map from a BSON types to the Class to which it should be decoded. This class is useful if, for example,
71
70
*/
72
71
public class BsonTypeClassMap {
73
72
static final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap ();
74
- private final Map < BsonType , Class <?>> map = new HashMap <>() ;
73
+ private final Class <?>[] bsonTypeOrdinalToClassMap = new Class <?>[ 256 ] ;
75
74
76
75
/**
77
76
* Construct an instance with the default mapping, but replacing the default mapping with any values contained in the given map.
@@ -81,7 +80,7 @@ public class BsonTypeClassMap {
81
80
*/
82
81
public BsonTypeClassMap (final Map <BsonType , Class <?>> replacementsForDefaults ) {
83
82
addDefaults ();
84
- map . putAll ( replacementsForDefaults );
83
+ replacementsForDefaults . forEach (( key , value ) -> bsonTypeOrdinalToClassMap [ key . getValue ()] = value );
85
84
}
86
85
87
86
/**
@@ -91,41 +90,37 @@ public BsonTypeClassMap() {
91
90
this (Collections .emptyMap ());
92
91
}
93
92
94
- Set <BsonType > keys () {
95
- return map .keySet ();
96
- }
97
-
98
93
/**
99
94
* Gets the Class that is mapped to the given BSON type.
100
95
*
101
96
* @param bsonType the BSON type
102
97
* @return the Class that is mapped to the BSON type
103
98
*/
104
99
public Class <?> get (final BsonType bsonType ) {
105
- return map . get ( bsonType ) ;
100
+ return bsonTypeOrdinalToClassMap [ bsonType . getValue ()] ;
106
101
}
107
102
108
103
private void addDefaults () {
109
- map . put ( BsonType .ARRAY , List .class ) ;
110
- map . put ( BsonType .BINARY , Binary .class ) ;
111
- map . put ( BsonType .BOOLEAN , Boolean .class ) ;
112
- map . put ( BsonType .DATE_TIME , Date .class ) ;
113
- map . put ( BsonType .DB_POINTER , BsonDbPointer .class ) ;
114
- map . put ( BsonType .DOCUMENT , Document .class ) ;
115
- map . put ( BsonType .DOUBLE , Double .class ) ;
116
- map . put ( BsonType .INT32 , Integer .class ) ;
117
- map . put ( BsonType .INT64 , Long .class ) ;
118
- map . put ( BsonType .DECIMAL128 , Decimal128 .class ) ;
119
- map . put ( BsonType .MAX_KEY , MaxKey .class ) ;
120
- map . put ( BsonType .MIN_KEY , MinKey .class ) ;
121
- map . put ( BsonType .JAVASCRIPT , Code .class ) ;
122
- map . put ( BsonType .JAVASCRIPT_WITH_SCOPE , CodeWithScope .class ) ;
123
- map . put ( BsonType .OBJECT_ID , ObjectId .class ) ;
124
- map . put ( BsonType .REGULAR_EXPRESSION , BsonRegularExpression .class ) ;
125
- map . put ( BsonType .STRING , String .class ) ;
126
- map . put ( BsonType .SYMBOL , Symbol .class ) ;
127
- map . put ( BsonType .TIMESTAMP , BsonTimestamp .class ) ;
128
- map . put ( BsonType .UNDEFINED , BsonUndefined .class ) ;
104
+ bsonTypeOrdinalToClassMap [ BsonType .ARRAY . getValue ()] = List .class ;
105
+ bsonTypeOrdinalToClassMap [ BsonType .BINARY . getValue ()] = Binary .class ;
106
+ bsonTypeOrdinalToClassMap [ BsonType .BOOLEAN . getValue ()] = Boolean .class ;
107
+ bsonTypeOrdinalToClassMap [ BsonType .DATE_TIME . getValue ()] = Date .class ;
108
+ bsonTypeOrdinalToClassMap [ BsonType .DB_POINTER . getValue ()] = BsonDbPointer .class ;
109
+ bsonTypeOrdinalToClassMap [ BsonType .DOCUMENT . getValue ()] = Document .class ;
110
+ bsonTypeOrdinalToClassMap [ BsonType .DOUBLE . getValue ()] = Double .class ;
111
+ bsonTypeOrdinalToClassMap [ BsonType .INT32 . getValue ()] = Integer .class ;
112
+ bsonTypeOrdinalToClassMap [ BsonType .INT64 . getValue ()] = Long .class ;
113
+ bsonTypeOrdinalToClassMap [ BsonType .DECIMAL128 . getValue ()] = Decimal128 .class ;
114
+ bsonTypeOrdinalToClassMap [ BsonType .MAX_KEY . getValue ()] = MaxKey .class ;
115
+ bsonTypeOrdinalToClassMap [ BsonType .MIN_KEY . getValue ()] = MinKey .class ;
116
+ bsonTypeOrdinalToClassMap [ BsonType .JAVASCRIPT . getValue ()] = Code .class ;
117
+ bsonTypeOrdinalToClassMap [ BsonType .JAVASCRIPT_WITH_SCOPE . getValue ()] = CodeWithScope .class ;
118
+ bsonTypeOrdinalToClassMap [ BsonType .OBJECT_ID . getValue ()] = ObjectId .class ;
119
+ bsonTypeOrdinalToClassMap [ BsonType .REGULAR_EXPRESSION . getValue ()] = BsonRegularExpression .class ;
120
+ bsonTypeOrdinalToClassMap [ BsonType .STRING . getValue ()] = String .class ;
121
+ bsonTypeOrdinalToClassMap [ BsonType .SYMBOL . getValue ()] = Symbol .class ;
122
+ bsonTypeOrdinalToClassMap [ BsonType .TIMESTAMP . getValue ()] = BsonTimestamp .class ;
123
+ bsonTypeOrdinalToClassMap [ BsonType .UNDEFINED . getValue ()] = BsonUndefined .class ;
129
124
}
130
125
131
126
@ Override
@@ -139,15 +134,11 @@ public boolean equals(final Object o) {
139
134
140
135
BsonTypeClassMap that = (BsonTypeClassMap ) o ;
141
136
142
- if (!map .equals (that .map )) {
143
- return false ;
144
- }
145
-
146
- return true ;
137
+ return Arrays .equals (bsonTypeOrdinalToClassMap , that .bsonTypeOrdinalToClassMap );
147
138
}
148
139
149
140
@ Override
150
141
public int hashCode () {
151
- return map .hashCode ();
142
+ return Arrays .hashCode (bsonTypeOrdinalToClassMap );
152
143
}
153
144
}
0 commit comments