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: */
018: package org.apache.tools.ant.types.resources;
019:
020: import java.io.File;
021: import java.util.Iterator;
022: import java.util.NoSuchElementException;
023:
024: /**
025: * Iterator of FileResources from filenames.
026: * @since Ant 1.7
027: */
028: public class FileResourceIterator implements Iterator {
029: private File basedir;
030: private String[] files;
031: private int pos = 0;
032:
033: /**
034: * Construct a new FileResourceIterator.
035: */
036: public FileResourceIterator() {
037: }
038:
039: /**
040: * Construct a new FileResourceIterator relative to the specified
041: * base directory.
042: * @param f the base directory of this instance.
043: */
044: public FileResourceIterator(File f) {
045: basedir = f;
046: }
047:
048: /**
049: * Construct a new FileResourceIterator over the specified filenames,
050: * relative to the specified base directory.
051: * @param f the base directory of this instance.
052: * @param s the String[] of filenames.
053: */
054: public FileResourceIterator(File f, String[] s) {
055: this (f);
056: addFiles(s);
057: }
058:
059: /**
060: * Add an array of filenames to this FileResourceIterator.
061: * @param s the filenames to add.
062: */
063: public void addFiles(String[] s) {
064: int start = (files == null) ? 0 : files.length;
065: String[] newfiles = new String[start + s.length];
066: if (start > 0) {
067: System.arraycopy(files, 0, newfiles, 0, start);
068: }
069: files = newfiles;
070: System.arraycopy(s, 0, files, start, s.length);
071: }
072:
073: /**
074: * Find out whether this FileResourceIterator has more elements.
075: * @return whether there are more Resources to iterate over.
076: */
077: public boolean hasNext() {
078: return pos < files.length;
079: }
080:
081: /**
082: * Get the next element from this FileResourceIterator.
083: * @return the next Object.
084: */
085: public Object next() {
086: return nextResource();
087: }
088:
089: /**
090: * Not implemented.
091: */
092: public void remove() {
093: throw new UnsupportedOperationException();
094: }
095:
096: /**
097: * Convenience method to return the next resource.
098: * @return the next File.
099: */
100: public FileResource nextResource() {
101: if (!hasNext()) {
102: throw new NoSuchElementException();
103: }
104: return new FileResource(basedir, files[pos++]);
105: }
106:
107: }
|