Returns "" rather than "null" on missing value
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / commands / Expression.java
index 3c377e2..694996f 100644 (file)
@@ -1,12 +1,25 @@
 package com.indexdata.mkjsf.pazpar2.commands;\r
 \r
 import java.io.Serializable;\r
-import java.util.StringTokenizer;\r
 \r
 import org.apache.log4j.Logger;\r
 \r
-import com.indexdata.mkjsf.pazpar2.commands.Expression;\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
@@ -16,21 +29,51 @@ public class Expression implements Serializable {
   String rightEntity;\r
   String label;  \r
   \r
-  public Expression (String leftEntity, String operator, String rightEntity, String label) {\r
-    this.leftEntity = leftEntity;\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 = rightEntity;    \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
-    StringTokenizer tokenizer = new StringTokenizer(expressionString,"=");\r
-    this.leftEntity = tokenizer.nextToken();\r
-    this.operator = "=";\r
-    this.rightEntity = tokenizer.nextToken();\r
-    this.label=rightEntity;\r
+    String[] parts = expressionString.split("[=~]");\r
+    if (parts.length>0) {\r
+      this.leftEntity = parts[0];\r
+      this.operator = expressionString.contains("=") ? "=" : "~";\r
+    }\r
+    if (parts.length>1) {\r
+      this.rightEntity = parts[1];\r
+      this.label=rightEntity;\r
+    } else {\r
+      this.rightEntity = "";\r
+      this.label = "";\r
+    }\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
@@ -40,18 +83,39 @@ public class Expression implements Serializable {
     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