001package org.unix4j.unix.wc;
002
003import java.util.Collections;
004import java.util.EnumSet;
005import java.util.Iterator;
006
007import org.unix4j.option.Option;
008import org.unix4j.unix.Wc;
009
010/**
011 * Options for the {@link Wc wc} command.
012 * <p>
013 * For most applications, it may be more convenient to use {@link Wc#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 -l}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --lines}</td><td>&nbsp;</td><td>Executes a count of lines and writes this count to the output.</td></tr>
018 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -w}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --words}</td><td>&nbsp;</td><td>Executes a count of words and writes this count to the output. A
019                        word is a non-zero-length string of characters delimited by white
020                        space as defined by {@link Character#isWhitespace(char)}.</td></tr>
021 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -m}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --chars}</td><td>&nbsp;</td><td>Executes a count of chars and writes this count to the output.</td></tr>
022 * </table>
023 */
024public enum WcOption implements Option, WcOptions {
025        /**
026         * Option <b>{@code --lines}</b>, <b>{@code -l}</b>: 
027         * Executes a count of lines and writes this count to the output.
028         */
029        lines('l'),
030        /**
031         * Option <b>{@code --words}</b>, <b>{@code -w}</b>: 
032         * Executes a count of words and writes this count to the output. A
033                        word is a non-zero-length string of characters delimited by white
034                        space as defined by {@link Character#isWhitespace(char)}.
035         */
036        words('w'),
037        /**
038         * Option <b>{@code --chars}</b>, <b>{@code -m}</b>: 
039         * Executes a count of chars and writes this count to the output.
040         */
041        chars('m');
042        
043        private final char acronym;
044        private WcOption(char acronym) {
045                this.acronym = acronym;
046        }
047        @Override
048        public Class<WcOption> optionType() {
049                return WcOption.class;
050        }
051        /**
052         * Returns the option with the given {@code acronym}, or {@code null} if no
053         * such option is found.
054         * 
055         * @param acronym the option {@link #acronym() acronym}
056         * @return      the option with the given {@code acronym} or {@code null} if it
057         *                      is not found
058         */
059        public static WcOption findByAcronym(char acronym) {
060                for (final WcOption opt : values()) {
061                        if (opt.acronym() == acronym) return opt;
062                }
063                return null;
064        }
065        @Override
066        public char acronym() {
067                return acronym;
068        }
069        @Override
070        public boolean isSet(WcOption option) {
071                return equals(option);
072        }
073        /**
074         * Returns a new set with {@code this} active option.
075         * 
076         * @return a new set containing this option
077         */
078        @Override
079        public EnumSet<WcOption> asSet() {
080                return EnumSet.of(this);
081        }
082        
083        /**
084         * Returns an immutable iterator returning o single element: {@code this} 
085         * option.
086         * 
087         * @return an immutable iterator with {@code this} active option.
088         */
089        @Override
090        public Iterator<WcOption> iterator() {
091                return Collections.singleton(this).iterator();
092        }
093        
094        /**
095         * Returns 1 as this is a set with a single element: {@code this} option
096         * 
097         * @return one
098         */
099        @Override
100        public int size() {
101                return 1;
102        }
103
104        /**
105         * Returns true if the {@link Option#acronym() acronym} should be used for
106         * the specified {@code option} in string representations. 
107         * <p>
108         * This method returns always true for all options.
109         *  
110         * @param option
111         *            the option of interest
112         * @return always true indicating that option acronyms should be used in
113         *                      string representations for all options
114         */
115        @Override
116        public boolean useAcronymFor(WcOption option) {
117                return true;
118        }
119}