001    /*
002     * Created on Sep 14, 2003 by Ravi Mohan
003     *  
004     */
005    package aima.logic.fol.parsing.ast;
006    
007    import java.util.ArrayList;
008    import java.util.Collections;
009    import java.util.List;
010    
011    import aima.logic.fol.parsing.FOLVisitor;
012    
013    /**
014     * @author Ravi Mohan
015     * @author Ciaran O'Reilly
016     */
017    public class TermEquality implements AtomicSentence {
018            private Term term1, term2;
019            private List<Term> terms = new ArrayList<Term>();
020            private String stringRep = null;
021            private int hashCode = 0;
022    
023            public static String getEqualitySynbol() {
024                    return "=";
025            }
026    
027            public TermEquality(Term term1, Term term2) {
028                    this.term1 = term1;
029                    this.term2 = term2;
030                    terms.add(term1);
031                    terms.add(term2);
032            }
033    
034            public Term getTerm1() {
035                    return term1;
036            }
037    
038            public Term getTerm2() {
039                    return term2;
040            }
041    
042            //
043            // START-AtomicSentence
044            public String getSymbolicName() {
045                    return getEqualitySynbol();
046            }
047    
048            public boolean isCompound() {
049                    return true;
050            }
051    
052            public List<Term> getArgs() {
053                    return Collections.unmodifiableList(terms);
054            }
055    
056            public Object accept(FOLVisitor v, Object arg) {
057                    return v.visitTermEquality(this, arg);
058            }
059    
060            public TermEquality copy() {
061                    return new TermEquality(term1.copy(), term2.copy());
062            }
063    
064            // END-AtomicSentence
065            //
066    
067            @Override
068            public boolean equals(Object o) {
069    
070                    if (this == o) {
071                            return true;
072                    }
073                    if ((o == null) || (this.getClass() != o.getClass())) {
074                            return false;
075                    }
076                    TermEquality te = (TermEquality) o;
077    
078                    return te.getTerm1().equals(term1) && te.getTerm2().equals(term2);
079            }
080    
081            @Override
082            public int hashCode() {
083                    if (0 == hashCode) {
084                            hashCode = 17;
085                            hashCode = 37 * hashCode + getTerm1().hashCode();
086                            hashCode = 37 * hashCode + getTerm2().hashCode();
087                    }
088                    return hashCode;
089            }
090    
091            @Override
092            public String toString() {
093                    if (null == stringRep) {
094                            StringBuilder sb = new StringBuilder();
095                            sb.append(term1.toString());
096                            sb.append(" = ");
097                            sb.append(term2.toString());
098                            stringRep = sb.toString();
099                    }
100                    return stringRep;
101            }
102    }