001 package org.maltparser.core.feature.spec; 002 003 import java.util.Iterator; 004 import java.util.LinkedHashMap; 005 import java.util.Map; 006 007 /** 008 * 009 * 010 * @author Johan Hall 011 */ 012 public class SpecificationSubModel implements Iterable<String> { 013 private Map<String, Integer> featureSpec2IndexMap; 014 private int counter; 015 private String name; 016 017 public SpecificationSubModel() { 018 this("MAIN"); 019 } 020 021 public SpecificationSubModel(String name) { 022 setSubModelName(name); 023 featureSpec2IndexMap = new LinkedHashMap<String, Integer>(); 024 counter = 0; 025 } 026 027 public void add(String featureSpec) { 028 if (!featureSpec2IndexMap.containsKey(featureSpec)) { 029 featureSpec2IndexMap.put(featureSpec, counter++); 030 } 031 } 032 033 public int getFeatureIndex(String featureSpec) { 034 if (featureSpec2IndexMap.containsKey(featureSpec)) { 035 return -1; 036 } 037 return featureSpec2IndexMap.get(featureSpec); 038 } 039 040 public String getFeatureSpec(int featureId) { 041 if (featureId < 0 || featureId >= featureSpec2IndexMap.size()) { 042 return null; 043 } 044 return featureSpec2IndexMap.keySet().toArray(new String[]{})[featureId]; 045 } 046 047 public String getSubModelName() { 048 return name; 049 } 050 051 public void setSubModelName(String name) { 052 this.name = name; 053 } 054 055 public int size() { 056 return featureSpec2IndexMap.size(); 057 } 058 059 public Iterator<String> iterator() { 060 return featureSpec2IndexMap.keySet().iterator(); 061 } 062 063 public boolean equals(Object obj) { 064 if (this == obj) 065 return true; 066 if (obj == null) 067 return false; 068 if (getClass() != obj.getClass()) 069 return false; 070 if (featureSpec2IndexMap.size() != ((SpecificationSubModel)obj).size()) { return false; } 071 for (String str : this) { 072 if (!str.equals(((SpecificationSubModel)obj).getFeatureSpec(featureSpec2IndexMap.get(str)))) { 073 return false; 074 } 075 } 076 return true; 077 } 078 079 public String toString() { 080 StringBuilder sb = new StringBuilder(); 081 for (String str : this) { 082 sb.append(featureSpec2IndexMap.get(str)); 083 sb.append('\t'); 084 sb.append(str); 085 sb.append('\n'); 086 } 087 return sb.toString(); 088 } 089 }