com.codetaco.cli.impl.criteria.RegxCriteria Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cli Show documentation
Show all versions of cli Show documentation
A command line parser for Java
package com.codetaco.cli.impl.criteria;
import com.codetaco.cli.impl.usage.UsageBuilder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* RegxCriteria class.
*
*
* @param
* @author Chris DeGreef [email protected]
*/
public class RegxCriteria implements ICmdLineArgCriteria {
Pattern pattern;
String patternParm;
/**
*
* Constructor for RegxCriteria.
*
*
* @param _pattern a {@link java.lang.String} object.
*/
public RegxCriteria(final String _pattern) {
this.patternParm = _pattern;
}
/**
* {@inheritDoc}
*/
@Override
public void asDefinitionText(final StringBuilder sb) {
sb.append(" --matches '");
sb.append(patternParm);
sb.append("'");
}
/**
* {@inheritDoc}
*/
@Override
public void asSetter(final StringBuilder sb) {
sb.append(".setRegxCriteria(\"");
sb.append(patternParm);
sb.append("\")");
}
/**
* {@inheritDoc}
*/
@Override
public RegxCriteria clone() throws CloneNotSupportedException {
@SuppressWarnings("unchecked") final RegxCriteria clone = (RegxCriteria) super.clone();
return clone;
}
/**
*
* Getter for the field patternParm
.
*
*
* @return a {@link java.lang.String} object.
*/
public String getPatternParm() {
return patternParm;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSelected(final Comparable value, final boolean caseSensitive) {
if (pattern == null) {
if (caseSensitive) {
this.pattern = Pattern.compile(patternParm);
} else {
this.pattern = Pattern.compile(patternParm, Pattern.CASE_INSENSITIVE);
}
}
final Matcher m = pattern.matcher(value.toString());
return m.matches();
}
/**
* {@inheritDoc}
*
* Normalization is not possible for patterns.
*/
@Override
public E normalizeValue(final E value, final boolean caseSensitive) {
return value;
}
/**
* {@inheritDoc}
*/
@Override
public void usage(final UsageBuilder str, final int indentLevel) {
str.append("The value must match this regular expression: ").append(patternParm).append(".");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy