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

com.couchbase.lite.support.LazyJsonArray Maven / Gradle / Ivy

package com.couchbase.lite.support;

/**
 * A wrapper object for data representing a JSON array, parsing of the JSON
 * data is delayed until it is accessed via one of the List methods
 */
import com.couchbase.lite.Database;
import com.couchbase.lite.Manager;
import com.couchbase.lite.util.Log;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class LazyJsonArray extends AbstractList {

    private boolean parsed = false;
    private byte[] json;

    private List cache = new ArrayList();
    private Iterator cacheIterator;

    public LazyJsonArray(byte[] json) {
        if(json[0] != '[') {
            throw new IllegalArgumentException("data must represent a JSON array");
        }
        this.json = json;
    }

    @Override
    public Iterator iterator() {
        if(parsed) {
            return cache.iterator();
        } else {
            parseJson();
            return cache.iterator();
        }
    }

    @Override
    public T get(int index) {
        if(parsed) {
            return cache.get(index);
        } else {
            parseJson();
            return cache.get(index);
        }
    }

    @Override
    public int size() {
        if(parsed) {
            return cache.size();
        } else {
            parseJson();
            return cache.size();
        }
    }

    // the following methods in AbstractList use #iterator(). Overwrite them to make sure they use the
    // cached version
    @Override
    public boolean contains(Object o) {
        if(parsed) {
            return cache.contains(o);
        } else {
            parseJson();
            return cache.contains(o);
        }
    }
    @Override
    public Object[] toArray() {
        if(parsed) {
            return cache.toArray();
        } else {
            parseJson();
            return cache.toArray();
        }
    }
    public  S[] toArray(S[] a) {
        if(parsed) {
            return cache.toArray(a);
        } else {
            parseJson();
            return cache.toArray(a);
        }
    }
    @Override
    public int hashCode() {
        if(parsed) {
            return cache.hashCode();
        } else {
            parseJson();
            return cache.hashCode();
        }
    }

    private void parseJson() {
        if(parsed) {
            return;
        }

        try {
            List parsedvalues  = (List) Manager.getObjectMapper().readValue(json, Object.class);
            //Merge parsed values into List, overwriting the values for duplicate keys
            parsedvalues.addAll(cache);
            cache = parsedvalues;
        } catch (Exception e) {
            Log.e(Database.TAG, this.getClass().getName()+": Failed to parse Json data: ", e);
        } finally {
            parsed = true;
            json = null;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy