diff --git a/WHATSNEW b/WHATSNEW
index 92d18168a..9f7eb7011 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -79,7 +79,9 @@ Other changes:
* If you try and use a type in a namespace (or an antlib), and the type is not
recognized but there are other definitions in that namespace, Ant lists what
- the known definitions are. This helps you find spelling errors.
+ the known definitions are. This helps you find spelling errors.
+
+* Add a The classpath can also be specified as nested classpath element,
where <classpath> is a path-like structure.
A single resource collection is required.
+Includes the last count resources from a nested resource collection. +This can be used in conjunction with the sort collection, +for example, to select the last few oldest, largest, etc. resources from a +larger collection.
++++
++ +Attribute +Description +Required ++ +count +The number of resources to include +No, default 1 ++ +cache +Whether to cache results; disabling + may seriously impact performance +No, default true +Parameters specified as nested elements
+A single resource collection is required.
+
Includes the string tokens gathered from a nested
resource collection. Uses the same tokenizers supported by the
diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties
index 5e1bcab50..3c74dda10 100644
--- a/src/main/org/apache/tools/ant/types/defaults.properties
+++ b/src/main/org/apache/tools/ant/types/defaults.properties
@@ -55,6 +55,7 @@ intersect=org.apache.tools.ant.types.resources.Intersect
sort=org.apache.tools.ant.types.resources.Sort
resources=org.apache.tools.ant.types.resources.Resources
first=org.apache.tools.ant.types.resources.First
+last=org.apache.tools.ant.types.resources.Last
tarfileset=org.apache.tools.ant.types.TarFileSet
tokens=org.apache.tools.ant.types.resources.Tokens
diff --git a/src/main/org/apache/tools/ant/types/resources/First.java b/src/main/org/apache/tools/ant/types/resources/First.java
index 1354484bf..96c270786 100644
--- a/src/main/org/apache/tools/ant/types/resources/First.java
+++ b/src/main/org/apache/tools/ant/types/resources/First.java
@@ -21,44 +21,19 @@ import java.util.Iterator;
import java.util.ArrayList;
import java.util.Collection;
-import org.apache.tools.ant.BuildException;
-
/**
* ResourceCollection that contains the first count
elements of
- * another ResourceCollection.
+ * another ResourceCollection, a la the UNIX head command.
* @since Ant 1.7
*/
-public class First extends BaseResourceCollectionWrapper {
- private static final String BAD_COUNT
- = "count of first resources should be set to an int >= 0";
-
- private int count = 1;
-
- /**
- * Set the number of resources to be included.
- * @param i the count as int.
- */
- public synchronized void setCount(int i) {
- count = i;
- }
-
- /**
- * Get the number of resources to be included. Default is 1.
- * @return the count as
int.
- */
- public synchronized int getCount() {
- return count;
- }
+public class First extends SizeLimitCollection {
/**
* Take the first
count
elements.
* @return a Collection of Resources.
*/
protected Collection getCollection() {
- int ct = getCount();
- if (ct < 0) {
- throw new BuildException(BAD_COUNT);
- }
+ int ct = getValidCount();
Iterator iter = getResourceCollection().iterator();
ArrayList al = new ArrayList(ct);
for (int i = 0; i < ct && iter.hasNext(); i++) {
diff --git a/src/main/org/apache/tools/ant/types/resources/Last.java b/src/main/org/apache/tools/ant/types/resources/Last.java
new file mode 100644
index 000000000..64b010804
--- /dev/null
+++ b/src/main/org/apache/tools/ant/types/resources/Last.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.tools.ant.types.resources;
+
+import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.ResourceCollection;
+
+/**
+ * ResourceCollection that contains the last count
elements of
+ * another ResourceCollection, a la the UNIX tail command.
+ * @since Ant 1.7.1
+ */
+public class Last extends SizeLimitCollection {
+
+ /**
+ * Take the last count
elements.
+ * @return a Collection of Resources.
+ */
+ protected Collection getCollection() {
+ int count = getValidCount();
+ ResourceCollection rc = getResourceCollection();
+ int i = count;
+ Iterator iter = rc.iterator();
+ int size = rc.size();
+ for (; i < size; i++) iter.next();
+
+ ArrayList al = new ArrayList(count);
+ for (; iter.hasNext(); i++) {
+ al.add(iter.next());
+ }
+ int found = al.size();
+ if (found == count || (size < count && found == size)) {
+ return al;
+ }
+
+ //mismatch:
+ String msg = "Resource collection " + rc + " reports size " + size
+ + " but returns " + i + " elements.";
+
+ //size was understated -> too many results; warn and continue:
+ if (found > count) {
+ log(msg, Project.MSG_WARN);
+ return al.subList(found - count, found);
+ }
+ //size was overstated; we missed some and are now in error-land:
+ throw new BuildException(msg);
+ }
+
+}
diff --git a/src/main/org/apache/tools/ant/types/resources/SizeLimitCollection.java b/src/main/org/apache/tools/ant/types/resources/SizeLimitCollection.java
new file mode 100644
index 000000000..aeb42ddf7
--- /dev/null
+++ b/src/main/org/apache/tools/ant/types/resources/SizeLimitCollection.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.tools.ant.types.resources;
+
+import org.apache.tools.ant.BuildException;
+
+/**
+ * ResourceCollection that imposes a size limit on another ResourceCollection.
+ * @since Ant 1.7.1
+ */
+public abstract class SizeLimitCollection extends BaseResourceCollectionWrapper {
+ private static final String BAD_COUNT
+ = "size-limited collection count should be set to an int >= 0";
+
+ private int count = 1;
+
+ /**
+ * Set the number of resources to be included.
+ * @param i the count as int.
+ */
+ public synchronized void setCount(int i) {
+ count = i;
+ }
+
+ /**
+ * Get the number of resources to be included. Default is 1.
+ * @return the count as
int.
+ */
+ public synchronized int getCount() {
+ return count;
+ }
+
+ /**
+ * Efficient size implementation.
+ * @return int size
+ */
+ public synchronized int size() {
+ int sz = getResourceCollection().size();
+ int ct = getValidCount();
+ return sz < ct ? sz : ct;
+ }
+
+ /**
+ * Get the count, verifying it is >= 0.
+ * @return int count
+ */
+ protected int getValidCount() {
+ int ct = getCount();
+ if (ct < 0) {
+ throw new BuildException(BAD_COUNT);
+ }
+ return ct;
+ }
+
+}
diff --git a/src/tests/antunit/types/resources/first-last-test.xml b/src/tests/antunit/types/resources/first-last-test.xml
new file mode 100644
index 000000000..8d753e2c4
--- /dev/null
+++ b/src/tests/antunit/types/resources/first-last-test.xml
@@ -0,0 +1,140 @@
+