不理解 Java class 包含可迭代自身的定义。

Don't understand Java class definition containing an iterable of itself.

我在我们的 java 项目中遇到了一个 class,这让我难以理解。 它使用类型参数实现 Iterable 接口,因为它是该可迭代对象的自身。有人可以阐明它的作用以及它为什么有用吗?

public class IndexRequest extends Request implements Cloneable, Iterable<IndexRequest> {
    public enum OpType {insert, update};

    protected Index index;
    protected Type type;
    protected boolean incomingAsArray;
    protected long timeToLive;
    protected String parent;
    protected String route;
    protected LinkedListMultimap<String, IndexRequest> iterable;
    protected String errorMessage;
    protected String stackTrace;
    protected String warnMessage;
    protected OpType opType = OpType.insert;
    protected long versionId;
    protected JsonNode previousDocument;
    protected boolean ignored;
    protected String resultCode;
    protected int retries = 0;
    protected boolean esUpsertFlag = false;
    protected String id = UUID.randomUUID().toString();


    /**
     * Constructor
     */
    public IndexRequest() {
        super();
        logger = LoggerFactory.getLogger(IndexRequest.class);
        this.requestJson = JsonNodeFactory.instance.arrayNode();
    }


    /**
     * Constructor
     */
    public IndexRequest(String endpointPath, String requestId, String user, JsonNode requestJson, Map<String, String[]> requestParameters) {
        super(endpointPath, requestId, user, requestJson, requestParameters);
    }

    /**
     * Initialize our iterable version of this class
     */
    protected void initIterable() {
LinkedListMultimap <String, IndexRequest> contents = LinkedListMultimap.create();
        if (isArray()) {
            ArrayNode docsArray = (ArrayNode) getDocument();
            Iterator<JsonNode> docIterator = docsArray.iterator();
            while (docIterator.hasNext()) {
                IndexRequest is = this.clone(false);
                // generate a new id since this is a new wrapper around a particular doc
                is.id = UUID.randomUUID().toString();
                is.setDocument(docIterator.next());
                contents.put(is.getId(), is);
            }

            iterable = contents;
        }
        else {
            iterable = LinkedListMultimap.create();                     
            iterable.put(getId(), this);
        }
    }

    /**
     * Returns an iterator for this index set
     * @return Iterator<IndexRequest> An iterator for this index set
     */
    public Iterator<IndexRequest> iterator() {
        if (iterable == null) {
            initIterable();
        }
        return iterable.values().iterator();
    }
}

提前致谢。

这个class看起来像是在这样的节点树中定义了一个节点。实例持有从 Strings 到相同 class:

的其他实例的映射
    protected LinkedListMultimap<String, IndexRequest> iterable;

他们还从他们的 parent class 那里继承了一些存在/持有其他实例数组的感觉。当它们被迭代时,它们会提供映射中的值或数组中的元素。

实现 Iterable 的 class 可以与新的 for 循环一起使用。这是这样一个例子:

List list = new ArrayList();

for(Object o : list){
    //do something o;    
}

Iterable接口只有一个方法:

public interface Iterable<T> {
  public Iterator<T> iterator();    
}

可以将我们自己的集合类型 classes 与新的 for 循环一起使用。为此,我们的 class 必须实现 java.lang.Iterable 接口。并提供 iterator 方法的实现,您可以在此处看到 class 中提供的方法:

  /**
     * Returns an iterator for this index set
     * @return Iterator<IndexRequest> An iterator for this index set
     */
    public Iterator<IndexRequest> iterator() {
        if (iterable == null) {
            initIterable();
        }
        return iterable.values().iterator();
    }