org.hcjf.bson.BsonArray Maven / Gradle / Ivy
package org.hcjf.bson;
import java.util.*;
/**
* Bson array implementation.
* @author javaito
*/
public class BsonArray extends BsonCollection {
private static final String JSON_ARRAY_START = "[";
private static final String JSON_ARRAY_END = "]";
public BsonArray(String name) {
this(name, DEFAULT_COLLECTION_SIZE);
}
public BsonArray(String name, Collection extends Object> values) {
this(name, DEFAULT_COLLECTION_SIZE);
for(Object value : values) {
if(value != null) {
add(value);
}
}
}
public BsonArray(String name, Integer length) {
super(name, length);
}
/**
* Return the element of the position.
* @param index Position index.
* @return Element founded or null.
*/
public final BsonElement get(Integer index) {
return getValue().get(index.toString());
}
/**
* Add the element to the array.
* @param element Element to add.
* @throws IllegalArgumentException if the element is null.
*/
public final void add(BsonElement element) {
if(element == null) {
throw new IllegalArgumentException("Bson array not support null elements.");
}
Integer currentSize = getValue().size();
element.setName(currentSize.toString());
putElement(element);
}
/**
* Add a new value to the array.
* @param value Array's value.
*/
public final void add(Object value) {
String name = Integer.toString(getValue().size());
if(value instanceof Map) {
add(new BsonDocument(name, (Map)value));
} else if(value instanceof List) {
add(new BsonArray(name, (List