001package org.unix4j.unix.head;
002
003import java.util.Collections;
004import java.util.EnumSet;
005import java.util.Iterator;
006
007import org.unix4j.option.Option;
008import org.unix4j.unix.Head;
009
010/**
011 * Options for the {@link Head head} command.
012 * <p>
013 * For most applications, it may be more convenient to use {@link Head#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 {@code count} argument is in units of characters instead of 
018                        lines. Starts from 1 and includes line ending characters.</td></tr>
019 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -q}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --suppressHeaders}</td><td>&nbsp;</td><td>Suppresses printing of headers when multiple files are being
020                        examined.</td></tr>
021 * </table>
022 */
023public enum HeadOption implements Option, HeadOptions {
024        /**
025         * Option <b>{@code --chars}</b>, <b>{@code -c}</b>: 
026         * The {@code count} argument is in units of characters instead of 
027                        lines. Starts from 1 and includes line ending characters.
028         */
029        chars('c'),
030        /**
031         * Option <b>{@code --suppressHeaders}</b>, <b>{@code -q}</b>: 
032         * Suppresses printing of headers when multiple files are being
033                        examined.
034         */
035        suppressHeaders('q');
036        
037        private final char acronym;
038        private HeadOption(char acronym) {
039                this.acronym = acronym;
040        }
041        @Override
042        public Class<HeadOption> optionType() {
043                return HeadOption.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 HeadOption findByAcronym(char acronym) {
054                for (final HeadOption 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(HeadOption 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<HeadOption> 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<HeadOption> 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(HeadOption option) {
111                return true;
112        }
113}