Javadoc
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / commands / Expression.java
1 package com.indexdata.mkjsf.pazpar2.commands;\r
2 \r
3 import java.io.Serializable;\r
4 \r
5 import org.apache.log4j.Logger;\r
6 \r
7 /**\r
8  * Represents a complex command parameter value, in form of an expression with \r
9  * an equality operator\r
10  * <p>\r
11  * An expression consist of a left-of-operator field or key, an equality operator (= or ~), \r
12  * a right-of-operator value, and optionally a label describing the value for UI display.\r
13  * </p> \r
14  * <p>Examples:</p>\r
15  * <ul>\r
16  *  <li><code>pz:id=1234</code> "My Target"</li>\r
17  *  <li><code>category~libcatalog</code> "Library Catalogs"</li>\r
18  *  <li><code>author="Steinbeck, John"</code></li>\r
19  * </ul>\r
20  * @author Niels Erik\r
21  *\r
22  */\r
23 public class Expression implements Serializable {\r
24   \r
25   private static final long serialVersionUID = -751704027842027769L;\r
26   private static Logger logger = Logger.getLogger(Expression.class);\r
27   String leftEntity;\r
28   String operator;\r
29   String rightEntity;\r
30   String label;  \r
31   \r
32   /**\r
33    * Instantiates an expression with a label\r
34    * \r
35    * @param leftEntity left-of-operator field name (or 'key')\r
36    * @param operator an equality operator\r
37    * @param rightEntity right-of-operator value\r
38    * @param label to be used for display, for instance in a UI control that adds or removes the expression\r
39    *  from a command parameter\r
40    */\r
41   public Expression (String field, String operator, String value, String label) {\r
42     this.leftEntity = field;\r
43     this.operator = operator;\r
44     this.rightEntity = value;    \r
45     this.label = label;\r
46   }\r
47   \r
48   /**\r
49    * Instantiates an expression by parsing the provided expression string, which must be\r
50    * on the form {name}({=}or{~}){value}.\r
51    * <p>\r
52    * Currently only '=' and '~' are recognized as operators\r
53    * </p>\r
54    * \r
55    * @param expressionString\r
56    */\r
57   public Expression (String expressionString) {\r
58     String[] parts = expressionString.split("[=~]");\r
59     this.leftEntity = parts[0];\r
60     this.operator = expressionString.contains("=") ? "=" : "~";\r
61     this.rightEntity = parts[1];\r
62     this.label=rightEntity;\r
63   }\r
64   \r
65   /** \r
66    * Clones the expression\r
67    * \r
68    * @return a clone of this expression\r
69    */\r
70   public Expression copy() {\r
71     logger.trace("Copying " + this.toString());\r
72     return new Expression(leftEntity, operator, rightEntity, label);\r
73   }\r
74   \r
75   public String toString() {\r
76     return leftEntity + operator + rightEntity;\r
77   }\r
78   \r
79   /**\r
80    * Returns the label describing the value of the expression or,\r
81    * if no label was provided, the value itself.\r
82    * \r
83    * @return label or right-of-operator value if no label provided\r
84    */\r
85   public String getLabel() {\r
86     return label;\r
87   }\r
88   \r
89   /**\r
90    * Returns the left-of-operator field (or name or key).\r
91    * \r
92    * @return entity left of operator\r
93    */\r
94   public String getField () {\r
95     return leftEntity;\r
96   }\r
97   \r
98   /**\r
99    * Returns the operator \r
100    * \r
101    * @return the operator of the expression\r
102    */\r
103   public String getOperator() {\r
104     return operator;\r
105   }\r
106   \r
107   /**\r
108    * Returns the right-of-operator value of the expression\r
109    * \r
110    * @return entity right of operator\r
111    */\r
112   public String getValue() {\r
113     return rightEntity;\r
114   }\r
115   \r
116 }\r