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 #c c}, {@link #d d}, {@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_cdgu implements UniqOptions {
020        /** Option set with the following active options: {@link #global g}.*/
021        Active_g(
022                /*c:*/UniqOptionSet_cg.Active_cg, /*count:*/UniqOptionSet_cg.Active_cg_long, /*d:*/UniqOptionSet_dg.Active_dg, /*duplicatedOnly:*/UniqOptionSet_dg.Active_dg_long, /*g:*/null /*already set*/, /*global:*/null /*already set*/, /*u:*/UniqOptionSet_gu.Active_gu, /*uniqueOnly:*/UniqOptionSet_gu.Active_gu_long, 
023                true, 
024                /*active:*/UniqOption.global
025        ),
026        /** Option set with the following active options: {@link #global g}.*/
027        Active_g_long(
028                /*c:*/UniqOptionSet_cg.Active_cg, /*count:*/UniqOptionSet_cg.Active_cg_long, /*d:*/UniqOptionSet_dg.Active_dg, /*duplicatedOnly:*/UniqOptionSet_dg.Active_dg_long, /*g:*/null /*already set*/, /*global:*/null /*already set*/, /*u:*/UniqOptionSet_gu.Active_gu, /*uniqueOnly:*/UniqOptionSet_gu.Active_gu_long, 
029                false, 
030                /*active:*/UniqOption.global
031        );
032        private UniqOptionSet_cdgu(
033                UniqOptionSet_cg c, UniqOptionSet_cg count, UniqOptionSet_dg d, UniqOptionSet_dg duplicatedOnly, UniqOptionSet_cdgu g, UniqOptionSet_cdgu global, UniqOptionSet_gu u, UniqOptionSet_gu uniqueOnly, 
034                boolean useAcronym,
035                UniqOption... activeOptions
036        ) {
037                this.c = notNull(c);
038                this.count = notNull(count);
039                this.d = notNull(d);
040                this.duplicatedOnly = notNull(duplicatedOnly);
041                this.g = g == null ? this : g;
042                this.global = global == null ? this : global;
043                this.u = notNull(u);
044                this.uniqueOnly = notNull(uniqueOnly);
045                this.useAcronym = useAcronym;
046                this.options = activeOptions.length == 0 ? EnumSet.noneOf(UniqOption.class) : EnumSet.copyOf(Arrays.asList(activeOptions));
047        }
048        private final boolean useAcronym;
049        /**
050         * Option {@code "-c"}: Precedes each output line with a count of the number of times the
051                        line occurred in the input.
052         * <p>
053         * The option {@code "-c"} is equivalent to the {@code "--}{@link #count count}{@code "} option.
054         * <p>
055         * Technically speaking, this field points to a set with the options of the 
056         * current set plus the option {@code "-c"}. If the option {@code "-c"}
057         * is already set, the field {@code c} points to the enum constant itself
058         * as it already represents the current set of options.
059         */
060        public final UniqOptionSet_cg c;
061        /**
062         * Option {@code "--count"}: Precedes each output line with a count of the number of times the
063                        line occurred in the input.
064         * <p>
065         * The option {@code "--count"} is equivalent to the {@code "-}{@link #c c}{@code "} option.
066         * <p>
067         * Technically speaking, this field points to a set with the options of the 
068         * current set plus the option {@code "--count"}. If the option {@code "--count"}
069         * is already set, the field {@code count} points to the enum constant itself
070         * as it already represents the current set of options.
071         */
072        public final UniqOptionSet_cg count;
073        /**
074         * Option {@code "-d"}: Suppresses the writing of lines that are not repeated in the input.
075         * <p>
076         * The option {@code "-d"} is equivalent to the {@code "--}{@link #duplicatedOnly duplicatedOnly}{@code "} option.
077         * <p>
078         * Technically speaking, this field points to a set with the options of the 
079         * current set plus the option {@code "-d"}. If the option {@code "-d"}
080         * is already set, the field {@code d} points to the enum constant itself
081         * as it already represents the current set of options.
082         */
083        public final UniqOptionSet_dg d;
084        /**
085         * Option {@code "--duplicatedOnly"}: Suppresses the writing of lines that are not repeated in the input.
086         * <p>
087         * The option {@code "--duplicatedOnly"} is equivalent to the {@code "-}{@link #d d}{@code "} option.
088         * <p>
089         * Technically speaking, this field points to a set with the options of the 
090         * current set plus the option {@code "--duplicatedOnly"}. If the option {@code "--duplicatedOnly"}
091         * is already set, the field {@code duplicatedOnly} points to the enum constant itself
092         * as it already represents the current set of options.
093         */
094        public final UniqOptionSet_dg duplicatedOnly;
095        /**
096         * Option {@code "-g"}: Suppresses repeated lines globally, that is, if lines are 
097                        non-adjacent. This option guarantees unique output lines even if the
098                        input lines are not sorted.
099         * <p>
100         * The option {@code "-g"} is equivalent to the {@code "--}{@link #global global}{@code "} option.
101         * <p>
102         * Technically speaking, this field points to a set with the options of the 
103         * current set plus the option {@code "-g"}. If the option {@code "-g"}
104         * is already set, the field {@code g} points to the enum constant itself
105         * as it already represents the current set of options.
106         */
107        public final UniqOptionSet_cdgu g;
108        /**
109         * Option {@code "--global"}: Suppresses repeated lines globally, that is, if lines are 
110                        non-adjacent. This option guarantees unique output lines even if the
111                        input lines are not sorted.
112         * <p>
113         * The option {@code "--global"} is equivalent to the {@code "-}{@link #g g}{@code "} option.
114         * <p>
115         * Technically speaking, this field points to a set with the options of the 
116         * current set plus the option {@code "--global"}. If the option {@code "--global"}
117         * is already set, the field {@code global} points to the enum constant itself
118         * as it already represents the current set of options.
119         */
120        public final UniqOptionSet_cdgu global;
121        /**
122         * Option {@code "-u"}: Suppresses the writing of lines that are repeated in the input.
123         * <p>
124         * The option {@code "-u"} is equivalent to the {@code "--}{@link #uniqueOnly uniqueOnly}{@code "} option.
125         * <p>
126         * Technically speaking, this field points to a set with the options of the 
127         * current set plus the option {@code "-u"}. If the option {@code "-u"}
128         * is already set, the field {@code u} points to the enum constant itself
129         * as it already represents the current set of options.
130         */
131        public final UniqOptionSet_gu u;
132        /**
133         * Option {@code "--uniqueOnly"}: Suppresses the writing of lines that are repeated in the input.
134         * <p>
135         * The option {@code "--uniqueOnly"} is equivalent to the {@code "-}{@link #u u}{@code "} option.
136         * <p>
137         * Technically speaking, this field points to a set with the options of the 
138         * current set plus the option {@code "--uniqueOnly"}. If the option {@code "--uniqueOnly"}
139         * is already set, the field {@code uniqueOnly} points to the enum constant itself
140         * as it already represents the current set of options.
141         */
142        public final UniqOptionSet_gu uniqueOnly;
143        private final EnumSet<UniqOption> options;
144        
145        //inherit javadoc
146        @Override
147        public Class<UniqOption> optionType() {
148                return UniqOption.class;
149        }
150        //inherit javadoc
151        @Override
152        public boolean isSet(UniqOption option) {
153                return options.contains(option);
154        }
155        //inherit javadoc
156        @Override
157        public int size() {
158                return options.size();
159        }
160        /** 
161         * Checks that the given {@code value} is not null and throws an exception 
162         * otherwise.
163         * 
164         * @param the value to check
165         * @return the given {@code value} if it is not null
166         * @throws NullPointerException if {@code value==null} 
167         */
168        private static <T> T notNull(T value) {
169                if (value != null) return value;
170                throw new NullPointerException();
171        }
172        /**
173         * Returns the set with the active options. The returned set a new defensive
174         * copy instance created when this method is called, modifications of this
175         * set will therefore not alter {@code this} option set.
176         * 
177         * @return a copy of the set with the active options.
178         */
179        @Override
180        public EnumSet<UniqOption> asSet() {
181                return EnumSet.copyOf(options);
182        }
183        /**
184         * Returns an immutable iterator with the active options of this option set.
185         * 
186         * @return an immutable iterator for over the active options
187         */
188        @Override
189        public Iterator<UniqOption> iterator() {
190                return Collections.unmodifiableSet(options).iterator();
191        }
192        /**
193         * Returns true if the {@link Option#acronym() acronym} should be used in
194         * for the specified {@code option} string representations. 
195         * <p>
196         * In particular and independent from the {@code option} argument, this 
197         * option set returns true if the last option added to this set was an 
198         * acronym, and false if it was a long option name. 
199         * <p>
200         * For instance, the set defined as
201         * <pre>
202         *    UniqOptionSet_cdgu.count.d;
203         * </pre>
204         * uses acronyms, that is, this method always returns true for the above 
205         * set. 
206         * <p>
207         * On the other hand, long option names are used and this method always 
208         * returns false for the set
209         * <pre>
210         *    UniqOptionSet_cdgu.c.duplicatedOnly;
211         * </pre>
212         * <p>
213         * Note that a repeated option is <i>not</i> treated as the last set option.
214         * For instance, the first and last option of the following set are 
215         * equivalent and acronyms are used:
216         * <pre>
217         *    UniqOptionSet_cdgu.c.d.count;
218         * </pre>
219         * <p>
220         * This method always returns true for the empty set with no active options.
221         *  
222         * @param option
223         *            the option of interest, has no impact on the result returned
224         *            by this method
225         * @return true if option acronyms should be used for string representations
226         *         of any option of this option set
227         */
228        @Override
229        public boolean useAcronymFor(UniqOption option) {
230                return useAcronym;
231        }
232}