001package org.unix4j.unix.echo;
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.Echo;
010
011/**
012 * Option sets for the {@link Echo echo} command with 
013 * the following options: {@link #n n}.
014 * <p>
015 * Application code does normally not directly refer to this class;
016 * {@link Echo#Options} should be used instead to specify command 
017 * options. See also {@link org.unix4j.unix.echo.EchoOptions} for more information.
018 */
019public enum EchoOptionSet_n implements EchoOptions {
020        /** Option set with the following active options: {@link #noNewline n}.*/
021        Active_n(
022                /*n:*/null /*already set*/, /*noNewline:*/null /*already set*/, 
023                true, 
024                /*active:*/EchoOption.noNewline
025        ),
026        /** Option set with the following active options: {@link #noNewline n}.*/
027        Active_n_long(
028                /*n:*/null /*already set*/, /*noNewline:*/null /*already set*/, 
029                false, 
030                /*active:*/EchoOption.noNewline
031        );
032        private EchoOptionSet_n(
033                EchoOptionSet_n n, EchoOptionSet_n noNewline, 
034                boolean useAcronym,
035                EchoOption... activeOptions
036        ) {
037                this.n = n == null ? this : n;
038                this.noNewline = noNewline == null ? this : noNewline;
039                this.useAcronym = useAcronym;
040                this.options = activeOptions.length == 0 ? EnumSet.noneOf(EchoOption.class) : EnumSet.copyOf(Arrays.asList(activeOptions));
041        }
042        private final boolean useAcronym;
043        /**
044         * Option {@code "-n"}: Do not print the trailing newline character(s).
045         * <p>
046         * The option {@code "-n"} is equivalent to the {@code "--}{@link #noNewline noNewline}{@code "} option.
047         * <p>
048         * Technically speaking, this field points to a set with the options of the 
049         * current set plus the option {@code "-n"}. If the option {@code "-n"}
050         * is already set, the field {@code n} points to the enum constant itself
051         * as it already represents the current set of options.
052         */
053        public final EchoOptionSet_n n;
054        /**
055         * Option {@code "--noNewline"}: Do not print the trailing newline character(s).
056         * <p>
057         * The option {@code "--noNewline"} is equivalent to the {@code "-}{@link #n n}{@code "} option.
058         * <p>
059         * Technically speaking, this field points to a set with the options of the 
060         * current set plus the option {@code "--noNewline"}. If the option {@code "--noNewline"}
061         * is already set, the field {@code noNewline} points to the enum constant itself
062         * as it already represents the current set of options.
063         */
064        public final EchoOptionSet_n noNewline;
065        private final EnumSet<EchoOption> options;
066        
067        //inherit javadoc
068        @Override
069        public Class<EchoOption> optionType() {
070                return EchoOption.class;
071        }
072        //inherit javadoc
073        @Override
074        public boolean isSet(EchoOption option) {
075                return options.contains(option);
076        }
077        //inherit javadoc
078        @Override
079        public int size() {
080                return options.size();
081        }
082        /**
083         * Returns the set with the active options. The returned set a new defensive
084         * copy instance created when this method is called, modifications of this
085         * set will therefore not alter {@code this} option set.
086         * 
087         * @return a copy of the set with the active options.
088         */
089        @Override
090        public EnumSet<EchoOption> asSet() {
091                return EnumSet.copyOf(options);
092        }
093        /**
094         * Returns an immutable iterator with the active options of this option set.
095         * 
096         * @return an immutable iterator for over the active options
097         */
098        @Override
099        public Iterator<EchoOption> iterator() {
100                return Collections.unmodifiableSet(options).iterator();
101        }
102        /**
103         * Returns true if the {@link Option#acronym() acronym} should be used in
104         * for the specified {@code option} string representations. 
105         * <p>
106         * In particular and independent from the {@code option} argument, this 
107         * option set returns true if the last option added to this set was an 
108         * acronym, and false if it was a long option name. 
109         * <p>
110         * For instance, the set defined as
111         * <pre>
112         *    EchoOptionSet_n.n;
113         * </pre>
114         * uses acronyms, that is, this method always returns true for the above 
115         * set. 
116         * <p>
117         * On the other hand, long option names are used and this method always 
118         * returns false for the set
119         * <pre>
120         *    EchoOptionSet_n.noNewline;
121         * </pre>
122         * <p>
123         * Note that a repeated option is <i>not</i> treated as the last set option.
124         * For instance, the first and last option of the following set are 
125         * equivalent and acronyms are used:
126         * <pre>
127         *    EchoOptionSet_n.n.noNewline;
128         * </pre>
129         * <p>
130         * This method always returns true for the empty set with no active options.
131         *  
132         * @param option
133         *            the option of interest, has no impact on the result returned
134         *            by this method
135         * @return true if option acronyms should be used for string representations
136         *         of any option of this option set
137         */
138        @Override
139        public boolean useAcronymFor(EchoOption option) {
140                return useAcronym;
141        }
142}