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

com.github.aidensuen.mongo.bson.DocumentList Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.github.aidensuen.mongo.bson;

import com.github.aidensuen.mongo.bson.codecs.DocumentListCodec;
import org.bson.BsonType;
import org.bson.Document;
import org.bson.codecs.DecoderContext;
import org.bson.json.JsonReader;

import java.util.*;

public class DocumentList implements List {

    private final List values;

    /**
     * Construct an instance with the given list of values.
     *
     * @param values the list of values, none of whose members may be null.
     */
    public DocumentList(final List values) {
        this(values, true);
    }

    /**
     * Construct an empty DocumentList
     */
    public DocumentList() {
        this(new ArrayList(), false);
    }

    @SuppressWarnings("unchecked")
    DocumentList(final List values, final boolean copy) {
        if (copy) {
            this.values = new ArrayList(values);
        } else {
            this.values = (List) values;
        }
    }

    /**
     * Parses a string in MongoDB Extended JSON format to a {@code DocumentList}
     *
     * @param json the JSON string
     * @return a corresponding {@code DocumentList} object
     * @mongodb.driver.manual reference/mongodb-extended-json/ MongoDB Extended JSON
     * @see org.bson.json.JsonReader
     * @since 3.4
     */
    public static DocumentList parse(final String json) {
        return new DocumentListCodec().decode(new JsonReader(json), DecoderContext.builder().build());
    }

    /**
     * Gets the values in this array as a list of {@code Document} objects.
     *
     * @return the values in this array.
     */
    public List getValues() {
        return Collections.unmodifiableList(values);
    }

    public BsonType getBsonType() {
        return BsonType.ARRAY;
    }

    @Override
    public int size() {
        return values.size();
    }

    @Override
    public boolean isEmpty() {
        return values.isEmpty();
    }

    @Override
    public boolean contains(final Object o) {
        return values.contains(o);
    }

    @Override
    public Iterator iterator() {
        return values.iterator();
    }

    @Override
    public Object[] toArray() {
        return values.toArray();
    }

    @Override
    public  T[] toArray(final T[] a) {
        return values.toArray(a);
    }

    @Override
    public boolean add(final Document bsonValue) {
        return values.add(bsonValue);
    }

    @Override
    public boolean remove(final Object o) {
        return values.remove(o);
    }

    @Override
    public boolean containsAll(final Collection c) {
        return values.containsAll(c);
    }

    @Override
    public boolean addAll(final Collection c) {
        return values.addAll(c);
    }

    @Override
    public boolean addAll(final int index, final Collection c) {
        return values.addAll(index, c);
    }

    @Override
    public boolean removeAll(final Collection c) {
        return values.removeAll(c);
    }

    @Override
    public boolean retainAll(final Collection c) {
        return values.retainAll(c);
    }

    @Override
    public void clear() {
        values.clear();
    }

    @Override
    public Document get(final int index) {
        return values.get(index);
    }

    @Override
    public Document set(final int index, final Document element) {
        return values.set(index, element);
    }

    @Override
    public void add(final int index, final Document element) {
        values.add(index, element);
    }

    @Override
    public Document remove(final int index) {
        return values.remove(index);
    }

    @Override
    public int indexOf(final Object o) {
        return values.indexOf(o);
    }

    @Override
    public int lastIndexOf(final Object o) {
        return values.lastIndexOf(o);
    }

    @Override
    public ListIterator listIterator() {
        return values.listIterator();
    }

    @Override
    public ListIterator listIterator(final int index) {
        return values.listIterator(index);
    }

    @Override
    public List subList(final int fromIndex, final int toIndex) {
        return values.subList(fromIndex, toIndex);
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof DocumentList)) {
            return false;
        }

        DocumentList that = (DocumentList) o;
        return getValues().equals(that.getValues());
    }

    @Override
    public int hashCode() {
        return values.hashCode();
    }

    @Override
    public String toString() {
        return "DocumentList{"
                + "values=" + values
                + '}';
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy