001package org.maltparser.core.symbol;
002
003import org.maltparser.core.exception.MaltChainedException;
004import org.maltparser.core.feature.function.FeatureFunction;
005import org.maltparser.core.feature.function.Modifiable;
006import org.maltparser.core.feature.value.FeatureValue;
007import org.maltparser.core.feature.value.SingleFeatureValue;
008import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
009
010public abstract class TableFeature implements FeatureFunction, Modifiable {
011        protected final SingleFeatureValue featureValue;
012        protected SymbolTable table;
013        protected String tableName;
014        protected SymbolTableHandler tableHandler;
015        protected int type;
016        
017        public TableFeature(SymbolTableHandler tableHandler) throws MaltChainedException {
018                this.tableHandler = tableHandler;
019                this.featureValue = new SingleFeatureValue(this);
020        }
021        
022        public abstract void update() throws MaltChainedException;
023        public abstract void initialize(Object[] arguments) throws MaltChainedException;
024        public abstract Class<?>[] getParameterTypes();
025        
026        public String getSymbol(int value) throws MaltChainedException {
027                return table.getSymbolCodeToString(value);
028        }
029        
030        public int getCode(String value) throws MaltChainedException {
031                return table.getSymbolStringToCode(value);
032        }
033        
034        public SymbolTable getSymbolTable() {
035                return table;
036        }
037
038        public void setSymbolTable(SymbolTable table) {
039                this.table = table;
040        }
041        
042        public void setFeatureValue(int indexCode) throws MaltChainedException {
043                if (table.getSymbolCodeToString(indexCode) == null) {
044                        featureValue.setIndexCode(indexCode);
045                        featureValue.setValue(1);
046                        featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE));
047                        featureValue.setNullValue(true);
048                } else {
049                        featureValue.setIndexCode(indexCode);
050                        featureValue.setValue(1);
051                        featureValue.setSymbol(table.getSymbolCodeToString(indexCode));
052                        featureValue.setNullValue(table.isNullValue(indexCode));
053                }
054        }
055        
056        public void setFeatureValue(String symbol) throws MaltChainedException {
057                if (table.getSymbolStringToCode(symbol) < 0) {
058                        featureValue.setIndexCode(table.getNullValueCode(NullValueId.NO_NODE));
059                        featureValue.setValue(1);
060                        featureValue.setSymbol(symbol);
061                        featureValue.setNullValue(true);
062                } else {
063                        featureValue.setIndexCode(table.getSymbolStringToCode(symbol));
064                        featureValue.setValue(1);
065                        featureValue.setSymbol(symbol);
066                        featureValue.setNullValue(table.isNullValue(symbol));
067                }
068        }
069        
070        public FeatureValue getFeatureValue() {
071                return featureValue;
072        }
073        
074        public SymbolTableHandler getTableHandler() {
075                return tableHandler;
076        }
077        
078        public boolean equals(Object obj) {
079                if (!(obj instanceof TableFeature)) {
080                        return false;
081                }
082                if (!obj.toString().equals(this.toString())) {
083                        return false;
084                }
085                return true;
086        }
087
088        public void setTableName(String name) {
089                this.tableName = name;
090        }
091        
092        public String getTableName() {
093                return tableName;
094        }
095        
096        public  int getType() {
097                return type;
098        }
099        
100        public void setType(int type) {
101                this.type = type;
102        }
103        
104        public String getMapIdentifier() {
105                return getSymbolTable().getName();
106        }
107        
108        public String toString() {
109                return tableName;
110        }
111}