Package org.bson

Class BsonDocumentWrapper<T>

  • Type Parameters:
    T - the type of the document that is wrapped
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.String,​BsonValue>, Bson

    public final class BsonDocumentWrapper<T>
    extends BsonDocument
    A BsonDocument that begins its life as a document of any type and an Encoder for that document, which lets an instance of any class with an Encoder be treated as a BsonDocument. If any methods are called which required access to the individual elements of the document, then, on demand, the document will be unwrapped into a BsonDocument using a BsonDocumentWriter and the Encoder. But if all that is done with this document is to encode it, then the Encoder will be used to do that.
    Since:
    3.0
    See Also:
    BsonDocumentWriter, Serialized Form
    • Constructor Detail

      • BsonDocumentWrapper

        public BsonDocumentWrapper​(T wrappedDocument,
                                   Encoder<T> encoder)
        Construct a new instance with the given document and encoder for the document.
        Parameters:
        wrappedDocument - the wrapped document
        encoder - the encoder for the wrapped document
    • Method Detail

      • asBsonDocument

        public static BsonDocument asBsonDocument​(java.lang.Object document,
                                                  CodecRegistry codecRegistry)
        A helper to convert an document of type Object to a BsonDocument

        If not already a BsonDocument it looks up the documents' class in the codecRegistry and wraps it into a BsonDocumentWrapper

        Parameters:
        document - the document to convert
        codecRegistry - the codecRegistry that can be used in the conversion of the Object
        Returns:
        a BsonDocument
      • getWrappedDocument

        public T getWrappedDocument()
        Get the wrapped document.
        Returns:
        the wrapped document
      • getEncoder

        public Encoder<T> getEncoder()
        Get the encoder to use for the wrapped document.
        Returns:
        the encoder
      • isUnwrapped

        public boolean isUnwrapped()
        Determine whether the document has been unwrapped already.
        Returns:
        true if the wrapped document has been unwrapped already
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        size in class BsonDocument
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        isEmpty in class BsonDocument
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        containsKey in class BsonDocument
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        containsValue in class BsonDocument
      • get

        public BsonValue get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        get in class BsonDocument
      • remove

        public BsonValue remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        remove in class BsonDocument
      • putAll

        public void putAll​(java.util.Map<? extends java.lang.String,​? extends BsonValue> m)
        Specified by:
        putAll in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        putAll in class BsonDocument
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        clear in class BsonDocument
      • keySet

        public java.util.Set<java.lang.String> keySet()
        Specified by:
        keySet in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        keySet in class BsonDocument
      • values

        public java.util.Collection<BsonValue> values()
        Specified by:
        values in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        values in class BsonDocument
      • entrySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,​BsonValue>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        entrySet in class BsonDocument
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        equals in class BsonDocument
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        hashCode in class BsonDocument