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

com.dropbox.client2.jsonextract.JsonList Maven / Gradle / Ivy

Go to download

The Dropbox API for Java is a Dropbox supported client library for accessing the JSON+REST interface to Dropbox. It supports OAuth proxied authentication. It is designed to be simple and easy to use, as well as instructional in case you want to write your own. It is MIT licensed.

The newest version!
package com.dropbox.client2.jsonextract;

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

/**
 * (Internal class for extracting JSON.)
 *
 * A JSON "array" (a list of arbitrary JSON values).
 */
public final class JsonList extends JsonBase> implements Iterable {

    public JsonList(java.util.List internal, String path) {
        super(internal, path);
    }

    public JsonList(java.util.List internal) {
        super(internal);
    }

    private static String pathConcatIndex(String path, int index) {
        return JsonThing.pathConcat(path, Integer.toString(index));
    }

    public JsonThing get(int index) throws JsonExtractionException {
        if (index >= internal.size()) {
            throw error("expecting array to have an element at index " + index + ", but it only has length " + internal.size());
        }
        return new JsonThing(internal.get(index), pathConcatIndex(path, index));
    }

    public void expectLength(int length) throws JsonExtractionException {
        if (internal.size() != length) {
            throw error("expecting array to have length " + length + ", but it has length " + internal.size());
        }
    }

    public int length() {
        return internal.size();
    }

    private static final class WrapperIterator implements Iterator {
        private int numReturned = 0;
        private final String path;
        private final Iterator internal;
        private WrapperIterator(String path, Iterator internal) {
            this.path = path;
            this.internal = internal;
        }

        public boolean hasNext() { return internal.hasNext(); }
        public void remove() { throw new UnsupportedOperationException("can't remove"); }

        public JsonThing next() {
            int index = numReturned++;
            return new JsonThing(internal.next(), pathConcatIndex(path, index));
        }
    }

    public Iterator iterator() {
        return new WrapperIterator(path, internal.iterator());
    }

    public static final class Extractor extends JsonExtractor> {
        public final JsonExtractor elementExtractor;
        public Extractor(JsonExtractor elementExtractor) {
            this.elementExtractor = elementExtractor;
        }

        public List extract(JsonThing jt) throws JsonExtractionException {
            return jt.expectList().extract(this.elementExtractor);
        }
    }

    public  ArrayList extract(JsonExtractor elementExtractor) throws JsonExtractionException {
        ArrayList result = new ArrayList(length());
        for (Object o : internal) {
            result.add(elementExtractor.extract(new JsonThing(o)));
        }
        return result;
    }
}