KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > Sort


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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.Stack JavaDoc;
21 import java.util.TreeMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.AbstractCollection JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.types.DataType;
32 import org.apache.tools.ant.types.ResourceCollection;
33 import org.apache.tools.ant.types.resources.comparators.ResourceComparator;
34 import org.apache.tools.ant.types.resources.comparators.DelegatedResourceComparator;
35
36 /**
37  * ResourceCollection that sorts another ResourceCollection.
38  * @since Ant 1.7
39  */

40 public class Sort extends BaseResourceCollectionWrapper {
41
42     //sorted bag impl. borrowed from commons-collections TreeBag:
43
private static class SortedBag extends AbstractCollection JavaDoc {
44         private class MutableInt {
45             private int value = 0;
46         }
47         private class MyIterator implements Iterator JavaDoc {
48             private Iterator JavaDoc keyIter = t.keySet().iterator();
49             private Object JavaDoc current;
50             private int occurrence;
51             public synchronized boolean hasNext() {
52                 return occurrence > 0 || keyIter.hasNext();
53             }
54             public synchronized Object JavaDoc next() {
55                 if (!hasNext()) {
56                     throw new NoSuchElementException JavaDoc();
57                 }
58                 if (occurrence == 0) {
59                     current = keyIter.next();
60                     occurrence = ((MutableInt) t.get(current)).value;
61                 }
62                 --occurrence;
63                 return current;
64             }
65             public void remove() {
66                 throw new UnsupportedOperationException JavaDoc();
67             }
68         }
69         private TreeMap JavaDoc t;
70         private int size;
71
72         SortedBag(Comparator JavaDoc c) {
73             t = new TreeMap JavaDoc(c);
74         }
75         public synchronized Iterator JavaDoc iterator() {
76             return new MyIterator();
77         }
78         public synchronized boolean add(Object JavaDoc o) {
79             if (size < Integer.MAX_VALUE) {
80                 ++size;
81             }
82             MutableInt m = (MutableInt) (t.get(o));
83             if (m == null) {
84                 m = new MutableInt();
85                 t.put(o, m);
86             }
87             m.value++;
88             return true;
89         }
90         public synchronized int size() {
91             return size;
92         }
93     }
94
95     private DelegatedResourceComparator comp = new DelegatedResourceComparator();
96
97     /**
98      * Sort the contained elements.
99      * @return a Collection of Resources.
100      */

101     protected synchronized Collection JavaDoc getCollection() {
102         ResourceCollection rc = getResourceCollection();
103         Iterator JavaDoc iter = rc.iterator();
104         if (!(iter.hasNext())) {
105             return Collections.EMPTY_SET;
106         }
107         SortedBag b = new SortedBag(comp);
108         while (iter.hasNext()) {
109             b.add(iter.next());
110         }
111         return b;
112     }
113
114     /**
115      * Add a ResourceComparator to this Sort ResourceCollection.
116      * If multiple ResourceComparators are added, they will be processed in LIFO order.
117      * @param c the ResourceComparator to add.
118      */

119     public synchronized void add(ResourceComparator c) {
120         if (isReference()) {
121             throw noChildrenAllowed();
122         }
123         comp.add(c);
124         FailFast.invalidate(this);
125     }
126
127     /**
128      * Overrides the BaseResourceCollectionContainer version
129      * to recurse on nested ResourceComparators.
130      * @param stk the stack of data types to use (recursively).
131      * @param p the project to use to dereference the references.
132      * @throws BuildException on error.
133      */

134     protected synchronized void dieOnCircularReference(Stack JavaDoc stk, Project p)
135         throws BuildException {
136         if (isChecked()) {
137             return;
138         }
139         if (isReference()) {
140             super.dieOnCircularReference(stk, p);
141         } else {
142             DataType.invokeCircularReferenceCheck(comp, stk, p);
143             setChecked(true);
144         }
145     }
146
147 }
148
Popular Tags