001package org.unix4j.unix.echo;
002
003import java.util.Collections;
004import java.util.EnumSet;
005import java.util.Iterator;
006
007import org.unix4j.option.Option;
008import org.unix4j.unix.Echo;
009
010/**
011 * Options for the {@link Echo echo} command.
012 * <p>
013 * For most applications, it may be more convenient to use {@link Echo#Options} 
014 * instead of the option constants defined here.
015 * <p>
016 * <table>
017 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -n}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --noNewline}</td><td>&nbsp;</td><td>Do not print the trailing newline character(s).</td></tr>
018 * </table>
019 */
020public enum EchoOption implements Option, EchoOptions {
021        /**
022         * Option <b>{@code --noNewline}</b>, <b>{@code -n}</b>: 
023         * Do not print the trailing newline character(s).
024         */
025        noNewline('n');
026        
027        private final char acronym;
028        private EchoOption(char acronym) {
029                this.acronym = acronym;
030        }
031        @Override
032        public Class<EchoOption> optionType() {
033                return EchoOption.class;
034        }
035        /**
036         * Returns the option with the given {@code acronym}, or {@code null} if no
037         * such option is found.
038         * 
039         * @param acronym the option {@link #acronym() acronym}
040         * @return      the option with the given {@code acronym} or {@code null} if it
041         *                      is not found
042         */
043        public static EchoOption findByAcronym(char acronym) {
044                for (final EchoOption opt : values()) {
045                        if (opt.acronym() == acronym) return opt;
046                }
047                return null;
048        }
049        @Override
050        public char acronym() {
051                return acronym;
052        }
053        @Override
054        public boolean isSet(EchoOption option) {
055                return equals(option);
056        }
057        /**
058         * Returns a new set with {@code this} active option.
059         * 
060         * @return a new set containing this option
061         */
062        @Override
063        public EnumSet<EchoOption> asSet() {
064                return EnumSet.of(this);
065        }
066        
067        /**
068         * Returns an immutable iterator returning o single element: {@code this} 
069         * option.
070         * 
071         * @return an immutable iterator with {@code this} active option.
072         */
073        @Override
074        public Iterator<EchoOption> iterator() {
075                return Collections.singleton(this).iterator();
076        }
077        
078        /**
079         * Returns 1 as this is a set with a single element: {@code this} option
080         * 
081         * @return one
082         */
083        @Override
084        public int size() {
085                return 1;
086        }
087
088        /**
089         * Returns true if the {@link Option#acronym() acronym} should be used for
090         * the specified {@code option} in string representations. 
091         * <p>
092         * This method returns always true for all options.
093         *  
094         * @param option
095         *            the option of interest
096         * @return always true indicating that option acronyms should be used in
097         *                      string representations for all options
098         */
099        @Override
100        public boolean useAcronymFor(EchoOption option) {
101                return true;
102        }
103}