lphy.base.function.Split Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lphy-base Show documentation
Show all versions of lphy-base Show documentation
The standard library of LPhy, which contains the required generative distributions and basic functions.
The newest version!
package lphy.base.function;
import lphy.core.model.DeterministicFunction;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;
import lphy.core.model.datatype.StringValue;
public class Split extends DeterministicFunction {
public static final String stringParamName = "str";
public static final String regexParamName = "regex";
public static final String indexParamName = "i";
public Split(@ParameterInfo(name = stringParamName, description = "the string value to extract a substring matching the given pattern.") Value str,
@ParameterInfo(name = regexParamName, description = "a regular expression") Value regex,
@ParameterInfo(name = indexParamName, description = "the index (>=0) of the substring to return") Value i) {
setParam(stringParamName, str);
setParam(regexParamName, regex);
setParam(indexParamName, i);
}
@Override
@GeneratorInfo(name = "split", description = "A function to split a given string at the regular expressions " +
"and return the i'th (starting from 0) substring of the resulting list.")
public Value apply() {
String str = getString().value();
String regex = getRegex().value();
int i = getI().value();
String[] parts = str.split(regex);
if (parts.length > i) {
return new StringValue(null, parts[i], this);
} else {
return new StringValue(null, "", this);
}
}
public Value getString() {
return (Value) paramMap.get(stringParamName);
}
public Value getRegex() {
return (Value) paramMap.get(regexParamName);
}
public Value getI() {
return (Value) paramMap.get(indexParamName);
}
}