org.unix4j.unix.find.FindOptions Maven / Gradle / Ivy
package org.unix4j.unix.find;
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.Find;
import org.unix4j.unix.find.FindOption;
/**
* Interface implemented by all option sets for the {@link Find find} command.
* It is recommended to use {@link Find#Options} to specify a valid
* combination of options.
*
* The options for the find command are:
*
*
* {@code -d} {@code --typeDirectory} Consider only directories
* {@code -f} {@code --typeFile} Consider only regular files
* {@code -l} {@code --typeSymlink} Consider only symbolic links
* {@code -x} {@code --typeOther} Consider only files that are neither of directory (d),
regular file (f) or symlink (l).
* {@code -r} {@code --regex} Use full regular expression syntax for the patterns specified by the
name operand
(This option is ignored if no name operand is specified).
* {@code -i} {@code --ignoreCase} Use case insensitive matching when applying the file name pattern
specified by the name operand
(This option is ignored if no name operand is specified).
* {@code -n} {@code --timeNewer} Consider only files that have been created, modified or accessed
after or at the time specified by the time operand (the default)
(This option is ignored if no time operand is specified).
* {@code -o} {@code --timeOlder} Consider only files that have been created, modified or accessed
before or at the time specified by the time operand
(This option is ignored if no time operand is specified).
* {@code -c} {@code --timeCreate} The time operand refers to the creation time of the file
(This option is ignored if no time operand is specified).
* {@code -a} {@code --timeAccess} The time operand refers to the last access time of the file
(This option is ignored if no time operand is specified).
* {@code -m} {@code --timeModified} The time operand refers to the last modification time of the file
(the default)
(This option is ignored if no time operand is specified).
* {@code -z} {@code --print0} Print the full file name on the standard output, followed by a null
character (instead of the newline character used by default). This
allows file names that contain newlines or other types of white
space to be correctly interpreted by programs that process the find
output. This option corresponds to the --delimiter0 option of xargs.
*
*
* This interface serves as an alias for the extended interface to simplify the
* command signature methods by avoiding generic parameters.
*/
public interface FindOptions extends OptionSet {
/**
* Constant for an empty option set.
*/
FindOptions EMPTY = new FindOptions() {
@Override
public Class optionType() {
return FindOption.class;
}
@Override
public boolean isSet(FindOption 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(FindOption option) {
return true;
}
};
/**
* Default implementation for a modifiable option set.
*/
class Default extends DefaultOptionSet implements FindOptions {
/**
* Default constructor for an empty option set with no active options.
*/
public Default() {
super(FindOption.class);
}
/**
* Constructor for an option set with a single active option.
* @param option the option to be set
*/
public Default(FindOption option) {
super(option);
}
/**
* Constructor for an option set with the given active options.
* @param options the options to be set
*/
public Default(FindOption... 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 FindOptions} based on an {@link OptionSetConverter}.
*/
ValueConverter CONVERTER = new ValueConverter() {
private final OptionSetConverter converter = new OptionSetConverter(FindOption.class);
@Override
public FindOptions convert(Object value) {
final OptionSet set = converter.convert(value);
return set == null ? null : new Default(set);
}
};
}