001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.kernel;
019:
020: /**
021: * Abstract super class for predicates. Mainly <code>equals()</code>
022: * and <code>hashCode()</code> are implemented here. Implementations of <code>Predicate</code>
023: * should consider to subclass this class in order to use these implementations.
024: * The idea is to allow also instances of different classes to be equal, since these instances
025: * could for example represent the same predicate using different data sources.<p>
026: * Note that properties of instances of this class are inmutable. This can be used to cache the hash!
027: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
028: * @version 3.4 <7 March 05>
029: * @since 1.0
030: */
031: public abstract class AbstractPredicate implements Predicate {
032:
033: protected int cachedHashCode = -1;
034:
035: /**
036: * Indicates whether the two objects are equal. This method invokes
037: * comparing the names and checking the structure types for compatibility (and not for
038: * identity!).
039: * @param obj the object to compare this predicate with
040: * @return true if the objects are equal, false otherwise
041: */
042: public boolean equals(Object obj) {
043: if (obj instanceof org.mandarax.kernel.Predicate) {
044: if (toString().equals("equalsNot")
045: && obj.toString().equals("equalsNot")) {
046: System.out.println("stop here");
047: }
048:
049: Predicate p = (Predicate) obj;
050: Class[] c1 = p.getStructure();
051: Class[] c2 = getStructure();
052: boolean result = p.getName().equals(getName());
053:
054: result = result && (c1.length == c2.length);
055:
056: for (int i = 0; i < c1.length; i++) {
057: result = result
058: && ((c1[i] == c2[i])
059: || (c1[i].isAssignableFrom(c2[i])) || (c2[i]
060: .isAssignableFrom(c1[i])));
061: }
062:
063: // test method
064: // if (result) System.out.println(this.toString() + " = " + obj.toString());
065: if (toString().equals("equalsNot")
066: && obj.toString().equals("equalsNot") && !result) {
067: System.out
068: .println("------------------------------------------");
069:
070: if (!result) {
071: System.out.println(this .toString() + " != "
072: + obj.toString());
073: }
074:
075: for (int i1 = 0; i1 < c1.length; i1++) {
076: System.out.println(c1[i1].toString());
077: }
078:
079: System.out.println("");
080:
081: for (int i2 = 0; i2 < c2.length; i2++) {
082: System.out.println(c2[i2].toString());
083: }
084: }
085:
086: return result;
087: }
088:
089: return false;
090: }
091:
092: /**
093: * Get the name of the predicate.
094: * @return the name of the predicate
095: */
096: public abstract String getName();
097:
098: /**
099: * Get the type structure of the object, e.g. the types of terms.
100: * @return the structure of the predicate
101: */
102: public abstract Class[] getStructure();
103:
104: /**
105: * Get the hash code of the object.
106: * @return the hash code value
107: */
108: public int hashCode() {
109: if (cachedHashCode == -1) {
110: if (getName() == null) {
111: cachedHashCode = 0;
112: } else {
113: cachedHashCode = getName().hashCode()
114: + getStructure().length;
115: }
116: }
117:
118: return cachedHashCode;
119: }
120: }
|