01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
21: import java.util.ArrayList;
22: import java.util.Collection;
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 BAD_COUNT = "count of first resources should be set to an int >= 0";
33:
34: private int count = 1;
35:
36: /**
37: * Set the number of resources to be included.
38: * @param i the count as <code>int</count>.
39: */
40: public synchronized void setCount(int i) {
41: count = i;
42: }
43:
44: /**
45: * Get the number of resources to be included. Default is 1.
46: * @return the count as <code>int</count>.
47: */
48: public synchronized int getCount() {
49: return count;
50: }
51:
52: /**
53: * Take the first <code>count</code> elements.
54: * @return a Collection of Resources.
55: */
56: protected Collection getCollection() {
57: int ct = getCount();
58: if (ct < 0) {
59: throw new BuildException(BAD_COUNT);
60: }
61: Iterator iter = getResourceCollection().iterator();
62: ArrayList al = new ArrayList(ct);
63: for (int i = 0; i < ct && iter.hasNext(); i++) {
64: al.add(iter.next());
65: }
66: return al;
67: }
68:
69: }
|