001package org.unix4j.unix.tail;
002
003import java.util.Collections;
004import java.util.EnumSet;
005import java.util.Iterator;
006
007import org.unix4j.option.Option;
008import org.unix4j.unix.Tail;
009
010/**
011 * Options for the {@link Tail tail} command.
012 * <p>
013 * For most applications, it may be more convenient to use {@link Tail#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 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -s}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --countFromStart}</td><td>&nbsp;</td><td>The {@code count} argument is relative to the beginning of the file
022                        instead of counting from the end of the file. For instance, 
023                        {@code tail -s 10} prints the lines starting from line 10;
024                        {@code tail -s 1} prints the whole file.</td></tr>
025 * </table>
026 */
027public enum TailOption implements Option, TailOptions {
028        /**
029         * Option <b>{@code --chars}</b>, <b>{@code -c}</b>: 
030         * The {@code count} argument is in units of characters instead of 
031                        lines. Starts from 1 and includes line ending characters.
032         */
033        chars('c'),
034        /**
035         * Option <b>{@code --suppressHeaders}</b>, <b>{@code -q}</b>: 
036         * Suppresses printing of headers when multiple files are being
037                        examined.
038         */
039        suppressHeaders('q'),
040        /**
041         * Option <b>{@code --countFromStart}</b>, <b>{@code -s}</b>: 
042         * The {@code count} argument is relative to the beginning of the file
043                        instead of counting from the end of the file. For instance, 
044                        {@code tail -s 10} prints the lines starting from line 10;
045                        {@code tail -s 1} prints the whole file.
046         */
047        countFromStart('s');
048        
049        private final char acronym;
050        private TailOption(char acronym) {
051                this.acronym = acronym;
052        }
053        @Override
054        public Class<TailOption> optionType() {
055                return TailOption.class;
056        }
057        /**
058         * Returns the option with the given {@code acronym}, or {@code null} if no
059         * such option is found.
060         * 
061         * @param acronym the option {@link #acronym() acronym}
062         * @return      the option with the given {@code acronym} or {@code null} if it
063         *                      is not found
064         */
065        public static TailOption findByAcronym(char acronym) {
066                for (final TailOption opt : values()) {
067                        if (opt.acronym() == acronym) return opt;
068                }
069                return null;
070        }
071        @Override
072        public char acronym() {
073                return acronym;
074        }
075        @Override
076        public boolean isSet(TailOption option) {
077                return equals(option);
078        }
079        /**
080         * Returns a new set with {@code this} active option.
081         * 
082         * @return a new set containing this option
083         */
084        @Override
085        public EnumSet<TailOption> asSet() {
086                return EnumSet.of(this);
087        }
088        
089        /**
090         * Returns an immutable iterator returning o single element: {@code this} 
091         * option.
092         * 
093         * @return an immutable iterator with {@code this} active option.
094         */
095        @Override
096        public Iterator<TailOption> iterator() {
097                return Collections.singleton(this).iterator();
098        }
099        
100        /**
101         * Returns 1 as this is a set with a single element: {@code this} option
102         * 
103         * @return one
104         */
105        @Override
106        public int size() {
107                return 1;
108        }
109
110        /**
111         * Returns true if the {@link Option#acronym() acronym} should be used for
112         * the specified {@code option} in string representations. 
113         * <p>
114         * This method returns always true for all options.
115         *  
116         * @param option
117         *            the option of interest
118         * @return always true indicating that option acronyms should be used in
119         *                      string representations for all options
120         */
121        @Override
122        public boolean useAcronymFor(TailOption option) {
123                return true;
124        }
125}