[77] | 1 | package com.framsticks.communication.queries; |
---|
| 2 | |
---|
[96] | 3 | import com.framsticks.util.Misc; |
---|
| 4 | import com.framsticks.util.lang.Pair; |
---|
[77] | 5 | |
---|
[96] | 6 | import java.util.ArrayList; |
---|
| 7 | import java.util.Collection; |
---|
[77] | 8 | import java.util.List; |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * @author Piotr Sniegowski |
---|
| 12 | */ |
---|
| 13 | public class CallRequest extends ApplicationRequest { |
---|
[84] | 14 | |
---|
[96] | 15 | protected final List<Object> arguments = new ArrayList<>(); |
---|
| 16 | protected String procedure; |
---|
[77] | 17 | |
---|
[96] | 18 | public CallRequest addArguments(String arguments) { |
---|
[77] | 19 | return this; |
---|
| 20 | } |
---|
| 21 | |
---|
[96] | 22 | /** |
---|
| 23 | * @return the arguments |
---|
| 24 | */ |
---|
| 25 | public List<Object> getArguments() { |
---|
| 26 | return arguments; |
---|
[77] | 27 | } |
---|
| 28 | |
---|
[96] | 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); |
---|
[77] | 42 | return this; |
---|
| 43 | } |
---|
| 44 | |
---|
[96] | 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 | |
---|
[77] | 55 | @Override |
---|
| 56 | protected StringBuilder construct(StringBuilder buffer) { |
---|
| 57 | super.construct(buffer); |
---|
[96] | 58 | Misc.throwIfNull(procedure); |
---|
| 59 | buffer.append(' ').append(procedure); |
---|
| 60 | for (Object arg : arguments) { |
---|
| 61 | buffer.append(' '); |
---|
| 62 | quoteArgumentIfNeeded(buffer, arg); |
---|
[77] | 63 | } |
---|
[84] | 64 | return buffer; |
---|
[77] | 65 | } |
---|
| 66 | |
---|
| 67 | @Override |
---|
| 68 | public String getCommand() { |
---|
| 69 | return "call"; |
---|
| 70 | } |
---|
| 71 | |
---|
[96] | 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 | |
---|
[77] | 83 | } |
---|