Move some code around, simplify
[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     void toXCQLInternal(XCQLBuilder b, int level, String relationElement) {
62         b.indent(level).append("<modifier>\n");
63         b.indent(level + 1).append("<type>");
64         b.xq(type).append("</type>\n");
65         if (value != null) {
66             b.indent(level + 1).append("<").append(relationElement).append(">");
67             b.xq(comparison).append("</").append(relationElement).append(">\n");
68             b.indent(level + 1).append("<value>");
69             b.xq(value).append("</value>\n");
70         }
71         b.indent(level).append("</modifier>\n");
72     }
73
74     public String toCQL() {
75         StringBuilder buf = new StringBuilder(type);
76         if (value != null)
77             buf.append(" ").append(comparison).append(" ").append(value);
78         return buf.toString();
79     }
80 }