001package org.unix4j.unix.grep; 002 003import java.util.Collections; 004import java.util.Iterator; 005 006import org.unix4j.convert.OptionSetConverters.OptionSetConverter; 007import org.unix4j.convert.ValueConverter; 008import org.unix4j.option.DefaultOptionSet; 009import org.unix4j.option.Option; 010import org.unix4j.option.OptionSet; 011 012import org.unix4j.unix.Grep; 013import org.unix4j.unix.grep.GrepOption; 014 015/** 016 * Interface implemented by all option sets for the {@link Grep grep} command. 017 * It is recommended to use {@link Grep#Options} to specify a valid 018 * combination of options. 019 * <p> 020 * The options for the grep command are: 021 * <p> 022 * <table> 023 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -i}</td><td> </td><td nowrap="nowrap">{@code --ignoreCase}</td><td> </td><td>Match lines ignoring the case when comparing the strings, also known 024 from Unix with its acronym 'i'.</td></tr> 025 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -v}</td><td> </td><td nowrap="nowrap">{@code --invertMatch}</td><td> </td><td>Invert the match result, that is, a non-matching line is written to 026 the output and a matching line is not. This option is also known 027 from Unix with its acronym 'v'.</td></tr> 028 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -F}</td><td> </td><td nowrap="nowrap">{@code --fixedStrings}</td><td> </td><td>Use fixed-strings matching instead of regular expressions. This is 029 usually faster than the standard regexp version. 030 <p> 031 (This option is ignored if a {@code pattern} operand is specified 032 instead of the {@code regexp} string).</td></tr> 033 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -n}</td><td> </td><td nowrap="nowrap">{@code --lineNumber}</td><td> </td><td>Prefix each line of output with the line number within its input 034 file.</td></tr> 035 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -c}</td><td> </td><td nowrap="nowrap">{@code --count}</td><td> </td><td>Suppress normal output; instead print a count of matching lines for 036 each input file. With the {@code -v}, {@code --invertMatch} option, 037 count non-matching lines.</td></tr> 038 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -l}</td><td> </td><td nowrap="nowrap">{@code --matchingFiles}</td><td> </td><td>Suppress normal output; instead print the name of each input file 039 from which output would normally have been printed. The scanning 040 will stop on the first match.</td></tr> 041 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -x}</td><td> </td><td nowrap="nowrap">{@code --wholeLine}</td><td> </td><td>Select only those matches that exactly match the whole line 042 excluding the terminating line ending. 043 <p> 044 (This option is ignored if a {@code pattern} operand is specified 045 instead of the {@code regexp} string).</td></tr> 046 * </table> 047 * <p> 048 * This interface serves as an alias for the extended interface to simplify the 049 * command signature methods by avoiding generic parameters. 050 */ 051public interface GrepOptions extends OptionSet<GrepOption> { 052 /** 053 * Constant for an empty option set. 054 */ 055 GrepOptions EMPTY = new GrepOptions() { 056 @Override 057 public Class<GrepOption> optionType() { 058 return GrepOption.class; 059 } 060 @Override 061 public boolean isSet(GrepOption option) { 062 return false; 063 } 064 /** 065 * Returns 0 as this is a set with no active options. 066 * 067 * @return zero 068 */ 069 @Override 070 public int size() { 071 return 0; 072 } 073 /** 074 * Returns an immutable empty set. 075 * 076 * @return an immutable empty set. 077 */ 078 @Override 079 public java.util.Set<GrepOption> asSet() { 080 return Collections.emptySet(); 081 } 082 083 /** 084 * Returns an iterator returning no elements. 085 * 086 * @return an immutable iterator with no elements. 087 */ 088 @Override 089 public Iterator<GrepOption> iterator() { 090 return asSet().iterator(); 091 } 092 093 /** 094 * Returns true if the {@link Option#acronym() acronym} should be used 095 * for the specified {@code option} in string representations. 096 * <p> 097 * This method returns always true; 098 * 099 * @param option 100 * the option of interest 101 * @return always true 102 */ 103 @Override 104 public boolean useAcronymFor(GrepOption option) { 105 return true; 106 } 107 }; 108 /** 109 * Default implementation for a modifiable option set. 110 */ 111 class Default extends DefaultOptionSet<GrepOption> implements GrepOptions { 112 /** 113 * Default constructor for an empty option set with no active options. 114 */ 115 public Default() { 116 super(GrepOption.class); 117 } 118 /** 119 * Constructor for an option set with a single active option. 120 * @param option the option to be set 121 */ 122 public Default(GrepOption option) { 123 super(option); 124 } 125 /** 126 * Constructor for an option set with the given active options. 127 * @param options the options to be set 128 */ 129 public Default(GrepOption... options) { 130 this(); 131 setAll(options); 132 } 133 /** 134 * Constructor for an option set initialized with the options given by 135 * another option set. 136 * 137 * @param optionSet set with the options to be active 138 */ 139 public Default(OptionSet<GrepOption> optionSet) { 140 this(); 141 setAll(optionSet); 142 } 143 } 144 145 /** 146 * Value converter for {@link GrepOptions} based on an {@link OptionSetConverter}. 147 */ 148 ValueConverter<GrepOptions> CONVERTER = new ValueConverter<GrepOptions>() { 149 private final OptionSetConverter<GrepOption> converter = new OptionSetConverter<GrepOption>(GrepOption.class); 150 @Override 151 public GrepOptions convert(Object value) { 152 final OptionSet<GrepOption> set = converter.convert(value); 153 return set == null ? null : new Default(set); 154 } 155 }; 156}