Use StringBuilder instead of StringBuffer, optimize
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / Modifier.java
1 // $Id: Modifier.java,v 1.4 2007-07-03 13:29:34 mike Exp $
2
3 package org.z3950.zing.cql;
4
5 /**
6  * Represents a single modifier, consisting of three elements: a type,
7  * a comparision and a value.  For example, "distance", "<", "3".  The
8  * type is mandatory; either the comparison and value must both occur,
9  * or neither must.
10  * <P>
11  * This class is used only by ModifierSet.
12  *
13  * @version $Id: Modifier.java,v 1.4 2007-07-03 13:29:34 mike Exp $
14  */
15 public class Modifier {
16     String type;
17     String comparison;
18     String value;
19
20     /**
21      * Creates a new Modifier with the specified type, comparison
22      * and value.
23      */
24     public Modifier(String type, String comparison, String value) {
25         this.type = type;
26         this.comparison = comparison;
27         this.value = value;
28         //System.err.println("Made new modifier with " + "type='" + type + "', " + "comparison='" + comparison + "', " + "value='" + value + "',\n");
29     }
30
31     /**
32      * Creates a new Modifier with the specified type but no
33      * comparison or value.
34      */
35     public Modifier(String type) {
36         this.type = type;
37         //System.err.println("Made new modifier of type '" + type + "'\n");
38     }
39
40     /**
41      * Returns the type with which the Modifier was created.
42      */
43     public String getType() {
44         return type;
45     }
46
47     /**
48      * Returns the comparison with which the Modifier was created.
49      */
50     public String getComparison() {
51         return comparison;
52     }
53
54     /**
55      * Returns the value with which the Modifier was created.
56      */
57     public String getValue() {
58         return value;
59     }
60
61     public String toXCQL(int level, String relationElement) {
62         StringBuilder buf = new StringBuilder();
63
64         buf.append(Utils.indent(level)).append("<modifier>\n").
65             append(Utils.indent(level + 1)).append("<type>").
66             append(Utils.xq(type)).append("</type>\n");
67         if (value != null) {
68             buf.append(Utils.indent(level + 1)).append("<").
69               append(relationElement).append(">").
70               append(Utils.xq(comparison)).append("</").
71               append(relationElement).append(">\n").
72               append(Utils.indent(level + 1)).append("<value>").
73               append(Utils.xq(value)).append("</value>\n");
74         }
75         buf.append(Utils.indent(level)).append("</modifier>\n");
76         return buf.toString();
77     }
78
79     public String toCQL() {
80         StringBuilder buf = new StringBuilder(type);
81         if (value != null)
82             buf.append(" ").append(comparison).append(" ").append(value);
83         return buf.toString();
84     }
85 }