001package org.maltparser.core.feature.spec;
002import java.util.Iterator;
003import java.util.LinkedHashMap;
004
005import org.maltparser.core.exception.MaltChainedException;
006
007/**
008*
009*
010* @author Johan Hall
011*/
012public class SpecificationModel implements Iterable<SpecificationSubModel> {
013        private final String specModelName;
014        private final LinkedHashMap<String, SpecificationSubModel> subModelMap;
015        
016        public SpecificationModel() throws MaltChainedException {
017                this(null);
018        }
019        
020        public SpecificationModel(String _specModelName) throws MaltChainedException {
021                this.specModelName = _specModelName;
022                this.subModelMap = new LinkedHashMap<String, SpecificationSubModel>();
023        }
024        
025        public void add(String featureSpec) throws MaltChainedException {
026                this.add("MAIN", featureSpec);
027        }
028        
029        public void add(String subModelName, String featureSpec) throws MaltChainedException {
030                if (subModelName == null || subModelName.length() < 1  || subModelName.toUpperCase().equals("MAIN")) {
031                        if (!subModelMap.containsKey("MAIN")) {
032                                subModelMap.put("MAIN", new SpecificationSubModel("MAIN"));
033                        }
034                        subModelMap.get("MAIN").add(featureSpec);
035                } else {
036                        if (!subModelMap.containsKey(subModelName.toUpperCase())) {
037                                subModelMap.put(subModelName.toUpperCase(), new SpecificationSubModel(subModelName.toUpperCase()));
038                        }
039                        subModelMap.get(subModelName.toUpperCase()).add(featureSpec);
040                }
041        }
042
043        public String getSpecModelName() {
044                return specModelName;
045        }
046        
047        public Iterator<SpecificationSubModel> iterator() {
048                return subModelMap.values().iterator();
049        }
050        
051        public int size() {
052                return subModelMap.size();
053        }
054        
055        public SpecificationSubModel getSpecSubModel(String subModelName) {
056                return subModelMap.get(subModelName);
057        }
058        
059        public String toString() {
060                StringBuilder sb = new StringBuilder();
061
062                for (SpecificationSubModel subModel : this) {
063                        if (subModel.size() > 0) { 
064                                if (subModelMap.size() != 1 || subModel.getSubModelName().equalsIgnoreCase("MAIN")) {
065                                        sb.append(subModel.getSubModelName());
066                                        sb.append('\n');
067                                }
068                                sb.append(subModel.toString());
069                        }
070                }
071                return sb.toString();
072        }
073}