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