/** * Construct a new instance with a default {@code BsonTypeClassMap} and the given {@code Transformer}. The transformer is used by the * IterableCodec as a last step when decoding values. * * @param valueTransformer the value transformer for decoded values */ public IterableCodecProvider(final Transformer valueTransformer) { this(new BsonTypeClassMap(), valueTransformer); }
/** * Construct an instance with the default mapping, but replacing the default mapping with any values contained in the given map. * This allows a caller to easily replace a single or a few mappings, while leaving the rest at their default values. * * @param replacementsForDefaults the replacement mappings */ public BsonTypeClassMap(final Map<BsonType, Class<?>> replacementsForDefaults) { addDefaults(); map.putAll(replacementsForDefaults); }
/** * Get the {@code BsonValue} subclass associated with the given {@code BsonType}. * @param bsonType the BsonType * @return the class associated with the given type */ @SuppressWarnings("unchecked") public static Class<? extends BsonValue> getClassForBsonType(final BsonType bsonType) { return (Class<? extends BsonValue>) DEFAULT_BSON_TYPE_CLASS_MAP.get(bsonType); }
/** * Initializes the map by iterating the keys of the given BsonTypeClassMap and looking up the Codec for the Class mapped to each key. * @param bsonTypeClassMap the non-null BsonTypeClassMap * @param codecRegistry the non-null CodecRegistry */ public BsonTypeCodecMap(final BsonTypeClassMap bsonTypeClassMap, final CodecRegistry codecRegistry) { this.bsonTypeClassMap = notNull("bsonTypeClassMap", bsonTypeClassMap); notNull("codecRegistry", codecRegistry); for (BsonType cur : bsonTypeClassMap.keys()) { Class<?> clazz = bsonTypeClassMap.get(cur); if (clazz != null) { try { codecs[cur.getValue()] = codecRegistry.get(clazz); } catch (CodecConfigurationException e) { // delay reporting this until the codec is actually requested } } } }
@Override public int hashCode() { int result = bsonTypeClassMap.hashCode(); result = 31 * result + (valueTransformer != null ? valueTransformer.hashCode() : 0); return result; } }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } MapCodecProvider that = (MapCodecProvider) o; if (!bsonTypeClassMap.equals(that.bsonTypeClassMap)) { return false; } if (valueTransformer != null ? !valueTransformer.equals(that.valueTransformer) : that.valueTransformer != null) { return false; } return true; }
@Override public int hashCode() { int result = bsonTypeClassMap.hashCode(); result = 31 * result + (valueTransformer != null ? valueTransformer.hashCode() : 0); return result; } }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } DocumentCodecProvider that = (DocumentCodecProvider) o; if (!bsonTypeClassMap.equals(that.bsonTypeClassMap)) { return false; } if (valueTransformer != null ? !valueTransformer.equals(that.valueTransformer) : that.valueTransformer != null) { return false; } return true; }
/** * Construct a new instance with a default {@code BsonTypeClassMap} and the given {@code Transformer}. The transformer is used by the * DocumentCodec as a last step when decoding values. * * @param valueTransformer the value transformer for decoded values * @see org.bson.codecs.DocumentCodec#DocumentCodec(org.bson.codecs.configuration.CodecRegistry, BsonTypeClassMap, org.bson.Transformer) */ public DocumentCodecProvider(final Transformer valueTransformer) { this(new BsonTypeClassMap(), valueTransformer); }
@Override public int hashCode() { int result = bsonTypeClassMap.hashCode(); result = 31 * result + (valueTransformer != null ? valueTransformer.hashCode() : 0); return result; } }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } IterableCodecProvider that = (IterableCodecProvider) o; if (!bsonTypeClassMap.equals(that.bsonTypeClassMap)) { return false; } if (valueTransformer != null ? !valueTransformer.equals(that.valueTransformer) : that.valueTransformer != null) { return false; } return true; }
/** * Gets the Codec mapped to the given bson type. * * @param bsonType the non-null BsonType * @return the non-null Codec */ public Codec<?> get(final BsonType bsonType) { Codec<?> codec = codecs[bsonType.getValue()]; if (codec == null) { Class<?> clazz = bsonTypeClassMap.get(bsonType); if (clazz == null) { throw new CodecConfigurationException(format("No class mapped for BSON type %s.", bsonType)); } else { throw new CodecConfigurationException(format("Can't find a codec for %s.", clazz)); } } return codec; } }
/** * Construct a new instance with a default {@code BsonTypeClassMap} and the given {@code Transformer}. The transformer is used by the * MapCodec as a last step when decoding values. * * @param valueTransformer the value transformer for decoded values */ public MapCodecProvider(final Transformer valueTransformer) { this(new BsonTypeClassMap(), valueTransformer); }
/** * Construct a new instance with a default {@code BsonTypeClassMap} and no {@code Transformer}. */ public IterableCodecProvider() { this(new BsonTypeClassMap()); }
/** * Construct a new instance with a default {@code BsonTypeClassMap}. */ public MapCodecProvider() { this(new BsonTypeClassMap()); }
/** * Construct a new instance with a default {@code BsonTypeClassMap}. */ public DocumentCodecProvider() { this(new BsonTypeClassMap()); }
static BsonTypeClassMap createDefaultBsonTypeClassMap() { Map<BsonType, Class<?>> replacements = new HashMap<BsonType, Class<?>>(); replacements.put(BsonType.REGULAR_EXPRESSION, Pattern.class); replacements.put(BsonType.SYMBOL, String.class); replacements.put(BsonType.TIMESTAMP, BSONTimestamp.class); replacements.put(BsonType.JAVASCRIPT_WITH_SCOPE, null); replacements.put(BsonType.DOCUMENT, null); return new BsonTypeClassMap(replacements); }
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry()); ------- final DocumentCodec codec = new DocumentCodec(codecRegistry, new BsonTypeClassMap()); ------- System.out.println(document.toJson(codec));
static BsonTypeClassMap createDefaultBsonTypeClassMap() { Map<BsonType, Class<?>> replacements = new HashMap<BsonType, Class<?>>(); replacements.put(BsonType.REGULAR_EXPRESSION, Pattern.class); replacements.put(BsonType.SYMBOL, String.class); replacements.put(BsonType.TIMESTAMP, BSONTimestamp.class); replacements.put(BsonType.JAVASCRIPT_WITH_SCOPE, null); replacements.put(BsonType.DOCUMENT, null); return new BsonTypeClassMap(replacements); }
public static CodecRegistry codecRegistry() { if (codecRegistry == null) { synchronized (MongoCodecs.class) { if (codecRegistry == null) { Map<BsonType, Class<?>> replacements = new HashMap<BsonType, Class<?>>(); replacements.put(BsonType.TIMESTAMP, Timestamp.class); // make it use ByteArrayCodec instead of BinaryCodec // see also https://jira.mongodb.org/browse/JAVA-2025 replacements.put(BsonType.BINARY, byte[].class); // TODO: replacement for java.sql.Date and java.sql.Time // need a way to add two distinct replacements // can't use DATE_TIME, used for java.util.Date BsonTypeClassMap bsonTypeClassMap = new BsonTypeClassMap(replacements); DocumentCodecProvider documentCodecProvider = new DocumentCodecProvider(bsonTypeClassMap); codecRegistry = CodecRegistries.fromRegistries( CodecRegistries.fromCodecs( new TimestampCodec(), new DateCodec(), new TimeCodec()), CodecRegistries.fromProviders(documentCodecProvider), MongoClient.getDefaultCodecRegistry()); } } } return codecRegistry; }