KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > FileResourceIterator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.types.resources;
19
20 import java.io.File JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 /**
25  * Iterator of FileResources from filenames.
26  * @since Ant 1.7
27  */

28 public class FileResourceIterator implements Iterator JavaDoc {
29     private File JavaDoc basedir;
30     private String JavaDoc[] files;
31     private int pos = 0;
32
33     /**
34      * Construct a new FileResourceIterator.
35      */

36     public FileResourceIterator() {
37     }
38
39     /**
40      * Construct a new FileResourceIterator relative to the specified
41      * base directory.
42      * @param f the base directory of this instance.
43      */

44     public FileResourceIterator(File JavaDoc f) {
45         basedir = f;
46     }
47
48     /**
49      * Construct a new FileResourceIterator over the specified filenames,
50      * relative to the specified base directory.
51      * @param f the base directory of this instance.
52      * @param s the String[] of filenames.
53      */

54     public FileResourceIterator(File JavaDoc f, String JavaDoc[] s) {
55         this(f);
56         addFiles(s);
57     }
58
59     /**
60      * Add an array of filenames to this FileResourceIterator.
61      * @param s the filenames to add.
62      */

63     public void addFiles(String JavaDoc[] s) {
64         int start = (files == null) ? 0 : files.length;
65         String JavaDoc[] newfiles = new String JavaDoc[start + s.length];
66         if (start > 0) {
67             System.arraycopy(files, 0, newfiles, 0, start);
68         }
69         files = newfiles;
70         System.arraycopy(s, 0, files, start, s.length);
71     }
72
73     /**
74      * Find out whether this FileResourceIterator has more elements.
75      * @return whether there are more Resources to iterate over.
76      */

77     public boolean hasNext() {
78         return pos < files.length;
79     }
80
81     /**
82      * Get the next element from this FileResourceIterator.
83      * @return the next Object.
84      */

85     public Object JavaDoc next() {
86         return nextResource();
87     }
88
89     /**
90      * Not implemented.
91      */

92     public void remove() {
93         throw new UnsupportedOperationException JavaDoc();
94     }
95
96     /**
97      * Convenience method to return the next resource.
98      * @return the next File.
99      */

100     public FileResource nextResource() {
101         if (!hasNext()) {
102             throw new NoSuchElementException JavaDoc();
103         }
104         return new FileResource(basedir, files[pos++]);
105     }
106
107 }
108
Popular Tags