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

com.mogudiandian.util.json.fastjson.JSONArrayIterable Maven / Gradle / Ivy

package com.mogudiandian.util.json.fastjson;

import com.alibaba.fastjson.JSONArray;

import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * JSONArray的迭代器
 * @author Joshua Sun
 * @since 1.0.0
 */
public final class JSONArrayIterable implements Iterable {

    /**
     * 内部维护着一个list 使用委托
     */
    private List items;

    public JSONArrayIterable(JSONArray jsonArray, Class itemType) {
        if (jsonArray == null) {
            throw new RuntimeException("json array can not be null");
        }
        if (itemType == null) {
            throw new RuntimeException("item type can not be null");
        }
        items = IntStream.range(0, jsonArray.size())
                         .mapToObj(x -> jsonArray.getObject(x, itemType))
                         .collect(Collectors.toList());
    }

    @Override
    public Iterator iterator() {
        return items.iterator();
    }

    @Override
    public Spliterator spliterator() {
        return items.spliterator();
    }

    public static  JSONArrayIterable iterator(JSONArray jsonArray, Class itemType) {
        return new JSONArrayIterable<>(jsonArray, itemType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy