All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bson.codecs.BsonTypeClassMap Maven / Gradle / Ivy

Go to download

The MongoDB Java Driver uber-artifact, containing the legacy driver, the mongodb-driver, mongodb-driver-core, and bson

There is a newer version: 3.12.14
Show newest version
/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.bson.codecs;

import org.bson.BsonDbPointer;
import org.bson.BsonRegularExpression;
import org.bson.BsonTimestamp;
import org.bson.BsonType;
import org.bson.BsonUndefined;
import org.bson.Document;
import org.bson.types.Binary;
import org.bson.types.Code;
import org.bson.types.CodeWithScope;
import org.bson.types.Decimal128;
import org.bson.types.MaxKey;
import org.bson.types.MinKey;
import org.bson.types.ObjectId;
import org.bson.types.Symbol;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 

A map from a BSON types to the Class to which it should be decoded. This class is useful if, for example, * you want to change the default decoding of BSON DATE to something besides {@code java.util.Date}.

* *

The default mappings are:

* *
    *
  • DOCUMENT: {@code org.bson.Document.class}
  • *
  • ARRAY: {@code java.util.List.class}
  • *
  • DATE_TIME: {@code java.util.Date.class}
  • *
  • BOOLEAN: {@code java.lang.Boolean.class}
  • *
  • DOUBLE: {@code java.lang.Double.class}
  • *
  • INT32: {@code java.lang.Integer.class}
  • *
  • INT64: {@code java.lang.Long.class}
  • *
  • DECIMAL128: {@code org.bson.types.Decimal128.class}
  • *
  • STRING: {@code java.lang.String.class}
  • *
  • BINARY: {@code org.bson.types.Binary.class}
  • *
  • OBJECT_ID: {@code org.bson.types.ObjectId.class}
  • *
  • REGULAR_EXPRESSION: {@code org.bson.types.RegularExpression.class}
  • *
  • SYMBOL: {@code org.bson.types.Symbol.class}
  • *
  • DB_POINTER: {@code org.bson.types.DBPointer.class}
  • *
  • MAX_KEY: {@code org.bson.types.MaxKey.class}
  • *
  • MIN_KEY: {@code org.bson.types.MinKey.class}
  • *
  • JAVASCRIPT: {@code org.bson.types.Code.class}
  • *
  • JAVASCRIPT_WITH_SCOPE: {@code org.bson.types.CodeWithScope.class}
  • *
  • TIMESTAMP: {@code org.bson.types.BSONTimestamp.class}
  • *
  • UNDEFINED: {@code org.bson.types.Undefined.class}
  • *
* * @since 3.0 */ public class BsonTypeClassMap { private final Map> map = new HashMap>(); /** * 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> replacementsForDefaults) { addDefaults(); map.putAll(replacementsForDefaults); } /** * Construct an instance with the default mappings. */ public BsonTypeClassMap() { this(Collections.>emptyMap()); } Set keys() { return map.keySet(); } /** * Gets the Class that is mapped to the given BSON type. * * @param bsonType the BSON type * @return the Class that is mapped to the BSON type */ public Class get(final BsonType bsonType) { return map.get(bsonType); } private void addDefaults() { map.put(BsonType.ARRAY, List.class); map.put(BsonType.BINARY, Binary.class); map.put(BsonType.BOOLEAN, Boolean.class); map.put(BsonType.DATE_TIME, Date.class); map.put(BsonType.DB_POINTER, BsonDbPointer.class); map.put(BsonType.DOCUMENT, Document.class); map.put(BsonType.DOUBLE, Double.class); map.put(BsonType.INT32, Integer.class); map.put(BsonType.INT64, Long.class); map.put(BsonType.DECIMAL128, Decimal128.class); map.put(BsonType.MAX_KEY, MaxKey.class); map.put(BsonType.MIN_KEY, MinKey.class); map.put(BsonType.JAVASCRIPT, Code.class); map.put(BsonType.JAVASCRIPT_WITH_SCOPE, CodeWithScope.class); map.put(BsonType.OBJECT_ID, ObjectId.class); map.put(BsonType.REGULAR_EXPRESSION, BsonRegularExpression.class); map.put(BsonType.STRING, String.class); map.put(BsonType.SYMBOL, Symbol.class); map.put(BsonType.TIMESTAMP, BsonTimestamp.class); map.put(BsonType.UNDEFINED, BsonUndefined.class); } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final BsonTypeClassMap that = (BsonTypeClassMap) o; if (!map.equals(that.map)) { return false; } return true; } @Override public int hashCode() { return map.hashCode(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy