001package 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*/ 012public class Transition implements Comparable<Transition> { 013 /** 014 * Transition code 015 */ 016 private final int code; 017 /** 018 * Transition symbol 019 */ 020 private final String symbol; 021 /** 022 * <code>true</code> if the transition is labeled, otherwise <code>false</code> 023 */ 024 private final boolean labeled; 025 private final int cachedHash; 026 /** 027 * Creates a transition 028 * 029 * @param code Transition code 030 * @param symbol Transition name 031 * @param labeled <code>true</code> if the transition is labeled, otherwise <code>false</code> 032 */ 033 public Transition(int code, String symbol, boolean labeled) { 034 this.code = code; 035 this.symbol = symbol; 036 this.labeled = labeled; 037 final int prime = 31; 038 int result = prime + code; 039 result = prime * result + (labeled ? 1231 : 1237); 040 this.cachedHash = prime * result + ((symbol == null) ? 0 : symbol.hashCode()); 041 } 042 043 /** 044 * Returns the transition code 045 * 046 * @return the transition code 047 */ 048 public int getCode() { 049 return code; 050 } 051 052 /** 053 * Returns the transition symbol 054 * 055 * @return the transition symbol 056 */ 057 public String getSymbol() { 058 return symbol; 059 } 060 061 /** 062 * Returns true if the transition is labeled, otherwise false 063 * 064 * @return <code>true</code> if the transition is labeled, otherwise <code>false</code> 065 */ 066 public boolean isLabeled() { 067 return labeled; 068 } 069 070 071 public int compareTo(Transition that) { 072 final int BEFORE = -1; 073 final int EQUAL = 0; 074 final int AFTER = 1; 075 if (this.code < that.code) return BEFORE; 076 if (this.code > that.code) return AFTER; 077 return EQUAL; 078 } 079 080 @Override 081 public int hashCode() { 082 return cachedHash; 083 } 084 085 @Override 086 public boolean equals(Object obj) { 087 if (this == obj) 088 return true; 089 if (obj == null) 090 return false; 091 if (getClass() != obj.getClass()) 092 return false; 093 Transition other = (Transition) obj; 094 if (code != other.code) 095 return false; 096 if (labeled != other.labeled) 097 return false; 098 if (symbol == null) { 099 if (other.symbol != null) 100 return false; 101 } else if (!symbol.equals(other.symbol)) 102 return false; 103 return true; 104 } 105 106 /* (non-Javadoc) 107 * @see java.lang.Object#toString() 108 */ 109 public String toString() { 110 return symbol + " [" + code +"] " + labeled; 111 } 112}