001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.resources.comparators;
019:
020: import java.util.Stack;
021: import java.util.Vector;
022: import java.util.Iterator;
023:
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.Project;
026: import org.apache.tools.ant.types.DataType;
027: import org.apache.tools.ant.types.Resource;
028:
029: /**
030: * Delegates to other ResourceComparators or, if none specified,
031: * uses Resources' natural ordering.
032: * @since Ant 1.7
033: */
034: public class DelegatedResourceComparator extends ResourceComparator {
035:
036: private Vector v = null;
037:
038: /**
039: * Add a delegate ResourceComparator.
040: * @param c the next delegate ResourceComparator.
041: */
042: public synchronized void add(ResourceComparator c) {
043: if (isReference()) {
044: throw noChildrenAllowed();
045: }
046: if (c == null) {
047: return;
048: }
049: v = (v == null) ? new Vector() : v;
050: v.add(c);
051: }
052:
053: /**
054: * Equality method based on the vector of resources,
055: * or if a reference, the referredto object.
056: * @param o the object to check against.
057: * @return true if there is equality.
058: */
059: public synchronized boolean equals(Object o) {
060: if (o == this ) {
061: return true;
062: }
063: if (isReference()) {
064: return getCheckedRef().equals(o);
065: }
066: if (!(o instanceof DelegatedResourceComparator)) {
067: return false;
068: }
069: Vector ov = ((DelegatedResourceComparator) o).v;
070: return v == null ? ov == null : v.equals(ov);
071: }
072:
073: /**
074: * Hashcode based on the rules for equality.
075: * @return a hashcode.
076: */
077: public synchronized int hashCode() {
078: if (isReference()) {
079: return getCheckedRef().hashCode();
080: }
081: return v == null ? 0 : v.hashCode();
082: }
083:
084: /** {@inheritDoc} */
085: protected synchronized int resourceCompare(Resource foo,
086: Resource bar) {
087: //if no nested, natural order:
088: if (v == null || v.isEmpty()) {
089: return foo.compareTo(bar);
090: }
091: int result = 0;
092: for (Iterator i = v.iterator(); result == 0 && i.hasNext();) {
093: result = ((ResourceComparator) i.next()).resourceCompare(
094: foo, bar);
095: }
096: return result;
097: }
098:
099: /**
100: * Overrides the version from DataType to recurse on nested ResourceSelector
101: s.
102: * @param stk the Stack of references.
103: * @param p the Project to resolve against.
104: * @throws BuildException on error.
105: */
106: protected void dieOnCircularReference(Stack stk, Project p)
107: throws BuildException {
108: if (isChecked()) {
109: return;
110: }
111: if (isReference()) {
112: super .dieOnCircularReference(stk, p);
113: } else {
114: if (!(v == null || v.isEmpty())) {
115: for (Iterator i = v.iterator(); i.hasNext();) {
116: Object o = i.next();
117: if (o instanceof DataType) {
118: stk.push(o);
119: invokeCircularReferenceCheck((DataType) o, stk,
120: p);
121: }
122: }
123: }
124: setChecked(true);
125: }
126: }
127: }
|