001package org.unix4j.codegen.optset;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.List;
006
007import org.unix4j.codegen.command.def.CommandDef;
008import org.unix4j.codegen.command.def.OptionDef;
009import org.unix4j.codegen.optset.constraint.EnabledOptionConstraint;
010import org.unix4j.codegen.optset.constraint.ExclusiveOptionConstraint;
011import org.unix4j.codegen.optset.constraint.OptionConstraint;
012
013public class Constraints {
014        
015        public static Collection<OptionConstraint> getOptionConstraints(CommandDef commandDef) {
016                final List<OptionConstraint> constraints = new ArrayList<OptionConstraint>();
017                addExclusiveOptionConstraints(commandDef, constraints);
018                addEnabledOptionConstraints(commandDef, constraints);
019                return constraints;
020        }
021
022        private static void addExclusiveOptionConstraints(CommandDef commandDef, List<OptionConstraint> constraints) {
023                for (final OptionDef opt : commandDef.options.values()) {
024                        if (!opt.excludes.isEmpty()) {
025                                constraints.add(new ExclusiveOptionConstraint(opt.name, opt.excludes));
026                        }
027                }
028        }
029        private static void addEnabledOptionConstraints(CommandDef commandDef, List<OptionConstraint> constraints) {
030                for (final OptionDef opt : commandDef.options.values()) {
031                        if (!opt.enabledBy.isEmpty()) {
032                                constraints.add(new EnabledOptionConstraint(opt.name, opt.enabledBy));
033                        }
034                }
035        }
036}