1// BEGIN LICENSE BLOCK 2// Version: CMPL 1.1 3// 4// The contents of this file are subject to the Cisco-style Mozilla Public 5// License Version 1.1 (the "License"); you may not use this file except 6// in compliance with the License. You may obtain a copy of the License 7// at www.eclipse-clp.org/license. 8// 9// Software distributed under the License is distributed on an "AS IS" 10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 11// the License for the specific language governing rights and limitations 12// under the License. 13// 14// The Original Code is The ECLiPSe Constraint Logic Programming System. 15// The Initial Developer of the Original Code is Cisco Systems, Inc. 16// Portions created by the Initial Developer are 17// Copyright (C) 2000 - 2006 Cisco Systems, Inc. All Rights Reserved. 18// 19// Contributor(s): Stefano Novello / Josh Singer, Parc Technologies 20// 21// END LICENSE BLOCK 22 23//Title: Java/ECLiPSe interface 24//Version: $Id: AbstractCompoundTerm.java,v 1.1 2006/09/23 01:54:08 snovello Exp $ 25//Author: Stefano Novello / Josh Singer 26//Company: Parc Technologies 27//Description: Superclass for user-defined objects which implement CompoundTerm. 28package com.parctechnologies.eclipse; 29 30/** 31 * A useful superclass for objects implementing the 32 * {@link CompoundTerm} interface. <p> 33 * Subclass <i>AbstractCompoundTerm</i> if you are creating a class which implements 34 * {@link CompoundTerm}. This abstract class provides some of the methods required. 35 * @see CompoundTerm 36 * @see CompoundTermImpl 37 */ 38public abstract class AbstractCompoundTerm implements CompoundTerm 39{ 40 /** 41 * The functor. 42 */ 43 private String functor; 44 45 /** 46 * The arity. 47 */ 48 private int arity; 49 50 /** 51 * Construct an <i>AbstractCompoundTerm</i> with a given functor and arity. 52 */ 53 public AbstractCompoundTerm(String functor, int arity) 54 { 55 this.functor = functor; 56 this.arity = arity; 57 } 58 59 /** 60 * Returns the functor. 61 */ 62 public String functor() 63 { 64 return functor; 65 } 66 67 /** 68 * Return the arity. 69 */ 70 public int arity() 71 { 72 return arity; 73 } 74 75 76 /** 77 * Overrides <code>equals()</code> in <i>java.lang.Object</i>. Returns true 78 * iff the parameter Object implements CompoundTerm and its functor and arity 79 * are equal to this object's and pairwise invocations of <code>equals()</code> 80 * return true between each of this object's arguments and the corresponding 81 * argument of the parameter object. 82 */ 83 public boolean equals(Object obj) 84 { 85 if(!(obj instanceof CompoundTerm)) 86 { 87 return(false); 88 } 89 CompoundTerm ct = (CompoundTerm) obj; 90 if(!ct.functor().equals(this.functor())) 91 { 92 return(false); 93 } 94 if(ct.arity() != this.arity()) 95 { 96 return(false); 97 } 98 for(int i = 1; i <= ct.arity(); i++) 99 { 100 if(!this.arg(i).equals(ct.arg(i))) 101 { 102 return(false); 103 } 104 } 105 return(true); 106 } 107 108 // overrides Object: two of these which are equals will have equals functors 109 // and args and therefore the same hashcode. 110 public int hashCode() 111 { 112 int hashcode = functor().hashCode(); 113 for(int i = 1; i <= arity(); i++) 114 { 115 if(arg(i) != null) 116 { 117 hashcode += arg(i).hashCode(); 118 } 119 } 120 return(hashcode); 121 } 122 123} 124