KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23
24 import org.apache.tools.ant.BuildException;
25
26 /**
27  * ResourceCollection that contains the first <code>count</code> elements of
28  * another ResourceCollection.
29  * @since Ant 1.7
30  */

31 public class First extends BaseResourceCollectionWrapper {
32     private static final String JavaDoc BAD_COUNT
33         = "count of first resources should be set to an int >= 0";
34
35     private int count = 1;
36
37     /**
38      * Set the number of resources to be included.
39      * @param i the count as <code>int</count>.
40      */

41     public synchronized void setCount(int i) {
42         count = i;
43     }
44
45     /**
46      * Get the number of resources to be included. Default is 1.
47      * @return the count as <code>int</count>.
48      */

49     public synchronized int getCount() {
50         return count;
51     }
52
53     /**
54      * Take the first <code>count</code> elements.
55      * @return a Collection of Resources.
56      */

57     protected Collection JavaDoc getCollection() {
58         int ct = getCount();
59         if (ct < 0) {
60             throw new BuildException(BAD_COUNT);
61         }
62         Iterator JavaDoc iter = getResourceCollection().iterator();
63         ArrayList JavaDoc al = new ArrayList JavaDoc(ct);
64         for (int i = 0; i < ct && iter.hasNext(); i++) {
65             al.add(iter.next());
66         }
67         return al;
68     }
69
70 }
71
Popular Tags