01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.Comparator;
21:
22: import org.apache.tools.ant.types.DataType;
23: import org.apache.tools.ant.types.Resource;
24:
25: /**
26: * Abstract Resource Comparator.
27: * @since Ant 1.7
28: */
29: public abstract class ResourceComparator extends DataType implements
30: Comparator {
31:
32: /**
33: * Compare two objects.
34: * @param foo the first Object.
35: * @param bar the second Object.
36: * @return a negative integer, zero, or a positive integer as the first
37: * argument is less than, equal to, or greater than the second.
38: * @throws ClassCastException if either argument is null.
39: */
40: public final int compare(Object foo, Object bar) {
41: dieOnCircularReference();
42: ResourceComparator c = isReference() ? (ResourceComparator) getCheckedRef()
43: : this ;
44: return c.resourceCompare((Resource) foo, (Resource) bar);
45: }
46:
47: /**
48: * Test for equality with this ResourceComparator.
49: * @param o the Object to compare against.
50: * @return true if the specified Object equals this one.
51: */
52: public boolean equals(Object o) {
53: if (isReference()) {
54: return getCheckedRef().equals(o);
55: }
56: if (o == null) {
57: return false;
58: }
59: return o == this || o.getClass().equals(getClass());
60: }
61:
62: /**
63: * Hashcode based on the rules for equality.
64: * @return a hashcode.
65: */
66: public synchronized int hashCode() {
67: if (isReference()) {
68: return getCheckedRef().hashCode();
69: }
70: return getClass().hashCode();
71: }
72:
73: /**
74: * Compare two Resources.
75: * @param foo the first Resource.
76: * @param bar the second Resource.
77: * @return a negative integer, zero, or a positive integer as the first
78: * argument is less than, equal to, or greater than the second.
79: */
80: protected abstract int resourceCompare(Resource foo, Resource bar);
81:
82: }
|