org.unix4j.unix.sed.SedOption Maven / Gradle / Ivy
package org.unix4j.unix.sed;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import org.unix4j.option.Option;
import org.unix4j.unix.Sed;
/**
* Options for the {@link Sed sed} command.
*
* For most applications, it may be more convenient to use {@link Sed#Options}
* instead of the option constants defined here.
*
*
* {@code -n} {@code --quiet} Suppress the default output (in which each line, after it is
examined for editing, is written to standard output). Only lines
explicitly selected for output are written.
* {@code -g} {@code --global} Globally substitute for all non-overlapping instances of the regexp
rather than just the first one.
(This option is ignored if the occurrence operand is specified).
* {@code -p} {@code --print} Write the matched line to standard output.
* {@code -l} {@code --lineNumber} Writes the current line number on a separate line to the standard
output.
* {@code -I} {@code --ignoreCase} Use case insensitive pattern matching.
* {@code -s} {@code --substitute} Substitutes the replacement string for instances of the regexp in
the matched line.
The characters "$0" appearing in the replacement are replaced
by the line matching the regexp. The characters "$n", where n is a
digit other than zero, are replaced by the text matched by the
corresponding backreference expression (aka group). The special
meaning of "$n" in this context can be suppressed by preceding it
by a backslash.
A line can be split by substituting a newline ('\n') into it.
A substitution is considered to have been performed even if the
replacement string is identical to the string that it replaces.
* {@code -a} {@code --append} Append string2 as a separate line after the matched line.
* {@code -i} {@code --insert} Insert string2 as a separate line before the matched line.
* {@code -c} {@code --change} Write string2 as a separate line instead of the matched line.
* {@code -d} {@code --delete} Delete the matched line.
* {@code -y} {@code --translate} Replace all occurrences of characters in string1 with the
corresponding characters in string2. If the number of characters in
the two strings are not equal, or if any of the characters in
string1 appear more than once, the results are undefined.
*
*/
public enum SedOption implements Option, SedOptions {
/**
* Option {@code --quiet}, {@code -n}:
* Suppress the default output (in which each line, after it is
examined for editing, is written to standard output). Only lines
explicitly selected for output are written.
*/
quiet('n'),
/**
* Option {@code --global}, {@code -g}:
* Globally substitute for all non-overlapping instances of the regexp
rather than just the first one.
(This option is ignored if the occurrence operand is specified).
*/
global('g'),
/**
* Option {@code --print}, {@code -p}:
* Write the matched line to standard output.
*/
print('p'),
/**
* Option {@code --lineNumber}, {@code -l}:
* Writes the current line number on a separate line to the standard
output.
*/
lineNumber('l'),
/**
* Option {@code --ignoreCase}, {@code -I}:
* Use case insensitive pattern matching.
*/
ignoreCase('I'),
/**
* Option {@code --substitute}, {@code -s}:
* Substitutes the replacement string for instances of the regexp in
the matched line.
The characters "$0" appearing in the replacement are replaced
by the line matching the regexp. The characters "$n", where n is a
digit other than zero, are replaced by the text matched by the
corresponding backreference expression (aka group). The special
meaning of "$n" in this context can be suppressed by preceding it
by a backslash.
A line can be split by substituting a newline ('\n') into it.
A substitution is considered to have been performed even if the
replacement string is identical to the string that it replaces.
*/
substitute('s'),
/**
* Option {@code --append}, {@code -a}:
* Append string2 as a separate line after the matched line.
*/
append('a'),
/**
* Option {@code --insert}, {@code -i}:
* Insert string2 as a separate line before the matched line.
*/
insert('i'),
/**
* Option {@code --change}, {@code -c}:
* Write string2 as a separate line instead of the matched line.
*/
change('c'),
/**
* Option {@code --delete}, {@code -d}:
* Delete the matched line.
*/
delete('d'),
/**
* Option {@code --translate}, {@code -y}:
* Replace all occurrences of characters in string1 with the
corresponding characters in string2. If the number of characters in
the two strings are not equal, or if any of the characters in
string1 appear more than once, the results are undefined.
*/
translate('y');
private final char acronym;
private SedOption(char acronym) {
this.acronym = acronym;
}
@Override
public Class optionType() {
return SedOption.class;
}
/**
* Returns the option with the given {@code acronym}, or {@code null} if no
* such option is found.
*
* @param acronym the option {@link #acronym() acronym}
* @return the option with the given {@code acronym} or {@code null} if it
* is not found
*/
public static SedOption findByAcronym(char acronym) {
for (final SedOption opt : values()) {
if (opt.acronym() == acronym) return opt;
}
return null;
}
@Override
public char acronym() {
return acronym;
}
@Override
public boolean isSet(SedOption option) {
return equals(option);
}
/**
* Returns a new set with {@code this} active option.
*
* @return a new set containing this option
*/
@Override
public EnumSet asSet() {
return EnumSet.of(this);
}
/**
* Returns an immutable iterator returning o single element: {@code this}
* option.
*
* @return an immutable iterator with {@code this} active option.
*/
@Override
public Iterator iterator() {
return Collections.singleton(this).iterator();
}
/**
* Returns 1 as this is a set with a single element: {@code this} option
*
* @return one
*/
@Override
public int size() {
return 1;
}
/**
* Returns true if the {@link Option#acronym() acronym} should be used for
* the specified {@code option} in string representations.
*
* This method returns always true for all options.
*
* @param option
* the option of interest
* @return always true indicating that option acronyms should be used in
* string representations for all options
*/
@Override
public boolean useAcronymFor(SedOption option) {
return true;
}
}