1 | package com.framsticks.params.types; |
---|
2 | |
---|
3 | import com.framsticks.params.Param; |
---|
4 | import com.framsticks.params.ParamBuilder; |
---|
5 | import com.framsticks.util.FramsticksException; |
---|
6 | import com.framsticks.util.lang.Strings; |
---|
7 | |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.List; |
---|
10 | import java.util.regex.Matcher; |
---|
11 | import java.util.regex.Pattern; |
---|
12 | |
---|
13 | import javax.annotation.concurrent.Immutable; |
---|
14 | |
---|
15 | /** |
---|
16 | * @author Piotr Sniegowski |
---|
17 | */ |
---|
18 | @Immutable |
---|
19 | public class ProcedureParam extends Param { |
---|
20 | private final Param resultType; |
---|
21 | private final List<Param> argumentsType = new ArrayList<Param>(); |
---|
22 | |
---|
23 | private static Pattern addressPattern = Pattern.compile("^([^\\(]+)?\\(([^\\)]*)\\)$"); |
---|
24 | |
---|
25 | /** |
---|
26 | * @param builder |
---|
27 | */ |
---|
28 | public ProcedureParam(ParamBuilder builder) { |
---|
29 | super(builder); |
---|
30 | String signature = builder.getProcedureSignature(); |
---|
31 | if (Strings.notEmpty(signature)) { |
---|
32 | Matcher matcher = addressPattern.matcher(signature); |
---|
33 | if (!matcher.matches()) { |
---|
34 | throw new FramsticksException().msg("invalid signature"); |
---|
35 | } |
---|
36 | String result = Strings.collapse(matcher.group(1)); |
---|
37 | resultType = (result != null) ? parseType(result, null) : null; |
---|
38 | String arguments = matcher.group(2); |
---|
39 | if (!Strings.notEmpty(arguments)) { |
---|
40 | return; |
---|
41 | } |
---|
42 | for (String a : arguments.split(",")) { |
---|
43 | int space = a.indexOf(' '); |
---|
44 | String type; |
---|
45 | String name; |
---|
46 | if (space == -1) { |
---|
47 | type = a; |
---|
48 | name = null; |
---|
49 | } else { |
---|
50 | type = a.substring(0, space); |
---|
51 | name = a.substring(space + 1); |
---|
52 | } |
---|
53 | argumentsType.add(parseType(type, name)); |
---|
54 | } |
---|
55 | } else { |
---|
56 | resultType = null; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | protected static Param parseType(String type, String name) { |
---|
61 | return Param.build().type(type).name(name).finish(); |
---|
62 | } |
---|
63 | |
---|
64 | @Override |
---|
65 | public Class<?> getStorageType() { |
---|
66 | return Void.class; |
---|
67 | } |
---|
68 | |
---|
69 | public Param getResultType() { |
---|
70 | return resultType; |
---|
71 | } |
---|
72 | |
---|
73 | public List<Param> getArgumentsType() { |
---|
74 | return argumentsType; |
---|
75 | } |
---|
76 | |
---|
77 | @Override |
---|
78 | public String getFramsTypeName() { |
---|
79 | return "p"; |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|