001 package org.maltparser.core.feature.spec; 002 003 import java.util.Iterator; 004 import java.util.Set; 005 import java.util.TreeSet; 006 import java.util.regex.Pattern; 007 008 /** 009 * 010 * 011 * @author Johan Hall 012 */ 013 public class SpecificationSubModel implements Iterable<String> { 014 private Set<String> featureSpecSet; 015 private String name; 016 private final Pattern blanks = Pattern.compile("\\s+"); 017 018 public SpecificationSubModel() { 019 this("MAIN"); 020 } 021 022 public SpecificationSubModel(String name) { 023 setSubModelName(name); 024 featureSpecSet = new TreeSet<String>(); 025 } 026 027 public void add(String featureSpec) { 028 if (featureSpec != null && featureSpec.trim().length() > 0) { 029 String strippedFeatureSpec = blanks.matcher(featureSpec).replaceAll(""); 030 featureSpecSet.add(strippedFeatureSpec); 031 } 032 } 033 034 public String getSubModelName() { 035 return name; 036 } 037 038 public void setSubModelName(String name) { 039 this.name = name; 040 } 041 042 public int size() { 043 return featureSpecSet.size(); 044 } 045 046 public Iterator<String> iterator() { 047 return featureSpecSet.iterator(); 048 } 049 050 public String toString() { 051 StringBuilder sb = new StringBuilder(); 052 for (String str : featureSpecSet) { 053 sb.append(str); 054 sb.append('\n'); 055 } 056 return sb.toString(); 057 } 058 059 @Override 060 public int hashCode() { 061 final int prime = 31; 062 int result = 1; 063 result = prime * result 064 + ((featureSpecSet == null) ? 0 : featureSpecSet.hashCode()); 065 result = prime * result + ((name == null) ? 0 : name.hashCode()); 066 return result; 067 } 068 069 @Override 070 public boolean equals(Object obj) { 071 if (this == obj) 072 return true; 073 if (obj == null) 074 return false; 075 if (getClass() != obj.getClass()) 076 return false; 077 SpecificationSubModel other = (SpecificationSubModel) obj; 078 if (featureSpecSet == null) { 079 if (other.featureSpecSet != null) 080 return false; 081 } else if (!featureSpecSet.equals(other.featureSpecSet)) 082 return false; 083 if (name == null) { 084 if (other.name != null) 085 return false; 086 } else if (!name.equals(other.name)) 087 return false; 088 return true; 089 } 090 }