001package org.unix4j.codegen.optset.def;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.LinkedHashMap;
006import java.util.List;
007import java.util.Map;
008
009import org.unix4j.codegen.command.def.CommandDef;
010import org.unix4j.codegen.command.def.OptionDef;
011import org.unix4j.codegen.def.AbstractElementDef;
012import org.unix4j.codegen.def.TypeDef;
013import org.unix4j.codegen.optset.OptionHelper;
014
015public class OptionGroupDef extends AbstractElementDef {
016        
017        public OptionGroupDef(CommandDef commandDef, Collection<OptionDef> options) {
018                final String groupName = commandDef.command.simpleName + "OptionSet";
019                this.groupType = new TypeDef(new OptionHelper().getNameWithOptionPostfix(groupName, options), commandDef.pkg);
020        }
021
022        public final TypeDef groupType;
023        public final Map<String, OptionDef> options = new LinkedHashMap<String, OptionDef>();   //key: option (long) names
024        public final Map<String, OptionGroupDef> optionToNextGroup = new LinkedHashMap<String, OptionGroupDef>();       //key: option (long) names
025        public final List<Map<String, ActiveSetDef>> levelActiveSets = new ArrayList<Map<String, ActiveSetDef>>();//key in map:activeSet.name / level 0: initial n active options, level 1: n+1 active option, ...
026
027        @Override
028        public String toString() {
029                return "{\n" + 
030                "\tgroupType:\t" + groupType.simpleName + "\n" + 
031                "\toptions:\t" + options.keySet() + "\n" + 
032                "\toptionToNextGroup:\t" + getOptionToNextGroupString() + "\n" + 
033                "\tlevelActiveSets:\t" + getLevelActiveSetString() + "\n" +
034                "}";
035        }
036
037        private String getOptionToNextGroupString() {
038                final Map<String, String> sm = new LinkedHashMap<String, String>();
039                for (final Map.Entry<String, OptionGroupDef> e : optionToNextGroup.entrySet()) {
040                        sm.put(e.getKey(), e.getValue().groupType.simpleName);
041                }
042                return sm.toString();
043        }
044
045        private String getLevelActiveSetString() {
046                final StringBuilder sb = new StringBuilder();
047                int index = 0;
048                for (final Map<String, ActiveSetDef> levelSets : levelActiveSets) {
049                        sb.append("\t[").append(index).append("]=");
050                        boolean first = true;
051                        for (final String setName : levelSets.keySet()) {
052                                if (!first) sb.append(", ");
053                                else first = false;
054                                sb.append(setName);
055                        }
056                        index++;
057                }
058                return sb.toString();
059        }
060}