package org.bson;
import org.bson.codecs.BsonArrayCodec;
import org.bson.codecs.DecoderContext;
import org.bson.json.JsonReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class BsonArray extends BsonValue implements List<BsonValue>, Cloneable {
private final List<BsonValue> values;
public BsonArray(final List<? extends BsonValue> values) {
this(values, true);
}
public BsonArray() {
this(new ArrayList<BsonValue>(), false);
}
@SuppressWarnings("unchecked")
BsonArray(final List<? extends BsonValue> values, final boolean copy) {
if (copy) {
this.values = new ArrayList<BsonValue>(values);
} else {
this.values = (List<BsonValue>) values;
}
}
public static BsonArray parse(final String json) {
return new BsonArrayCodec().decode(new JsonReader(json), DecoderContext.builder().build());
}
public List<BsonValue> getValues() {
return Collections.unmodifiableList(values);
}
@Override
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<BsonValue> iterator() {
return values.iterator();
}
@Override
public Object[] toArray() {
return values.toArray();
}
@Override
public <T> T[] toArray(final T[] a) {
return values.toArray(a);
}
@Override
public boolean add(final BsonValue 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<? extends BsonValue> c) {
return values.addAll(c);
}
@Override
public boolean addAll(final int index, final Collection<? extends BsonValue> 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 BsonValue get(final int index) {
return values.get(index);
}
@Override
public BsonValue set(final int index, final BsonValue element) {
return values.set(index, element);
}
@Override
public void add(final int index, final BsonValue element) {
values.add(index, element);
}
@Override
public BsonValue 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<BsonValue> listIterator() {
return values.listIterator();
}
@Override
public ListIterator<BsonValue> listIterator(final int index) {
return values.listIterator(index);
}
@Override
public List<BsonValue> 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 BsonArray)) {
return false;
}
BsonArray that = (BsonArray) o;
return getValues().equals(that.getValues());
}
@Override
public int hashCode() {
return values.hashCode();
}
@Override
public String toString() {
return "BsonArray{"
+ "values=" + values
+ '}';
}
@Override
public BsonArray clone() {
BsonArray to = new BsonArray();
for (BsonValue cur : this) {
switch (cur.getBsonType()) {
case DOCUMENT:
to.add(cur.asDocument().clone());
break;
case ARRAY:
to.add(cur.asArray().clone());
break;
case BINARY:
to.add(BsonBinary.clone(cur.asBinary()));
break;
case JAVASCRIPT_WITH_SCOPE:
to.add(BsonJavaScriptWithScope.clone(cur.asJavaScriptWithScope()));
break;
default:
to.add(cur);
}
}
return to;
}
}