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