Merge branch 'master' of ssh://git.indexdata.com/home/git/private/mkjsf.git into...
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / commands / Expression.java
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Expression.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Expression.java
new file mode 100644 (file)
index 0000000..1961bae
--- /dev/null
@@ -0,0 +1,116 @@
+package com.indexdata.mkjsf.pazpar2.commands;\r
+\r
+import java.io.Serializable;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+/**\r
+ * Represents a complex command parameter value, in form of an expression with \r
+ * an equality operator\r
+ * <p>\r
+ * An expression consist of a left-of-operator field or key, an equality operator (= or ~), \r
+ * a right-of-operator value, and optionally a label describing the value for UI display.\r
+ * </p> \r
+ * <p>Examples:</p>\r
+ * <ul>\r
+ *  <li><code>pz:id=1234</code> "My Target"</li>\r
+ *  <li><code>category~libcatalog</code> "Library Catalogs"</li>\r
+ *  <li><code>author="Steinbeck, John"</code></li>\r
+ * </ul>\r
+ * @author Niels Erik\r
+ *\r
+ */\r
+public class Expression implements Serializable {\r
+  \r
+  private static final long serialVersionUID = -751704027842027769L;\r
+  private static Logger logger = Logger.getLogger(Expression.class);\r
+  String leftEntity;\r
+  String operator;\r
+  String rightEntity;\r
+  String label;  \r
+  \r
+  /**\r
+   * Instantiates an expression with a label\r
+   * \r
+   * @param leftEntity left-of-operator field name (or 'key')\r
+   * @param operator an equality operator\r
+   * @param rightEntity right-of-operator value\r
+   * @param label to be used for display, for instance in a UI control that adds or removes the expression\r
+   *  from a command parameter\r
+   */\r
+  public Expression (String field, String operator, String value, String label) {\r
+    this.leftEntity = field;\r
+    this.operator = operator;\r
+    this.rightEntity = value;    \r
+    this.label = label;\r
+  }\r
+  \r
+  /**\r
+   * Instantiates an expression by parsing the provided expression string, which must be\r
+   * on the form {name}({=}or{~}){value}.\r
+   * <p>\r
+   * Currently only '=' and '~' are recognized as operators\r
+   * </p>\r
+   * \r
+   * @param expressionString\r
+   */\r
+  public Expression (String expressionString) {\r
+    String[] parts = expressionString.split("[=~]");\r
+    this.leftEntity = parts[0];\r
+    this.operator = expressionString.contains("=") ? "=" : "~";\r
+    this.rightEntity = parts[1];\r
+    this.label=rightEntity;\r
+  }\r
+  \r
+  /** \r
+   * Clones the expression\r
+   * \r
+   * @return a clone of this expression\r
+   */\r
+  public Expression copy() {\r
+    logger.trace("Copying " + this.toString());\r
+    return new Expression(leftEntity, operator, rightEntity, label);\r
+  }\r
+  \r
+  public String toString() {\r
+    return leftEntity + operator + rightEntity;\r
+  }\r
+  \r
+  /**\r
+   * Returns the label describing the value of the expression or,\r
+   * if no label was provided, the value itself.\r
+   * \r
+   * @return label or right-of-operator value if no label provided\r
+   */\r
+  public String getLabel() {\r
+    return label;\r
+  }\r
+  \r
+  /**\r
+   * Returns the left-of-operator field (or name or key).\r
+   * \r
+   * @return entity left of operator\r
+   */\r
+  public String getField () {\r
+    return leftEntity;\r
+  }\r
+  \r
+  /**\r
+   * Returns the operator \r
+   * \r
+   * @return the operator of the expression\r
+   */\r
+  public String getOperator() {\r
+    return operator;\r
+  }\r
+  \r
+  /**\r
+   * Returns the right-of-operator value of the expression\r
+   * \r
+   * @return entity right of operator\r
+   */\r
+  public String getValue() {\r
+    return rightEntity;\r
+  }\r
+  \r
+}\r