Example usage for org.apache.commons.collections.iterators IteratorChain size

List of usage examples for org.apache.commons.collections.iterators IteratorChain size

Introduction

In this page you can find the example usage for org.apache.commons.collections.iterators IteratorChain size.

Prototype

public int size() 

Source Link

Document

Number of Iterators in the current IteratorChain.

Usage

From source file:org.apache.sling.resourceresolver.impl.helper.ResourceResolverControl.java

/**
 * This method asks all matching resource providers for the children iterators,
 * merges them, adds {@link SyntheticResource}s (see
 * {@link #getResource(String, Resource, Map, boolean)} for more details),
 * filters out the duplicates and returns the resulting iterator. All
 * transformations are done lazily, during the {@link Iterator#hasNext()}
 * invocation on the result./*from  ww w.  ja v a 2 s .c  o m*/
 */
@SuppressWarnings("unchecked")
public Iterator<Resource> listChildren(final ResourceResolverContext context, final Resource parent) {
    final String parentPath = parent.getPath();

    // 3 sources are combined: children of the provider which owns 'parent',
    // providers which are directly mounted at a child path,
    // synthetic resources for providers mounted at a lower level

    // children of the 'parent' provider
    Iterator<Resource> realChildren = null;
    final AuthenticatedResourceProvider provider = this.getBestMatchingProvider(context, parentPath);
    if (provider != null) {
        realChildren = provider.listChildren(parent);
    }

    final Set<String> visitedNames = new HashSet<String>();

    IteratorChain chain = new IteratorChain();
    if (realChildren != null) {
        chain.addIterator(realChildren);
    }

    // synthetic and providers are done in one loop
    final Node<ResourceProviderHandler> node = getResourceProviderStorage().getTree().getNode(parent.getPath());
    if (node != null) {
        final List<Resource> syntheticList = new ArrayList<Resource>();
        final List<Resource> providerList = new ArrayList<Resource>();

        for (final Entry<String, Node<ResourceProviderHandler>> entry : node.getChildren().entrySet()) {
            final String name = entry.getKey();
            final ResourceProviderHandler handler = entry.getValue().getValue();
            PathBuilder pathBuilder = new PathBuilder(parent.getPath());
            pathBuilder.append(name);
            final String childPath = pathBuilder.toString();
            if (handler == null) {
                syntheticList.add(new SyntheticResource(context.getResourceResolver(), childPath,
                        ResourceProvider.RESOURCE_TYPE_SYNTHETIC));
            } else {
                Resource rsrc = null;
                try {
                    final AuthenticatedResourceProvider rp = context.getProviderManager()
                            .getOrCreateProvider(handler, this);
                    rsrc = rp == null ? null : rp.getResource(childPath, parent, null);
                } catch (final LoginException ignore) {
                    // ignore
                }
                if (rsrc != null) {
                    providerList.add(rsrc);
                } else {
                    // if there is a child provider underneath, we need to create a synthetic resource
                    // otherwise we need to make sure that no one else is providing this child
                    if (entry.getValue().getChildren().isEmpty()) {
                        syntheticList.add(new SyntheticResource(context.getResourceResolver(), childPath,
                                ResourceProvider.RESOURCE_TYPE_SYNTHETIC));
                    } else {
                        visitedNames.add(name);
                    }
                }
            }
        }
        if (!providerList.isEmpty()) {
            chain.addIterator(providerList.iterator());
        }
        if (!syntheticList.isEmpty()) {
            chain.addIterator(syntheticList.iterator());
        }
    }
    if (chain.size() == 0) {
        return Collections.EMPTY_LIST.iterator();
    }
    return new UniqueResourceIterator(visitedNames, chain);
}