001package org.unix4j.codegen.optset.constraint;
002
003import java.util.Set;
004
005/**
006 * Constraint for an option that is only enabled if at least one option from a
007 * set of "enabler options" is also active.
008 */
009public class EnabledOptionConstraint implements OptionConstraint {
010        
011        private final String option;
012        private final Set<String> enabledBy;
013
014        /**
015         * Constructor with the option which is only enabled if any one of the 
016         * options in the given set is active. In other words, the constraint 
017         * renders sets invalid which contain {@code option} but none of 
018         * {@code enabledBy}.
019         *  
020         * @param option                the constrained option
021         * @param enabledBy     the "enabler options" for the specified option
022         */
023        public EnabledOptionConstraint(String option, Set<String> enabledBy) {
024                this.option = option;
025                this.enabledBy = enabledBy;
026        }
027
028        @Override
029        public boolean isValidActiveSet(Set<String> activeSet) {
030                if (activeSet.contains(option)) {
031                        for (final String enabler : enabledBy) {
032                                if (activeSet.contains(enabler)) {
033                                        return true;
034                                }
035                        }
036                        return false;
037                }
038                return true;
039        }
040
041}