ResourceIterator.java 1.17 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.Resource
 */
package com.day.cq.wcm.commons;

import java.util.Iterator;
import java.util.NoSuchElementException;
import org.apache.sling.api.resource.Resource;

public class ResourceIterator<T>
implements Iterator<T> {
    private T next;
    private final Iterator<Resource> base;
    private final Class<T> adapterType;

    public ResourceIterator(Iterator<Resource> base, Class<T> adapterType) {
        this.base = base;
        this.adapterType = adapterType;
        this.seek();
    }

    private T seek() {
        T prev = this.next;
        this.next = null;
        while (this.base.hasNext() && this.next == null) {
            this.next = this.base.next().adaptTo(this.adapterType);
        }
        return prev;
    }

    @Override
    public boolean hasNext() {
        return this.next != null;
    }

    @Override
    public T next() {
        if (this.next == null) {
            throw new NoSuchElementException();
        }
        return this.seek();
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}