aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/Command.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands/Command.java')
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Command.java67
1 files changed, 67 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Command.java b/app/src/main/java/org/pacien/tincapp/commands/Command.java
new file mode 100644
index 0000000..28ff226
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/commands/Command.java
@@ -0,0 +1,67 @@
1package org.pacien.tincapp.commands;
2
3import com.annimon.stream.Collectors;
4import com.annimon.stream.Optional;
5import com.annimon.stream.Stream;
6
7import java.util.Arrays;
8import java.util.Collections;
9import java.util.LinkedList;
10import java.util.List;
11
12/**
13 * @author pacien
14 */
15class Command {
16
17 static private class Option {
18 final String key;
19 final Optional<java.lang.String> val;
20
21 Option(String key, String val) {
22 this.key = key;
23 this.val = Optional.ofNullable(val);
24 }
25
26 @Override
27 public String toString() {
28 return val.isPresent() ? "--" + key + "=" + val.get() : "--" + key;
29 }
30 }
31
32 final private String cmd;
33 final private List<Option> opts;
34 final private List<String> args;
35
36 public Command(String cmd) {
37 this.cmd = cmd;
38 this.opts = new LinkedList<>();
39 this.args = new LinkedList<>();
40 }
41
42 public Command withOption(String key, String val) {
43 this.opts.add(new Option(key, val));
44 return this;
45 }
46
47 public Command withOption(String key) {
48 return this.withOption(key, null);
49 }
50
51 public Command withArguments(String... args) {
52 this.args.addAll(Arrays.asList(args));
53 return this;
54 }
55
56 public List<String> asList() {
57 return Collections.unmodifiableList(
58 Stream.concat(Stream.of(Collections.singleton(this.cmd)),
59 Stream.concat(Stream.of(this.opts).map(Option::toString), Stream.of(this.args)))
60 .collect(Collectors.toList()));
61 }
62
63 public String[] asArray() {
64 return this.asList().toArray(new String[0]);
65 }
66
67}