001package org.unix4j.unix.cut;
002
003import java.util.Arrays;
004import java.util.Collections;
005import java.util.EnumSet;
006import java.util.Iterator;
007import org.unix4j.option.Option;
008
009import org.unix4j.unix.Cut;
010
011/**
012 * Option sets for the {@link Cut cut} command with 
013 * the following options: {@link #c c}, {@link #f f}.
014 * <p>
015 * Application code does normally not directly refer to this class;
016 * {@link Cut#Options} should be used instead to specify command 
017 * options. See also {@link org.unix4j.unix.cut.CutOptions} for more information.
018 */
019public enum CutOptionSet_cf implements CutOptions {
020        /** Option set with the following active options: {@link #chars c}, {@link #fields f}.*/
021        Active_cf(
022                /*c:*/null /*already set*/, /*chars:*/null /*already set*/, /*f:*/null /*already set*/, /*fields:*/null /*already set*/, 
023                true, 
024                /*active:*/CutOption.chars, CutOption.fields
025        ),
026        /** Option set with the following active options: {@link #chars c}, {@link #fields f}.*/
027        Active_cf_long(
028                /*c:*/null /*already set*/, /*chars:*/null /*already set*/, /*f:*/null /*already set*/, /*fields:*/null /*already set*/, 
029                false, 
030                /*active:*/CutOption.chars, CutOption.fields
031        ),
032        /** Option set with the following active options: {@link #chars c}.*/
033        Active_c(
034                /*c:*/null /*already set*/, /*chars:*/null /*already set*/, /*f:*/Active_cf, /*fields:*/Active_cf_long, 
035                true, 
036                /*active:*/CutOption.chars
037        ),
038        /** Option set with the following active options: {@link #chars c}.*/
039        Active_c_long(
040                /*c:*/null /*already set*/, /*chars:*/null /*already set*/, /*f:*/Active_cf, /*fields:*/Active_cf_long, 
041                false, 
042                /*active:*/CutOption.chars
043        ),
044        /** Option set with the following active options: {@link #fields f}.*/
045        Active_f(
046                /*c:*/Active_cf, /*chars:*/Active_cf_long, /*f:*/null /*already set*/, /*fields:*/null /*already set*/, 
047                true, 
048                /*active:*/CutOption.fields
049        ),
050        /** Option set with the following active options: {@link #fields f}.*/
051        Active_f_long(
052                /*c:*/Active_cf, /*chars:*/Active_cf_long, /*f:*/null /*already set*/, /*fields:*/null /*already set*/, 
053                false, 
054                /*active:*/CutOption.fields
055        );
056        private CutOptionSet_cf(
057                CutOptionSet_cf c, CutOptionSet_cf chars, CutOptionSet_cf f, CutOptionSet_cf fields, 
058                boolean useAcronym,
059                CutOption... activeOptions
060        ) {
061                this.c = c == null ? this : c;
062                this.chars = chars == null ? this : chars;
063                this.f = f == null ? this : f;
064                this.fields = fields == null ? this : fields;
065                this.useAcronym = useAcronym;
066                this.options = activeOptions.length == 0 ? EnumSet.noneOf(CutOption.class) : EnumSet.copyOf(Arrays.asList(activeOptions));
067        }
068        private final boolean useAcronym;
069        /**
070         * Option {@code "-c"}: The list specifies character positions.
071         * <p>
072         * The option {@code "-c"} is equivalent to the {@code "--}{@link #chars chars}{@code "} option.
073         * <p>
074         * Technically speaking, this field points to a set with the options of the 
075         * current set plus the option {@code "-c"}. If the option {@code "-c"}
076         * is already set, the field {@code c} points to the enum constant itself
077         * as it already represents the current set of options.
078         */
079        public final CutOptionSet_cf c;
080        /**
081         * Option {@code "--chars"}: The list specifies character positions.
082         * <p>
083         * The option {@code "--chars"} is equivalent to the {@code "-}{@link #c c}{@code "} option.
084         * <p>
085         * Technically speaking, this field points to a set with the options of the 
086         * current set plus the option {@code "--chars"}. If the option {@code "--chars"}
087         * is already set, the field {@code chars} points to the enum constant itself
088         * as it already represents the current set of options.
089         */
090        public final CutOptionSet_cf chars;
091        /**
092         * Option {@code "-f"}: The list specifies fields, separated in the input by the field
093                        delimiter character (see the -d option.)  Output fields are
094                        separated by a single occurrence of the field delimiter character.
095         * <p>
096         * The option {@code "-f"} is equivalent to the {@code "--}{@link #fields fields}{@code "} option.
097         * <p>
098         * Technically speaking, this field points to a set with the options of the 
099         * current set plus the option {@code "-f"}. If the option {@code "-f"}
100         * is already set, the field {@code f} points to the enum constant itself
101         * as it already represents the current set of options.
102         */
103        public final CutOptionSet_cf f;
104        /**
105         * Option {@code "--fields"}: The list specifies fields, separated in the input by the field
106                        delimiter character (see the -d option.)  Output fields are
107                        separated by a single occurrence of the field delimiter character.
108         * <p>
109         * The option {@code "--fields"} is equivalent to the {@code "-}{@link #f f}{@code "} option.
110         * <p>
111         * Technically speaking, this field points to a set with the options of the 
112         * current set plus the option {@code "--fields"}. If the option {@code "--fields"}
113         * is already set, the field {@code fields} points to the enum constant itself
114         * as it already represents the current set of options.
115         */
116        public final CutOptionSet_cf fields;
117        private final EnumSet<CutOption> options;
118        
119        //inherit javadoc
120        @Override
121        public Class<CutOption> optionType() {
122                return CutOption.class;
123        }
124        //inherit javadoc
125        @Override
126        public boolean isSet(CutOption option) {
127                return options.contains(option);
128        }
129        //inherit javadoc
130        @Override
131        public int size() {
132                return options.size();
133        }
134        /**
135         * Returns the set with the active options. The returned set a new defensive
136         * copy instance created when this method is called, modifications of this
137         * set will therefore not alter {@code this} option set.
138         * 
139         * @return a copy of the set with the active options.
140         */
141        @Override
142        public EnumSet<CutOption> asSet() {
143                return EnumSet.copyOf(options);
144        }
145        /**
146         * Returns an immutable iterator with the active options of this option set.
147         * 
148         * @return an immutable iterator for over the active options
149         */
150        @Override
151        public Iterator<CutOption> iterator() {
152                return Collections.unmodifiableSet(options).iterator();
153        }
154        /**
155         * Returns true if the {@link Option#acronym() acronym} should be used in
156         * for the specified {@code option} string representations. 
157         * <p>
158         * In particular and independent from the {@code option} argument, this 
159         * option set returns true if the last option added to this set was an 
160         * acronym, and false if it was a long option name. 
161         * <p>
162         * For instance, the set defined as
163         * <pre>
164         *    CutOptionSet_cf.chars.f;
165         * </pre>
166         * uses acronyms, that is, this method always returns true for the above 
167         * set. 
168         * <p>
169         * On the other hand, long option names are used and this method always 
170         * returns false for the set
171         * <pre>
172         *    CutOptionSet_cf.c.fields;
173         * </pre>
174         * <p>
175         * Note that a repeated option is <i>not</i> treated as the last set option.
176         * For instance, the first and last option of the following set are 
177         * equivalent and acronyms are used:
178         * <pre>
179         *    CutOptionSet_cf.c.f.chars;
180         * </pre>
181         * <p>
182         * This method always returns true for the empty set with no active options.
183         *  
184         * @param option
185         *            the option of interest, has no impact on the result returned
186         *            by this method
187         * @return true if option acronyms should be used for string representations
188         *         of any option of this option set
189         */
190        @Override
191        public boolean useAcronymFor(CutOption option) {
192                return useAcronym;
193        }
194}