001package org.maltparser.core.io.dataformat;
002
003import org.maltparser.core.exception.MaltChainedException;
004
005/**
006 *  
007 *
008 * @author Johan Hall
009**/
010public class ColumnDescription implements Comparable<ColumnDescription> {
011        // Categories
012        public static final int INPUT = 1;
013        public static final int HEAD = 2;
014        public static final int DEPENDENCY_EDGE_LABEL = 3;
015        public static final int PHRASE_STRUCTURE_EDGE_LABEL = 4;
016        public static final int PHRASE_STRUCTURE_NODE_LABEL = 5;
017        public static final int SECONDARY_EDGE_LABEL = 6;
018        public static final int IGNORE = 7;
019        public static final String[] categories = { "", "INPUT", "HEAD", "DEPENDENCY_EDGE_LABEL", "PHRASE_STRUCTURE_EDGE_LABEL", "PHRASE_STRUCTURE_NODE_LABEL", "SECONDARY_EDGE_LABEL", "IGNORE" };
020        
021        // Types
022        public static final int STRING = 1;
023        public static final int INTEGER = 2;
024        public static final int BOOLEAN = 3;
025        public static final int REAL = 4;
026        public static final String[] types = { "", "STRING", "INTEGER", "BOOLEAN", "REAL" };
027        
028        private static int positionCounter = 0;
029        private final int position;
030        private final String name;
031        private final int category;
032        private final int type;
033        private final String defaultOutput;
034        private final String nullValueStrategy;
035        private final boolean internal;
036        
037        public ColumnDescription(String name, int category, int type, String defaultOutput, String nullValueStrategy, boolean internal) throws MaltChainedException {
038                this(positionCounter++, name, category, type, defaultOutput, nullValueStrategy, internal);
039        }
040        
041        private ColumnDescription(int position, String name, int category, int type, String defaultOutput, String nullValueStrategy, boolean internal) throws MaltChainedException { 
042                this.position = position;
043                this.name = name;
044                this.category = category;
045                this.type = type;
046                this.defaultOutput = defaultOutput;
047                this.nullValueStrategy = nullValueStrategy;
048                this.internal = internal;
049        }
050        
051        public int getPosition() {
052                return position;
053        }
054
055        public String getName() {
056                return name;
057        }
058        
059        public String getDefaultOutput() {
060                return defaultOutput;
061        }
062        
063        public String getNullValueStrategy() {
064                return nullValueStrategy;
065        }
066
067        public boolean isInternal() {
068                return internal;
069        }
070        
071        public int getCategory() {
072                return category;
073        }
074        
075        public String getCategoryName() {
076                if (category < 1 || category > 7 ) {
077                        return "";
078                }
079                return categories[category];
080        }
081        
082        public int getType() {
083                return type;
084        }
085
086        public String getTypeName() {
087                if (type < 1 || type > 4 ) {
088                        return "";
089                }
090                return types[type];
091        }
092        
093        public int compareTo(ColumnDescription that) {
094                final int BEFORE = -1;
095            final int EQUAL = 0;
096            final int AFTER = 1;
097            if (this == that) return EQUAL;
098            if (this.position < that.position) return BEFORE;
099            if (this.position > that.position) return AFTER;
100            return EQUAL;
101        }
102
103        @Override
104        public int hashCode() {
105                final int prime = 31;
106                int result = 1;
107                result = prime * result + category;
108                result = prime * result + ((defaultOutput == null) ? 0 : defaultOutput.hashCode());
109                result = prime * result + (internal ? 1231 : 1237);
110                result = prime * result + ((name == null) ? 0 : name.hashCode());
111                result = prime * result + ((nullValueStrategy == null) ? 0 : nullValueStrategy.hashCode());
112                result = prime * result + position;
113                result = prime * result + type;
114                return result;
115        }
116
117        @Override
118        public boolean equals(Object obj) {
119                if (this == obj)
120                        return true;
121                if (obj == null)
122                        return false;
123                if (getClass() != obj.getClass())
124                        return false;
125                ColumnDescription other = (ColumnDescription) obj;
126                if (category != other.category)
127                        return false;
128                if (defaultOutput == null) {
129                        if (other.defaultOutput != null)
130                                return false;
131                } else if (!defaultOutput.equals(other.defaultOutput))
132                        return false;
133                if (internal != other.internal)
134                        return false;
135                if (name == null) {
136                        if (other.name != null)
137                                return false;
138                } else if (!name.equals(other.name))
139                        return false;
140                if (nullValueStrategy == null) {
141                        if (other.nullValueStrategy != null)
142                                return false;
143                } else if (!nullValueStrategy.equals(other.nullValueStrategy))
144                        return false;
145                if (position != other.position)
146                        return false;
147                if (type != other.type)
148                        return false;
149                return true;
150        }
151
152        public String toString() {
153                final StringBuilder sb = new StringBuilder();
154                sb.append(name);
155                sb.append('\t');
156                sb.append(category);
157                sb.append('\t');
158                sb.append(type);
159                if (defaultOutput != null) {
160                        sb.append('\t');
161                        sb.append(defaultOutput);
162                }
163                return sb.toString();
164        }
165        
166        public static int getCategory(String categoryName)  {
167                if (categoryName.equals("INPUT")) {
168                        return ColumnDescription.INPUT;
169                } else if (categoryName.equals("HEAD")) {
170                        return ColumnDescription.HEAD;
171                } else if (categoryName.equals("OUTPUT")) {
172                        return ColumnDescription.DEPENDENCY_EDGE_LABEL;
173                } else if (categoryName.equals("DEPENDENCY_EDGE_LABEL")) {
174                        return ColumnDescription.DEPENDENCY_EDGE_LABEL;
175                } else if (categoryName.equals("PHRASE_STRUCTURE_EDGE_LABEL")) {
176                        return ColumnDescription.PHRASE_STRUCTURE_EDGE_LABEL;
177                } else if (categoryName.equals("PHRASE_STRUCTURE_NODE_LABEL")) {
178                        return ColumnDescription.PHRASE_STRUCTURE_NODE_LABEL;
179                } else if (categoryName.equals("SECONDARY_EDGE_LABEL")) {
180                        return ColumnDescription.SECONDARY_EDGE_LABEL;
181                } else if (categoryName.equals("IGNORE")) {
182                        return ColumnDescription.IGNORE;
183                }
184                return -1;
185        }
186        
187        public static int getType(String typeName) {
188                if (typeName.equals("STRING")) {
189                        return ColumnDescription.STRING;
190                } else if (typeName.equals("INTEGER")) {
191                        return ColumnDescription.INTEGER;
192                } else if (typeName.equals("BOOLEAN")) {
193                        return ColumnDescription.BOOLEAN;
194                } else if (typeName.equals("REAL")) {
195                        return ColumnDescription.REAL;
196                } else if (typeName.equals("ECHO")) {
197                        // ECHO is removed, but if it occurs in the data format file it will be interpreted as an integer instead.
198                        return ColumnDescription.INTEGER;
199                } 
200                return -1;
201        }
202}