jvcard remote support (initial commit, not ready for use yet)
[jvcard.git] / src / be / nikiroo / jvcard / remote / Command.java
1 package be.nikiroo.jvcard.remote;
2
3 public class Command {
4 public enum Verb {
5 /** VERSION of the protocol */
6 VERSION,
7 /** TIME of the remote server in milliseconds since the Unix epoch */
8 TIME,
9 /** STOP the communication (client stops) */
10 STOP,
11 /**
12 * LIST all the contacts on the remote server that contain the search
13 * term, or all contacts if no search term given
14 */
15 LIST,
16 /** HELP about the protocol for interactive access */
17 HELP,
18 /** GET a remote contact */
19 GET,
20 /** PUT a new contact to the remote server or update an existing one */
21 PUT,
22 /** POST a new contact to the remote server */
23 POST,
24 /** DELETE an existing contact from the remote server */
25 DELETE,
26 }
27
28 private Verb verb;
29 private int version;
30 private String param;
31
32 /**
33 * Create a new, empty {@link Command} with the given {@link Verb} and
34 * version.
35 *
36 * @param verb
37 * the {@link Verb}
38 * @param version
39 * the version
40 */
41 public Command(Verb verb, int version) {
42 this(verb, null, version);
43 }
44
45 /**
46 * Create a new, empty {@link Command} with the given {@link Verb} and
47 * version.
48 *
49 * @param verb
50 * the {@link Verb}
51 * @param version
52 * the version
53 */
54 public Command(Verb verb, String param, int version) {
55 this.verb = verb;
56 this.version = version;
57 this.param = param;
58 }
59
60 /**
61 * Read a command line (starting with a {@link Verb}) and process its
62 * content here in a more readable format.
63 *
64 * @param input
65 * the command line
66 * @param version
67 * the version (which can be overrided by a {@link Verb#VERSION}
68 * command)
69 */
70 public Command(String input, int version) {
71 this.version = version;
72
73 if (input != null) {
74 String v = input;
75 int indexSp = input.indexOf(" ");
76 if (indexSp >= 0) {
77 v = input.substring(0, indexSp);
78 }
79
80 for (Verb verb : Verb.values()) {
81 if (v.equals(verb.name())) {
82 this.verb = verb;
83 }
84 }
85
86 if (verb != null) {
87 String param = null;
88 if (indexSp >= 0)
89 param = input.substring(indexSp + 1);
90
91 this.param = param;
92
93 if (verb == Verb.VERSION) {
94 try {
95 version = Integer.parseInt(param);
96 } catch (NumberFormatException e) {
97 e.printStackTrace();
98 }
99 }
100 }
101 }
102 }
103
104 /**
105 * Return the version
106 *
107 * @return the version
108 */
109 public int getVersion() {
110 return version;
111 }
112
113 /**
114 * Return the {@link Verb}
115 *
116 * @return the {@link Verb}
117 */
118 public Verb getVerb() {
119 return verb;
120 }
121
122 /**
123 * Return the parameter of this {@link Command} if any.
124 *
125 * @return the parameter or NULL
126 */
127 public String getParam() {
128 return param;
129 }
130
131 @Override
132 public String toString() {
133 if (verb == null)
134 return "[null command]";
135
136 switch (verb) {
137 case VERSION:
138 return verb.name() + " " + version;
139 default:
140 return verb.name() + (param == null ? "" : " " + param);
141 }
142 }
143 }