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            protected int type;
016            
017            public TableFeature() throws MaltChainedException {
018                    featureValue = new SingleFeatureValue(this);
019            }
020            
021            public abstract void update() throws MaltChainedException;
022            public abstract void initialize(Object[] arguments) throws MaltChainedException;
023            public abstract Class<?>[] getParameterTypes();
024            
025            public String getSymbol(int value) throws MaltChainedException {
026                    return table.getSymbolCodeToString(value);
027            }
028            
029            public int getCode(String value) throws MaltChainedException {
030                    return table.getSymbolStringToCode(value);
031            }
032            
033            public SymbolTable getSymbolTable() {
034                    return table;
035            }
036    
037            public void setSymbolTable(SymbolTable table) {
038                    this.table = table;
039            }
040            
041            public void updateCardinality() {
042    //              if (table != null) {
043    //                      featureValue.setCardinality(table.getValueCounter());
044    //              } else {
045    //                      featureValue.setCardinality(0);
046    //              }
047            }
048            
049            public void setFeatureValue(int indexCode) throws MaltChainedException {
050                    if (table.getSymbolCodeToString(indexCode) == null) {
051                            featureValue.setIndexCode(indexCode);
052                            featureValue.setValue(1);
053                            featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE));
054                            featureValue.setNullValue(true);
055                    } else {
056                            featureValue.setIndexCode(indexCode);
057                            featureValue.setValue(1);
058                            featureValue.setSymbol(table.getSymbolCodeToString(indexCode));
059                            featureValue.setNullValue(table.isNullValue(indexCode));
060                    }
061            }
062            
063            public void setFeatureValue(String symbol) throws MaltChainedException {
064                    if (table.getSymbolStringToCode(symbol) < 0) {
065                            featureValue.setIndexCode(table.getNullValueCode(NullValueId.NO_NODE));
066                            featureValue.setValue(1);
067                            featureValue.setSymbol(symbol);
068                            featureValue.setNullValue(true);
069                    } else {
070                            featureValue.setIndexCode(table.getSymbolStringToCode(symbol));
071                            featureValue.setValue(1);
072                            featureValue.setSymbol(symbol);
073                            featureValue.setNullValue(table.isNullValue(symbol));
074                    }
075            }
076            
077            public FeatureValue getFeatureValue() {
078                    return featureValue;
079            }
080            
081            public SymbolTableHandler getTableHandler() {
082                    return tableHandler;
083            }
084    
085            public void setTableHandler(SymbolTableHandler tableHandler) {
086                    this.tableHandler = tableHandler;
087            }
088            
089            public boolean equals(Object obj) {
090                    if (!(obj instanceof TableFeature)) {
091                            return false;
092                    }
093                    if (!obj.toString().equals(this.toString())) {
094                            return false;
095                    }
096                    return true;
097            }
098    
099            public void setTableName(String name) {
100                    this.tableName = name;
101            }
102            
103            public String getTableName() {
104                    return tableName;
105            }
106            
107            public  int getType() {
108                    return type;
109            }
110            
111            public void setType(int type) {
112                    this.type = type;
113            }
114            
115            public String getMapIdentifier() {
116                    return getSymbolTable().getName();
117            }
118            
119            public String toString() {
120                    return tableName;
121            }
122    }