org.unix4j.unix.sed.SedOptions Maven / Gradle / Ivy
package org.unix4j.unix.sed;
import java.util.Collections;
import java.util.Iterator;
import org.unix4j.convert.OptionSetConverters.OptionSetConverter;
import org.unix4j.convert.ValueConverter;
import org.unix4j.option.DefaultOptionSet;
import org.unix4j.option.Option;
import org.unix4j.option.OptionSet;
import org.unix4j.unix.Sed;
import org.unix4j.unix.sed.SedOption;
/**
* Interface implemented by all option sets for the {@link Sed sed} command.
* It is recommended to use {@link Sed#Options} to specify a valid
* combination of options.
*
* The options for the sed command are:
*
*
* {@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.
*
*
* This interface serves as an alias for the extended interface to simplify the
* command signature methods by avoiding generic parameters.
*/
public interface SedOptions extends OptionSet {
/**
* Constant for an empty option set.
*/
SedOptions EMPTY = new SedOptions() {
@Override
public Class optionType() {
return SedOption.class;
}
@Override
public boolean isSet(SedOption option) {
return false;
}
/**
* Returns 0 as this is a set with no active options.
*
* @return zero
*/
@Override
public int size() {
return 0;
}
/**
* Returns an immutable empty set.
*
* @return an immutable empty set.
*/
@Override
public java.util.Set asSet() {
return Collections.emptySet();
}
/**
* Returns an iterator returning no elements.
*
* @return an immutable iterator with no elements.
*/
@Override
public Iterator iterator() {
return asSet().iterator();
}
/**
* Returns true if the {@link Option#acronym() acronym} should be used
* for the specified {@code option} in string representations.
*
* This method returns always true;
*
* @param option
* the option of interest
* @return always true
*/
@Override
public boolean useAcronymFor(SedOption option) {
return true;
}
};
/**
* Default implementation for a modifiable option set.
*/
class Default extends DefaultOptionSet implements SedOptions {
/**
* Default constructor for an empty option set with no active options.
*/
public Default() {
super(SedOption.class);
}
/**
* Constructor for an option set with a single active option.
* @param option the option to be set
*/
public Default(SedOption option) {
super(option);
}
/**
* Constructor for an option set with the given active options.
* @param options the options to be set
*/
public Default(SedOption... options) {
this();
setAll(options);
}
/**
* Constructor for an option set initialized with the options given by
* another option set.
*
* @param optionSet set with the options to be active
*/
public Default(OptionSet optionSet) {
this();
setAll(optionSet);
}
}
/**
* Value converter for {@link SedOptions} based on an {@link OptionSetConverter}.
*/
ValueConverter CONVERTER = new ValueConverter() {
private final OptionSetConverter converter = new OptionSetConverter(SedOption.class);
@Override
public SedOptions convert(Object value) {
final OptionSet set = converter.convert(value);
return set == null ? null : new Default(set);
}
};
}