Avoid unnecessarily rebuilding the Guacamole protocol form of a GuacamoleInstruction.
@@ -33,12 +33,18 @@ public class GuacamoleInstruction {
/**
* The opcode of this instruction.
*/
- private String opcode;
+ private final String opcode;
/**
* All arguments of this instruction, in order.
*/
- private List<String> args;
+ private final List<String> args;
+
+ /**
+ * The cached result of converting this GuacamoleInstruction to the format
+ * used by the Guacamole protocol.
+ */
+ private String protocolForm = null;
/**
* Creates a new GuacamoleInstruction having the given Operation and
@@ -91,12 +97,17 @@ public class GuacamoleInstruction {
* Returns this GuacamoleInstruction in the form it would be sent over the
* Guacamole protocol.
*
- * @return This GuacamoleInstruction in the form it would be sent over the
+ * @return
+ * This GuacamoleInstruction in the form it would be sent over the
* Guacamole protocol.
*/
@Override
public String toString() {
+ // Avoid rebuilding Guacamole protocol form of instruction if already
+ // known
+ if (protocolForm == null) {
+
StringBuilder buff = new StringBuilder();
// Write opcode
@@ -115,7 +126,12 @@ public class GuacamoleInstruction {
// Write terminator
buff.append(';');
- return buff.toString();
+ // Cache result for future calls
+ protocolForm = buff.toString();
+
+ }
+
+ return protocolForm;
}
