001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.discovery.resource.names;
018    
019    import org.apache.commons.discovery.ResourceNameDiscover;
020    import org.apache.commons.discovery.ResourceNameIterator;
021    
022    /**
023     * Helper class for methods implementing the ResourceNameDiscover interface.
024     */
025    public abstract class ResourceNameDiscoverImpl implements ResourceNameDiscover {
026    
027        /**
028         * Locate names of resources that are bound to {@code resourceName}.
029         *
030         * @param resourceName The resource name to locate
031         * @return A new {@link ResourceNameIterator}
032         */
033        public abstract ResourceNameIterator findResourceNames(String resourceName);
034    
035        /**
036         * Locate names of resources that are bound to {@code inputNames}.
037         *
038         * @param inputNames The resource names to locate
039         * @return A new {@link ResourceNameIterator}
040         */
041        public ResourceNameIterator findResourceNames(final ResourceNameIterator inputNames) {
042            return new ResourceNameIterator() {
043    
044                private ResourceNameIterator resourceNames = null;
045    
046                private String resourceName = null;
047    
048                public boolean hasNext() {
049                    if (resourceName == null) {
050                        resourceName = getNextResourceName();
051                    }
052                    return resourceName != null;
053                }
054    
055                public String nextResourceName() {
056                    String name = resourceName;
057                    resourceName = null;
058                    return name;
059                }
060    
061                private String getNextResourceName() {
062                    while (inputNames.hasNext() && (resourceNames == null  ||  !resourceNames.hasNext())) {
063                        resourceNames =
064                            findResourceNames(inputNames.nextResourceName());
065                    }
066    
067                    return (resourceNames != null  &&  resourceNames.hasNext())
068                           ? resourceNames.nextResourceName()
069                           : null;
070                }
071            };
072        }
073    
074    }