--- Kev Jackson <[EMAIL PROTECTED]> wrote: > If you need to get the first X elements, presumably > there will be > occasions where you'll need to get the last X > elements. Anyway, I've > been playing with Haskell recently and it's affected > my thinking :)
Hi Kev, I've been (mostly) away for a few days. My thinking was that anytime you wanted to get the first N elements, you would want to sort them to ensure you got what you intended and so you could get the last N by simply reversing the sort. So this could come down to an informal "vote" for all I care. If we end up with first & last, we should also consider renaming to "head" & "tail" as suggested by (IIRC) Jan or Antoine. -Matt > > Kev > > Index: > D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/types/resources/Last.java > =================================================================== > --- > D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/types/resources/Last.java > (revision 0) > +++ > D:/java_projects/ant-core-trunk/src/main/org/apache/tools/ant/types/resources/Last.java > (revision 0) > @@ -0,0 +1,70 @@ > +/* > + * Copyright 2005 The Apache Software Foundation > + * > + * Licensed 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.ArrayList; > +import java.util.Collection; > +import java.util.Iterator; > + > +import org.apache.tools.ant.BuildException; > + > +/** > + * ResourceCollection that contains the last > <code>count</code> elements of > + * another ResourceCollection. > + * @since Ant 1.7 > + */ > +public class Last 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 <code>int</count>. > + */ > + public synchronized void setCount(int i) { > + count = i; > + } > + > + /** > + * Get the number of resources to be included. > Default is 1. > + * @return the count as <code>int</count>. > + */ > + public synchronized int getCount() { > + return count; > + } > + > + protected Collection getCollection() { > + int ct = getCount(); > + if (ct < 0) { > + throw new BuildException(BAD_COUNT); > + } > + Iterator iter = > getResourceCollection().iterator(); > + ArrayList al = new ArrayList(ct); > + // note logic here reverses the order, so > we have > + // to add each element at it's original > position > + // so first element selected will be the > last > + // element from the resorce collection, and > should > + // be placed in the last position of the > resulting > + // ArrayList > + for (int i = ct; i > 0 && iter.hasNext(); > i--) { > + al.add(i, iter.next()); > + } > + return al; > + } > +} > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: > [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] __________________________________ Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
