001package org.unix4j.unix.grep;
002
003import java.util.Collections;
004import java.util.EnumSet;
005import java.util.Iterator;
006
007import org.unix4j.option.Option;
008import org.unix4j.unix.Grep;
009
010/**
011 * Options for the {@link Grep grep} command.
012 * <p>
013 * For most applications, it may be more convenient to use {@link Grep#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 -i}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --ignoreCase}</td><td>&nbsp;</td><td>Match lines ignoring the case when comparing the strings, also known
018                        from Unix with its acronym 'i'.</td></tr>
019 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -v}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --invertMatch}</td><td>&nbsp;</td><td>Invert the match result, that is, a non-matching line is written to
020                        the output and a matching line is not. This option is also known 
021                        from Unix with its acronym 'v'.</td></tr>
022 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -F}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --fixedStrings}</td><td>&nbsp;</td><td>Use fixed-strings matching instead of regular expressions. This is
023                        usually faster than the standard regexp version.
024                        <p>
025                        (This option is ignored if a {@code pattern} operand is specified
026                        instead of the {@code regexp} string).</td></tr>
027 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -n}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --lineNumber}</td><td>&nbsp;</td><td>Prefix each line of output with the line number within its input
028                        file.</td></tr>
029 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -c}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --count}</td><td>&nbsp;</td><td>Suppress normal output; instead print a count of matching lines for
030                        each input file. With the {@code -v}, {@code --invertMatch} option,
031                        count non-matching lines.</td></tr>
032 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -l}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --matchingFiles}</td><td>&nbsp;</td><td>Suppress normal output; instead print the name of each input file
033                        from which output would normally have been printed. The scanning
034                        will stop on the first match.</td></tr>
035 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -x}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --wholeLine}</td><td>&nbsp;</td><td>Select only those matches that exactly match the whole line
036                        excluding the terminating line ending.
037                        <p>
038                        (This option is ignored if a {@code pattern} operand is specified
039                        instead of the {@code regexp} string).</td></tr>
040 * </table>
041 */
042public enum GrepOption implements Option, GrepOptions {
043        /**
044         * Option <b>{@code --ignoreCase}</b>, <b>{@code -i}</b>: 
045         * Match lines ignoring the case when comparing the strings, also known
046                        from Unix with its acronym 'i'.
047         */
048        ignoreCase('i'),
049        /**
050         * Option <b>{@code --invertMatch}</b>, <b>{@code -v}</b>: 
051         * Invert the match result, that is, a non-matching line is written to
052                        the output and a matching line is not. This option is also known 
053                        from Unix with its acronym 'v'.
054         */
055        invertMatch('v'),
056        /**
057         * Option <b>{@code --fixedStrings}</b>, <b>{@code -F}</b>: 
058         * Use fixed-strings matching instead of regular expressions. This is
059                        usually faster than the standard regexp version.
060                        <p>
061                        (This option is ignored if a {@code pattern} operand is specified
062                        instead of the {@code regexp} string).
063         */
064        fixedStrings('F'),
065        /**
066         * Option <b>{@code --lineNumber}</b>, <b>{@code -n}</b>: 
067         * Prefix each line of output with the line number within its input
068                        file.
069         */
070        lineNumber('n'),
071        /**
072         * Option <b>{@code --count}</b>, <b>{@code -c}</b>: 
073         * Suppress normal output; instead print a count of matching lines for
074                        each input file. With the {@code -v}, {@code --invertMatch} option,
075                        count non-matching lines.
076         */
077        count('c'),
078        /**
079         * Option <b>{@code --matchingFiles}</b>, <b>{@code -l}</b>: 
080         * Suppress normal output; instead print the name of each input file
081                        from which output would normally have been printed. The scanning
082                        will stop on the first match.
083         */
084        matchingFiles('l'),
085        /**
086         * Option <b>{@code --wholeLine}</b>, <b>{@code -x}</b>: 
087         * Select only those matches that exactly match the whole line
088                        excluding the terminating line ending.
089                        <p>
090                        (This option is ignored if a {@code pattern} operand is specified
091                        instead of the {@code regexp} string).
092         */
093        wholeLine('x');
094        
095        private final char acronym;
096        private GrepOption(char acronym) {
097                this.acronym = acronym;
098        }
099        @Override
100        public Class<GrepOption> optionType() {
101                return GrepOption.class;
102        }
103        /**
104         * Returns the option with the given {@code acronym}, or {@code null} if no
105         * such option is found.
106         * 
107         * @param acronym the option {@link #acronym() acronym}
108         * @return      the option with the given {@code acronym} or {@code null} if it
109         *                      is not found
110         */
111        public static GrepOption findByAcronym(char acronym) {
112                for (final GrepOption opt : values()) {
113                        if (opt.acronym() == acronym) return opt;
114                }
115                return null;
116        }
117        @Override
118        public char acronym() {
119                return acronym;
120        }
121        @Override
122        public boolean isSet(GrepOption option) {
123                return equals(option);
124        }
125        /**
126         * Returns a new set with {@code this} active option.
127         * 
128         * @return a new set containing this option
129         */
130        @Override
131        public EnumSet<GrepOption> asSet() {
132                return EnumSet.of(this);
133        }
134        
135        /**
136         * Returns an immutable iterator returning o single element: {@code this} 
137         * option.
138         * 
139         * @return an immutable iterator with {@code this} active option.
140         */
141        @Override
142        public Iterator<GrepOption> iterator() {
143                return Collections.singleton(this).iterator();
144        }
145        
146        /**
147         * Returns 1 as this is a set with a single element: {@code this} option
148         * 
149         * @return one
150         */
151        @Override
152        public int size() {
153                return 1;
154        }
155
156        /**
157         * Returns true if the {@link Option#acronym() acronym} should be used for
158         * the specified {@code option} in string representations. 
159         * <p>
160         * This method returns always true for all options.
161         *  
162         * @param option
163         *            the option of interest
164         * @return always true indicating that option acronyms should be used in
165         *                      string representations for all options
166         */
167        @Override
168        public boolean useAcronymFor(GrepOption option) {
169                return true;
170        }
171}