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

org.dsa.iot.dslink.util.json.JsonArray Maven / Gradle / Ivy

There is a newer version: 0.24.2
Show newest version
package org.dsa.iot.dslink.util.json;

import io.netty.util.CharsetUtil;

import java.util.*;

/**
 * @author Samuel Grenier
 */
@SuppressWarnings("unchecked")
public class JsonArray implements Iterable {

    private final List list;

    public JsonArray() {
        this(new LinkedList<>());
    }

    public JsonArray(String content) {
        this(EncodingFormat.JSON, content.getBytes(CharsetUtil.UTF_8));
    }

    public JsonArray(EncodingFormat format,
                     byte[] content) {
        this(format, content, 0, content.length);
    }

    public JsonArray(EncodingFormat format,
                     byte[] content,
                     int offset,
                     int length) {
        this(Json.decodeList(format, content, offset, length));
    }

    public JsonArray(List list) {
        if (list == null) {
            throw new NullPointerException("list");
        }
        this.list = list;
    }

    @SuppressWarnings("unused")
    public byte[] encode() {
        return encode(EncodingFormat.JSON);
    }

    public byte[] encode(EncodingFormat format) {
        return Json.encode(format, this);
    }

    @SuppressWarnings("unused")
    public byte[] encodePrettily() {
        return encodePrettily(EncodingFormat.JSON);
    }

    public byte[] encodePrettily(EncodingFormat format) {
        return Json.encodePrettily(format, this);
    }

    public  T remove(int index) {
        return (T) Json.update(list.remove(index));
    }

    public  T get(int index) {
        return (T) Json.update(list.get(index));
    }

    public  T set(int index, Object value) {
        value = Json.checkAndUpdate(value);
        return (T) list.set(index, value);
    }

    public JsonArray add(Object value) {
        value = Json.checkAndUpdate(value);
        list.add(value);
        return this;
    }

    public JsonArray add(int pos, Object value) {
        value = Json.checkAndUpdate(value);
        list.add(pos, value);
        return this;
    }

    public JsonArray addAll(int pos, List values) {
        if (values != null) {
            ListIterator it = values.listIterator();
            while (it.hasNext()) {
                Object o = it.next();
                it.set(Json.checkAndUpdate(o));
            }
            list.addAll(pos, values);
        }
        return this;
    }

    public int size() {
        return list.size();
    }

    public void mergeIn(JsonArray other) {
        list.addAll(other.list);
    }

    public List getList() {
        return Collections.unmodifiableList(list);
    }

    @Override
    public String toString() {
        return new String(encode(EncodingFormat.JSON), CharsetUtil.UTF_8);
    }

    @Override
    public Iterator iterator() {
        return new JsonIterator(list.iterator());
    }

    private static class JsonIterator implements Iterator {

        private final Iterator it;

        public JsonIterator(Iterator it) {
            this.it = it;
        }

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public Object next() {
            return Json.update(it.next());
        }

        @Override
        public void remove() {
            it.remove();
        }
    }
}