001 package org.maltparser.parser.transition; 002 003 004 /** 005 * Transition contains one individual transition. For example, Nivre arc-eager algorithms have the unlabeled 006 * transition <code>SH</code>, <code>RE</code> and the labeled transition<code>RA</code>, <code>LA</code>. These 007 * transition will be four individual transition. 008 * 009 * @author Joakim Nivre 010 * @author Johan Hall 011 * @since 1.0 012 */ 013 public class Transition implements Comparable<Transition> { 014 /** 015 * Transition code 016 */ 017 private int code; 018 /** 019 * Transition symbol 020 */ 021 private String symbol; 022 /** 023 * <code>true</code> if the transition is labeled, otherwise <code>false</code> 024 */ 025 private boolean labeled; 026 private int cachedHash; 027 /** 028 * Creates a transition 029 * 030 * @param code Transition code 031 * @param symbol Transition name 032 * @param labeled <code>true</code> if the transition is labeled, otherwise <code>false</code> 033 */ 034 public Transition(int code, String symbol, boolean labeled) { 035 this.code = code; 036 this.symbol = symbol; 037 this.labeled = labeled; 038 } 039 040 /** 041 * Returns the transition code 042 * 043 * @return the transition code 044 */ 045 public int getCode() { 046 return code; 047 } 048 049 /** 050 * Returns the transition symbol 051 * 052 * @return the transition symbol 053 */ 054 public String getSymbol() { 055 return symbol; 056 } 057 058 /** 059 * Returns true if the transition is labeled, otherwise false 060 * 061 * @return <code>true</code> if the transition is labeled, otherwise <code>false</code> 062 */ 063 public boolean isLabeled() { 064 return labeled; 065 } 066 067 068 public int compareTo(Transition that) { 069 final int BEFORE = -1; 070 final int EQUAL = 0; 071 final int AFTER = 1; 072 // if ( this == that ) return EQUAL; 073 if (this.code < that.code) return BEFORE; 074 if (this.code > that.code) return AFTER; 075 return EQUAL; 076 } 077 078 public boolean equals(Object obj) { 079 if (this == obj) 080 return true; 081 if (obj == null) 082 return false; 083 if (getClass() != obj.getClass()) 084 return false; 085 Transition t = (Transition)obj; 086 return (code == t.code && symbol.equals(t.symbol) && labeled == t.isLabeled()); 087 } 088 089 public int hashCode() { 090 if (cachedHash == 0) { 091 int hash = 31*7 + code; 092 hash = 31*hash + (null == symbol ? 0 : symbol.hashCode()); 093 hash = 31*hash + (labeled ? 1 : 0); 094 cachedHash = hash; 095 } 096 return cachedHash; 097 } 098 099 100 /* (non-Javadoc) 101 * @see java.lang.Object#toString() 102 */ 103 public String toString() { 104 return symbol + " [" + code +"] " + labeled; 105 } 106 }