KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > comparators > DelegatedResourceComparator


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.comparators;
19
20 import java.util.Stack JavaDoc;
21 import java.util.Vector JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.types.DataType;
27 import org.apache.tools.ant.types.Resource;
28
29 /**
30  * Delegates to other ResourceComparators or, if none specified,
31  * uses Resources' natural ordering.
32  * @since Ant 1.7
33  */

34 public class DelegatedResourceComparator extends ResourceComparator {
35
36     private Vector JavaDoc v = null;
37
38     /**
39      * Add a delegate ResourceComparator.
40      * @param c the next delegate ResourceComparator.
41      */

42     public synchronized void add(ResourceComparator c) {
43         if (isReference()) {
44             throw noChildrenAllowed();
45         }
46         if (c == null) {
47             return;
48         }
49         v = (v == null) ? new Vector JavaDoc() : v;
50         v.add(c);
51     }
52
53     /**
54      * Equality method based on the vector of resources,
55      * or if a reference, the referredto object.
56      * @param o the object to check against.
57      * @return true if there is equality.
58      */

59     public synchronized boolean equals(Object JavaDoc o) {
60         if (o == this) {
61             return true;
62         }
63         if (isReference()) {
64             return getCheckedRef().equals(o);
65         }
66         if (!(o instanceof DelegatedResourceComparator)) {
67             return false;
68         }
69         Vector JavaDoc ov = ((DelegatedResourceComparator) o).v;
70         return v == null ? ov == null : v.equals(ov);
71     }
72
73     /**
74      * Hashcode based on the rules for equality.
75      * @return a hashcode.
76      */

77     public synchronized int hashCode() {
78         if (isReference()) {
79             return getCheckedRef().hashCode();
80         }
81         return v == null ? 0 : v.hashCode();
82     }
83
84     /** {@inheritDoc} */
85     protected synchronized int resourceCompare(Resource foo, Resource bar) {
86         //if no nested, natural order:
87
if (v == null || v.isEmpty()) {
88             return foo.compareTo(bar);
89         }
90         int result = 0;
91         for (Iterator JavaDoc i = v.iterator(); result == 0 && i.hasNext();) {
92             result = ((ResourceComparator) i.next()).resourceCompare(foo, bar);
93         }
94         return result;
95     }
96
97     /**
98      * Overrides the version from DataType to recurse on nested ResourceSelector
99 s.
100      * @param stk the Stack of references.
101      * @param p the Project to resolve against.
102      * @throws BuildException on error.
103      */

104     protected void dieOnCircularReference(Stack JavaDoc stk, Project p)
105         throws BuildException {
106         if (isChecked()) {
107             return;
108         }
109         if (isReference()) {
110             super.dieOnCircularReference(stk, p);
111         } else {
112             if (!(v == null || v.isEmpty())) {
113                 for (Iterator JavaDoc i = v.iterator(); i.hasNext();) {
114                     Object JavaDoc o = i.next();
115                     if (o instanceof DataType) {
116                         stk.push(o);
117                         invokeCircularReferenceCheck((DataType) o, stk, p);
118                     }
119                 }
120             }
121             setChecked(true);
122         }
123     }
124 }
125
Popular Tags