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