Browse Source

PR 46527; sort losing equal entries

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@734866 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 16 years ago
parent
commit
2c7748a3fc
2 changed files with 8 additions and 62 deletions
  1. +3
    -0
      WHATSNEW
  2. +5
    -62
      src/main/org/apache/tools/ant/types/resources/Sort.java

+ 3
- 0
WHATSNEW View File

@@ -326,6 +326,9 @@ Fixed bugs:
a *. a *.
Bugzilla Report 46506. Bugzilla Report 46506.


* <sort> resource collection kept only one of entries deemed equal by
the chosen Comparator. Bugzilla report 46527.

Other changes: Other changes:
-------------- --------------




+ 5
- 62
src/main/org/apache/tools/ant/types/resources/Sort.java View File

@@ -17,14 +17,11 @@
*/ */
package org.apache.tools.ant.types.resources; package org.apache.tools.ant.types.resources;


import java.util.List;
import java.util.Stack; import java.util.Stack;
import java.util.TreeMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.Collections; import java.util.Collections;
import java.util.AbstractCollection;
import java.util.NoSuchElementException;


import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
@@ -32,6 +29,7 @@ import org.apache.tools.ant.types.DataType;
import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.comparators.ResourceComparator; import org.apache.tools.ant.types.resources.comparators.ResourceComparator;
import org.apache.tools.ant.types.resources.comparators.DelegatedResourceComparator; import org.apache.tools.ant.types.resources.comparators.DelegatedResourceComparator;
import org.apache.tools.ant.util.CollectionUtils;


/** /**
* ResourceCollection that sorts another ResourceCollection. * ResourceCollection that sorts another ResourceCollection.
@@ -43,59 +41,6 @@ import org.apache.tools.ant.types.resources.comparators.DelegatedResourceCompara
*/ */
public class Sort extends BaseResourceCollectionWrapper { public class Sort extends BaseResourceCollectionWrapper {


//sorted bag impl. borrowed from commons-collections TreeBag:
private static class SortedBag extends AbstractCollection {
private class MutableInt {
private int value = 0;
}
private class MyIterator implements Iterator {
private Iterator keyIter = t.keySet().iterator();
private Object current;
private int occurrence;
public synchronized boolean hasNext() {
return occurrence > 0 || keyIter.hasNext();
}
public synchronized Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
if (occurrence == 0) {
current = keyIter.next();
occurrence = ((MutableInt) t.get(current)).value;
}
--occurrence;
return current;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
private TreeMap t;
private int size;

SortedBag(Comparator c) {
t = new TreeMap(c);
}
public synchronized Iterator iterator() {
return new MyIterator();
}
public synchronized boolean add(Object o) {
if (size < Integer.MAX_VALUE) {
++size;
}
MutableInt m = (MutableInt) (t.get(o));
if (m == null) {
m = new MutableInt();
t.put(o, m);
}
m.value++;
return true;
}
public synchronized int size() {
return size;
}
}

private DelegatedResourceComparator comp = new DelegatedResourceComparator(); private DelegatedResourceComparator comp = new DelegatedResourceComparator();


/** /**
@@ -108,11 +53,9 @@ public class Sort extends BaseResourceCollectionWrapper {
if (!(iter.hasNext())) { if (!(iter.hasNext())) {
return Collections.EMPTY_SET; return Collections.EMPTY_SET;
} }
SortedBag b = new SortedBag(comp);
while (iter.hasNext()) {
b.add(iter.next());
}
return b;
List result = (List) CollectionUtils.asCollection(iter);
Collections.sort(result, comp);
return result;
} }


/** /**


Loading…
Cancel
Save