Browse Source

new resource collection "allbutfirst"

first part of https://bz.apache.org/bugzilla/show_bug.cgi?id=57834 -
allbutlast to follow
master
Stefan Bodewig 10 years ago
parent
commit
989ca3b787
3 changed files with 129 additions and 1 deletions
  1. +1
    -0
      src/main/org/apache/tools/ant/types/defaults.properties
  2. +60
    -0
      src/main/org/apache/tools/ant/types/resources/AllButFirst.java
  3. +68
    -1
      src/tests/antunit/types/resources/first-last-test.xml

+ 1
- 0
src/main/org/apache/tools/ant/types/defaults.properties View File

@@ -71,6 +71,7 @@ difference=org.apache.tools.ant.types.resources.Difference
intersect=org.apache.tools.ant.types.resources.Intersect intersect=org.apache.tools.ant.types.resources.Intersect
sort=org.apache.tools.ant.types.resources.Sort sort=org.apache.tools.ant.types.resources.Sort
resources=org.apache.tools.ant.types.resources.Resources resources=org.apache.tools.ant.types.resources.Resources
allbutfirst=org.apache.tools.ant.types.resources.AllButFirst
first=org.apache.tools.ant.types.resources.First first=org.apache.tools.ant.types.resources.First
last=org.apache.tools.ant.types.resources.Last last=org.apache.tools.ant.types.resources.Last
tarfileset=org.apache.tools.ant.types.TarFileSet tarfileset=org.apache.tools.ant.types.TarFileSet


+ 60
- 0
src/main/org/apache/tools/ant/types/resources/AllButFirst.java View File

@@ -0,0 +1,60 @@
/*
* 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.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.tools.ant.types.Resource;

/**
* ResourceCollection that contains all resources of another
* collection except for the first <code>count</code> elements, a la
* the UNIX tail command with parameter <code>-n +count</code>.
* @since Ant 1.9.5
*/
public class AllButFirst extends SizeLimitCollection {

/**
* Take the first <code>count</code> elements.
* @return a Collection of Resources.
*/
protected Collection<Resource> getCollection() {
int ct = getValidCount();
Iterator<Resource> iter = getResourceCollection().iterator();
List<Resource> al = new ArrayList<Resource>();
for (int i = 0; i < ct && iter.hasNext(); i++) {
// discard
iter.next();
}
while (iter.hasNext()) {
al.add(iter.next());
}
return al;
}

@Override
public synchronized int size() {
int sz = getResourceCollection().size();
int ct = getValidCount();
return sz > ct ? sz - ct : 0;
}

}

+ 68
- 1
src/tests/antunit/types/resources/first-last-test.xml View File

@@ -15,7 +15,9 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
--> -->
<project xmlns:au="antlib:org.apache.ant.antunit">
<project xmlns:au="antlib:org.apache.ant.antunit" default="antunit">

<import file="../../antunit-base.xml" />


<tokens id="testrc"> <tokens id="testrc">
<string value="1,2,3,4,5" /> <string value="1,2,3,4,5" />
@@ -154,4 +156,69 @@
</au:expectfailure> </au:expectfailure>
</target> </target>


<target name="testallbutfirst5">
<au:assertTrue>
<resourcecount count="0">
<allbutfirst count="5"><resources refid="testrc" /></allbutfirst>
</resourcecount>
</au:assertTrue>
</target>

<target name="testallbutfirst4">
<au:assertTrue>
<resourcecount count="0">
<difference>
<allbutfirst count="4"><resources refid="testrc" /></allbutfirst>
<string value="5" />
</difference>
</resourcecount>
</au:assertTrue>
</target>

<target name="testallbutfirst3">
<au:assertTrue>
<resourcecount count="0">
<difference>
<allbutfirst count="3"><resources refid="testrc" /></allbutfirst>
<resources>
<string value="4" />
<string value="5" />
</resources>
</difference>
</resourcecount>
</au:assertTrue>
</target>

<target name="testallbutfirst">
<au:assertTrue>
<resourcecount count="0">
<difference>
<allbutfirst><resources refid="testrc" /></allbutfirst>
<resources>
<string value="2" />
<string value="3" />
<string value="4" />
<string value="5" />
</resources>
</difference>
</resourcecount>
</au:assertTrue>
</target>

<target name="testallbutfirst6">
<au:assertTrue>
<resourcecount count="0">
<allbutfirst count="6"><resources refid="testrc" /></allbutfirst>
</resourcecount>
</au:assertTrue>
</target>

<target name="testallbutfirst-1">
<au:expectfailure expectedmessage="size-limited collection count should be set to an int &gt;= 0">
<resourcecount>
<allbutfirst count="-1"><resources refid="testrc" /></allbutfirst>
</resourcecount>
</au:expectfailure>
</target>

</project> </project>

Loading…
Cancel
Save