Package com.cognifide.sling.query.iterator

Source Code of com.cognifide.sling.query.iterator.NextIterator

package com.cognifide.sling.query.iterator;

import java.util.Iterator;

import org.apache.sling.api.resource.Resource;

import com.cognifide.sling.query.api.ResourcePredicate;

public class NextIterator extends AbstractResourceIterator {

  private final ResourcePredicate until;

  private final Iterator<Resource> nextResources;

  private boolean finished;

  public NextIterator(ResourcePredicate until, Resource resource) {
    this.until = until;
    this.nextResources = rewindedIterator(resource);
    this.finished = false;
  }

  @Override
  protected Resource getResource() {
    if (finished) {
      return null;
    }
    while (nextResources.hasNext()) {
      Resource resource = nextResources.next();
      if (until != null && until.accepts(resource)) {
        finished = true;
        return null;
      }
      if (until == null) {
        finished = true;
      }
      return resource;
    }
    return null;
  }

  private Iterator<Resource> rewindedIterator(Resource resource) {
    String resourceName = resource.getName();
    Resource parent = resource.getParent();
    if (parent == null) {
      return EmptyIterator.INSTANCE;
    }
    Iterator<Resource> siblings = parent.listChildren();
    while (siblings.hasNext()) {
      Resource sibling = siblings.next();
      if (sibling.getName().equals(resourceName)) {
        break;
      }
    }
    return siblings;
  }

}
TOP

Related Classes of com.cognifide.sling.query.iterator.NextIterator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.