001package org.maltparser.parser.history.container;
002
003import org.maltparser.core.exception.MaltChainedException;
004import org.maltparser.core.symbol.Table;
005/**
006*
007* @author Johan Hall
008**/
009public class TableContainer {
010        public enum RelationToNextDecision { COMBINED, SEQUANTIAL, BRANCHED, SWITCHED, NONE }
011        protected int cachedCode;
012        protected final StringBuilder cachedSymbol;
013        protected Table table;
014        protected String name;
015        private final RelationToNextDecision relationToNextDecision;
016        
017        public TableContainer(Table _table, String _name, char _decisionSeparator) {
018                this.table = _table;
019                this.name = _name;
020                switch (_decisionSeparator) {
021                case '+':
022                        this.relationToNextDecision = RelationToNextDecision.COMBINED;
023                        break;
024                case ',':
025                        this.relationToNextDecision = RelationToNextDecision.SEQUANTIAL;
026                        break;
027                case ';':
028                        this.relationToNextDecision = RelationToNextDecision.BRANCHED;
029                        break;
030                case '#':
031                        this.relationToNextDecision = RelationToNextDecision.BRANCHED;
032                        break;
033                case '?':
034                        this.relationToNextDecision = RelationToNextDecision.SWITCHED;
035                        break;
036                default:
037                        this.relationToNextDecision = RelationToNextDecision.NONE;
038                }
039                cachedSymbol = new StringBuilder();
040                cachedCode = -1;
041        }
042
043        
044        public void clearCache() {
045                cachedCode = -1;
046                cachedSymbol.setLength(0);
047        }
048
049        public String getSymbol(int code) throws MaltChainedException {
050                if (code < 0 && !containCode(code)) {
051                        clearCache();
052                        return null;
053                }
054                if (cachedCode != code) {
055                        clearCache();
056                        cachedCode = code;
057                        cachedSymbol.append(table.getSymbolCodeToString(cachedCode));
058                }
059                return cachedSymbol.toString();
060        }
061        
062        public int getCode(String symbol) throws MaltChainedException {
063                if (cachedSymbol == null || !cachedSymbol.equals(symbol)) {
064                        clearCache();
065                        cachedSymbol.append(symbol);
066                        cachedCode = table.getSymbolStringToCode(symbol);
067                }
068                return cachedCode;
069        }
070        
071        public boolean containCode(int code) throws MaltChainedException {
072                if (cachedCode != code) {
073                        clearCache();
074                        cachedSymbol.append(table.getSymbolCodeToString(code));
075                        if (cachedSymbol == null) {
076                                return false;
077                        }
078                        cachedCode = code;
079                }
080                return true;
081        }
082        
083        public boolean containSymbol(String symbol) throws MaltChainedException {
084                if (cachedSymbol == null || !cachedSymbol.equals(symbol)) {
085                        clearCache();
086                        cachedCode = table.getSymbolStringToCode(symbol);
087                        if (cachedCode < 0) {
088                                return false;
089                        }
090                        cachedSymbol.append(symbol);
091                }
092                return true;
093        }
094        
095        public boolean continueWithNextDecision(int code) throws MaltChainedException {
096                if (table instanceof DecisionPropertyTable) {
097                        return ((DecisionPropertyTable)table).continueWithNextDecision(code);
098                }
099                return true;
100        }
101        
102        public boolean continueWithNextDecision(String symbol) throws MaltChainedException {
103                if (table instanceof DecisionPropertyTable) {
104                        return ((DecisionPropertyTable)table).continueWithNextDecision(symbol);
105                }
106                return true;
107        }
108        
109        public Table getTable() {
110                return table;
111        }
112        
113        public String getTableName() {
114                return table != null?table.getName():null;              
115        }
116        
117        public String getTableContainerName() {
118                return name;
119        }
120
121        public RelationToNextDecision getRelationToNextDecision() {
122                return relationToNextDecision;
123        }
124
125        protected void setTable(Table table) {
126                this.table = table;
127        }
128
129        protected void setName(String name) {
130                this.name = name;
131        }
132        
133        public int size() {
134                return table.size();
135        }
136        
137        public String toString() {
138                final StringBuilder sb = new StringBuilder();
139                sb.append(name);
140                sb.append(" -> " );
141                sb.append(cachedSymbol);
142                sb.append(" = ");
143                sb.append(cachedCode);
144                return sb.toString();
145        }
146}