1 | package com.framsticks.communication.queries; |
---|
2 | |
---|
3 | import com.framsticks.util.Misc; |
---|
4 | import com.framsticks.util.lang.Pair; |
---|
5 | |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.Collection; |
---|
8 | import java.util.List; |
---|
9 | |
---|
10 | /** |
---|
11 | * @author Piotr Sniegowski |
---|
12 | */ |
---|
13 | public class CallRequest extends ApplicationRequest { |
---|
14 | |
---|
15 | protected final List<Object> arguments = new ArrayList<>(); |
---|
16 | protected String procedure; |
---|
17 | |
---|
18 | public CallRequest addArguments(String arguments) { |
---|
19 | return this; |
---|
20 | } |
---|
21 | |
---|
22 | /** |
---|
23 | * @return the arguments |
---|
24 | */ |
---|
25 | public List<Object> getArguments() { |
---|
26 | return arguments; |
---|
27 | } |
---|
28 | |
---|
29 | /** |
---|
30 | * @return the procedure |
---|
31 | */ |
---|
32 | public String getProcedure() { |
---|
33 | return procedure; |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * @param arguments the arguments to set |
---|
38 | */ |
---|
39 | public CallRequest arguments(Collection<Object> arguments) { |
---|
40 | this.arguments.clear(); |
---|
41 | this.arguments.addAll(arguments); |
---|
42 | return this; |
---|
43 | } |
---|
44 | |
---|
45 | public CallRequest argument(Object argument) { |
---|
46 | arguments.add(argument); |
---|
47 | return this; |
---|
48 | } |
---|
49 | |
---|
50 | public CallRequest procedure(String procedure) { |
---|
51 | this.procedure = procedure; |
---|
52 | return this; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | protected StringBuilder construct(StringBuilder buffer) { |
---|
57 | super.construct(buffer); |
---|
58 | Misc.throwIfNull(procedure); |
---|
59 | buffer.append(' ').append(procedure); |
---|
60 | for (Object arg : arguments) { |
---|
61 | buffer.append(' '); |
---|
62 | quoteArgumentIfNeeded(buffer, arg); |
---|
63 | } |
---|
64 | return buffer; |
---|
65 | } |
---|
66 | |
---|
67 | @Override |
---|
68 | public String getCommand() { |
---|
69 | return "call"; |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | public CharSequence parseRest(CharSequence rest) { |
---|
74 | rest = super.parseRest(rest); |
---|
75 | Pair<CharSequence, CharSequence> p = Misc.throwIfNull(takeIdentifier(rest)); |
---|
76 | this.procedure = p.first.toString(); |
---|
77 | while ((p = takeString(p.second)) != null) { |
---|
78 | arguments.add(p.first); |
---|
79 | } |
---|
80 | return null; |
---|
81 | } |
---|
82 | |
---|
83 | } |
---|