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 indexCode) throws MaltChainedException {
049                    if (table.getSymbolCodeToString(indexCode) == null) {
050                            featureValue.setIndexCode(indexCode);
051                            featureValue.setValue(1);
052                            featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE));
053                            featureValue.setNullValue(true);
054                    } else {
055                            featureValue.setIndexCode(indexCode);
056                            featureValue.setValue(1);
057                            featureValue.setSymbol(table.getSymbolCodeToString(indexCode));
058                            featureValue.setNullValue(table.isNullValue(indexCode));
059                    }
060            }
061            
062            public void setFeatureValue(String symbol) throws MaltChainedException {
063                    if (table.getSymbolStringToCode(symbol) < 0) {
064                            featureValue.setIndexCode(table.getNullValueCode(NullValueId.NO_NODE));
065                            featureValue.setValue(1);
066                            featureValue.setSymbol(symbol);
067                            featureValue.setNullValue(true);
068                    } else {
069                            featureValue.setIndexCode(table.getSymbolStringToCode(symbol));
070                            featureValue.setValue(1);
071                            featureValue.setSymbol(symbol);
072                            featureValue.setNullValue(table.isNullValue(symbol));
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    }