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