package com.framsticks.params.types;
import com.framsticks.params.Param;
import com.framsticks.params.ParamBuilder;
import com.framsticks.util.FramsticksException;
import com.framsticks.util.lang.Strings;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.concurrent.Immutable;
/**
* @author Piotr Sniegowski
*/
@Immutable
public class ProcedureParam extends Param {
private final Param resultType;
private final List argumentsType = new ArrayList();
private static Pattern addressPattern = Pattern.compile("^([^\\(]+)?\\(([^\\)]*)\\)$");
/**
* @param builder
*/
public ProcedureParam(ParamBuilder builder) {
super(builder);
String signature = builder.getProcedureSignature();
if (Strings.notEmpty(signature)) {
Matcher matcher = addressPattern.matcher(signature);
if (!matcher.matches()) {
throw new FramsticksException().msg("invalid signature");
}
String result = Strings.collapse(matcher.group(1));
resultType = (result != null) ? parseType(result, null) : null;
String arguments = matcher.group(2);
if (!Strings.notEmpty(arguments)) {
return;
}
for (String a : arguments.split(",")) {
int space = a.indexOf(' ');
String type;
String name;
if (space == -1) {
type = a;
name = null;
} else {
type = a.substring(0, space);
name = a.substring(space + 1);
}
argumentsType.add(parseType(type, name));
}
} else {
resultType = null;
}
}
protected static Param parseType(String type, String name) {
return Param.build().type(type).name(name).finish();
}
@Override
public Class> getStorageType() {
return Void.class;
}
public Param getResultType() {
return resultType;
}
public List getArgumentsType() {
return argumentsType;
}
@Override
public String getFramsTypeName() {
return "p";
}
}