Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.mokung.pomegranate.jackson.JsonArray Maven / Gradle / Ivy
package com.mokung.pomegranate.jackson;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.function.Consumer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.*;
/**
* json array
*
* @author mokung
*/
public class JsonArray extends AbstractJsonContainer
implements List, Cloneable, Serializable {
public JsonArray() {
super();
}
public JsonArray(ObjectMapper objectMapper) {
super(objectMapper);
}
public JsonArray(ArrayNode hub) {
super(null, hub);
}
public JsonArray(ObjectMapper objectMapper, ArrayNode hub) {
super(objectMapper, hub);
}
@Override
protected ArrayNode createHub(ObjectMapper objectMapper) {
return objectMapper.createArrayNode();
}
@Override
public Object get(int index) {
return getInterior(index);
}
@Override
public void add(int index, Object value) {
// null
if (value == null) {
container.insertNull(index);
return;
}
// 常用类型
if (value instanceof BigDecimal) {
container.insert(index, (BigDecimal)value);
return;
}
if (value instanceof BigInteger) {
container.insert(index, (BigInteger)value);
return;
}
if (value instanceof Boolean) {
container.insert(index, (Boolean)value);
return;
}
if (value instanceof Byte) {
container.insert(index, (Byte)value);
return;
}
if (value instanceof Double) {
container.insert(index, (Double)value);
return;
}
if (value instanceof Float) {
container.insert(index, (Float)value);
return;
}
if (value instanceof Integer) {
container.insert(index, (Integer)value);
return;
}
if (value instanceof Long) {
container.insert(index, (Long)value);
return;
}
if (value instanceof Short) {
container.insert(index, (Short)value);
return;
}
if (value instanceof String) {
container.insert(index, (String)value);
return;
}
// 自定义类型
if (value instanceof AbstractJsonContainer, ?>) {
container.insert(index, ((AbstractJsonContainer, ?>)value).getContainer());
return;
}
// JsonNode 类型
if (value instanceof JsonNode) {
container.insert(index, (JsonNode)value);
return;
}
// 默认添加类型
container.insertPOJO(index, value);
}
@Override
public boolean add(Object value) {
// null
if (value == null) {
container.addNull();
return true;
}
// 常用类型
if (value instanceof BigDecimal) {
container.add((BigDecimal)value);
return true;
}
if (value instanceof BigInteger) {
container.add((BigInteger)value);
return true;
}
if (value instanceof Boolean) {
container.add((Boolean)value);
return true;
}
if (value instanceof Byte) {
container.add((Byte)value);
return true;
}
if (value instanceof Double) {
container.add((Double)value);
return true;
}
if (value instanceof Float) {
container.add((Float)value);
return true;
}
if (value instanceof Integer) {
container.add((Integer)value);
return true;
}
if (value instanceof Long) {
container.add((Long)value);
return true;
}
if (value instanceof Short) {
container.add((Short)value);
return true;
}
if (value instanceof String) {
container.add((String)value);
return true;
}
// 自定义类型
if (value instanceof AbstractJsonContainer) {
container.add(((AbstractJsonContainer, ?>)value).getContainer());
return true;
}
// JsonNode 类型
if (value instanceof JsonNode) {
container.add((JsonNode)value);
return true;
}
// 默认添加类型
container.addPOJO(value);
return true;
}
@Override
public Object set(int index, Object value) {
// null
if (value == null) {
container.set(index, null);
return null;
}
// 常用类型
if (value instanceof BigDecimal) {
container.set(index, new DecimalNode((BigDecimal)value));
return value;
}
if (value instanceof BigInteger) {
container.set(index, new BigIntegerNode((BigInteger)value));
return value;
}
if (value instanceof Boolean) {
container.set(index, BooleanNode.valueOf((Boolean)value));
return value;
}
if (value instanceof Byte) {
container.set(index, new ShortNode((Byte)value));
return value;
}
if (value instanceof Double) {
container.set(index, new DoubleNode((Double)value));
return value;
}
if (value instanceof Float) {
container.set(index, new FloatNode((Float)value));
return value;
}
if (value instanceof Integer) {
container.set(index, new IntNode((Integer)value));
return value;
}
if (value instanceof Long) {
container.set(index, new LongNode((Long)value));
return value;
}
if (value instanceof Short) {
container.set(index, new ShortNode((Short)value));
return value;
}
if (value instanceof String) {
container.set(index, new TextNode((String)value));
return value;
}
// 自定义类型
if (value instanceof AbstractJsonContainer) {
container.set(index, ((AbstractJsonContainer, ?>)value).getContainer());
return value;
}
// JsonNode 类型
if (value instanceof JsonNode) {
container.set(index, (JsonNode)value);
return value;
}
// 默认添加类型
container.set(index, new POJONode(value));
return value;
}
@Override
public int size() {
return container.size();
}
@Override
public boolean isEmpty() {
return container.isEmpty(null);
}
@Override
public boolean contains(Object o) {
for (Object value : this) {
if (Objects.equals(value, o)) {
return true;
}
}
return false;
}
@Override
public Iterator iterator() {
return jsonArrayIterator();
}
@Override
public Object[] toArray() {
int size = size();
Object[] result = new Object[size];
for (int index = 0; index < size; index++) {
result[index] = get(index);
}
return result;
}
@Override
public T[] toArray(T[] a) {
throw new IllegalStateException("not cast");
}
@Override
public boolean remove(Object o) {
Iterator iterator = iterator();
while (iterator.hasNext()) {
Object value = iterator.next();
if (Objects.equals(value, o)) {
iterator.remove();
return true;
}
}
return false;
}
@Override
public boolean containsAll(Collection> c) {
for (Object con : c) {
if (!this.contains(con)) {
return false;
}
}
return true;
}
@Override
public boolean addAll(Collection> c) {
for (Object con : c) {
add(con);
}
return true;
}
@Override
public boolean addAll(int index, Collection> c) {
int addIndex = index;
for (Object value : c) {
add(addIndex++, value);
}
return true;
}
@Override
public boolean removeAll(Collection> c) {
batchRemove(c, true);
return true;
}
@Override
public boolean retainAll(Collection> c) {
batchRemove(c, false);
return true;
}
@Override
public void clear() {
container.removeAll();
}
@Override
public Object remove(int index) {
return container.remove(index);
}
@Override
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size(); i++) {
if (get(i) == null) {
return i;
}
}
} else {
for (int i = 0; i < size(); i++) {
if (o.equals(get(i))) {
return i;
}
}
}
return -1;
}
@Override
public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size() - 1; i >= 0; i--) {
if (get(i) == null) {
return i;
}
}
} else {
for (int i = size() - 1; i >= 0; i--) {
if (o.equals(get(i))) {
return i;
}
}
}
return -1;
}
@Override
public ListIterator listIterator() {
return jsonArrayIterator();
}
@Override
public ListIterator listIterator(int index) {
return jsonArrayIterator();
}
@Override
public List subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, container.size());
List subList = new ArrayList<>(toIndex - fromIndex);
for (int index = fromIndex; index < toIndex; index++) {
subList.add(get(index));
}
return subList;
}
void subListRangeCheck(int fromIndex, int toIndex, int size) {
if (fromIndex < 0) {
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
}
if (toIndex > size) {
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
}
if (fromIndex > toIndex) {
throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
}
}
protected void batchRemove(Collection> c, boolean equalsResult) {
for (Object value : c) {
if (contains(value) == equalsResult) {
remove(value);
}
}
}
protected JsonArrayIterator jsonArrayIterator() {
return new JsonArrayIterator(container.iterator(), objectMapper);
}
private static class JsonArrayIterator implements ListIterator, Iterator {
public JsonArrayIterator(Iterator iterator, ObjectMapper objectMapper) {
this.iterator = iterator;
this.objectMapper = objectMapper;
}
private final Iterator iterator;
private final ObjectMapper objectMapper;
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Object next() {
JsonNode valueNode = iterator.next();
if (valueNode == null || valueNode instanceof NullNode) {
return null;
}
return GlobalConfiguration.getInstance().getValue(valueNode, objectMapper, false);
}
@Override
public boolean hasPrevious() {
throw new NullPointerException();
}
@Override
public Object previous() {
throw new NullPointerException();
}
@Override
public int nextIndex() {
throw new NullPointerException();
}
@Override
public int previousIndex() {
throw new NullPointerException();
}
@Override
public void remove() {
iterator.remove();
}
@Override
public void set(Object o) {
throw new NullPointerException();
}
@Override
public void add(Object o) {
throw new NullPointerException();
}
@Override
public void forEachRemaining(Consumer super Object> action) {
iterator.forEachRemaining(valueNode -> {
Object value = GlobalConfiguration.getInstance().getValue(valueNode, objectMapper, false);
action.accept(value);
});
}
}
}