| 1 | package be.nikiroo.jvcard.remote; |
| 2 | |
| 3 | public class CommandInstance { |
| 4 | private Command cmd; |
| 5 | private int version; |
| 6 | private String param; |
| 7 | |
| 8 | /** |
| 9 | * Create a new, empty {@link CommandInstance} with the given |
| 10 | * {@link Command} and version. |
| 11 | * |
| 12 | * @param command |
| 13 | * the {@link Command} |
| 14 | * @param version |
| 15 | * the version |
| 16 | */ |
| 17 | public CommandInstance(Command command, int version) { |
| 18 | this(command, null, version); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Create a new, empty {@link CommandInstance} with the given |
| 23 | * {@link Command} and version. |
| 24 | * |
| 25 | * @param cmd |
| 26 | * the {@link Command} |
| 27 | * @param version |
| 28 | * the version |
| 29 | */ |
| 30 | public CommandInstance(Command cmd, String param, int version) { |
| 31 | this.cmd = cmd; |
| 32 | this.version = version; |
| 33 | this.param = param; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Read a command line (starting with a {@link Command}) and process its |
| 38 | * content here in a more readable format. |
| 39 | * |
| 40 | * @param input |
| 41 | * the command line |
| 42 | * @param version |
| 43 | * the version (which can be overrided by a |
| 44 | * {@link Command#VERSION} command) |
| 45 | */ |
| 46 | public CommandInstance(String input, int version) { |
| 47 | this.version = version; |
| 48 | |
| 49 | if (input != null) { |
| 50 | String v = input; |
| 51 | int indexSp = input.indexOf(" "); |
| 52 | if (indexSp >= 0) { |
| 53 | v = input.substring(0, indexSp); |
| 54 | } |
| 55 | |
| 56 | for (Command command : Command.values()) { |
| 57 | if (v.equals(command.name())) { |
| 58 | this.cmd = command; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (cmd != null) { |
| 63 | String param = null; |
| 64 | if (indexSp >= 0) |
| 65 | param = input.substring(indexSp + 1); |
| 66 | |
| 67 | this.param = param; |
| 68 | |
| 69 | if (cmd == Command.VERSION) { |
| 70 | try { |
| 71 | version = Integer.parseInt(param); |
| 72 | } catch (NumberFormatException e) { |
| 73 | e.printStackTrace(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Return the version |
| 82 | * |
| 83 | * @return the version |
| 84 | */ |
| 85 | public int getVersion() { |
| 86 | return version; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Return the {@link Command} |
| 91 | * |
| 92 | * @return the {@link Command} |
| 93 | */ |
| 94 | public Command getCommand() { |
| 95 | return cmd; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Return the parameter of this {@link CommandInstance} if any. |
| 100 | * |
| 101 | * @return the parameter or NULL |
| 102 | */ |
| 103 | public String getParam() { |
| 104 | return param; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | if (cmd == null) |
| 110 | return "[null command]"; |
| 111 | |
| 112 | switch (cmd) { |
| 113 | case VERSION: |
| 114 | return cmd.name() + " " + version; |
| 115 | default: |
| 116 | return cmd.name() + (param == null ? "" : " " + param); |
| 117 | } |
| 118 | } |
| 119 | } |