1 package jsdsi;
2
3 import java.util.Iterator;
4
5 import jsdsi.sexp.Sexp;
6 import jsdsi.sexp.SexpList;
7 import jsdsi.sexp.SexpParseException;
8 import jsdsi.sexp.SexpUtil;
9
10 /***
11 * A tag that contains a string type and a sequence of other tags.
12 *
13 * @author Sameer Ajmani
14 * @author Sean Radford
15 * @version $Revision: 1.3.2.1 $ $Date: 2005/11/08 03:12:52 $
16 */
17 public class SimpleTag extends ExprTag {
18
19 private static final long serialVersionUID = -7996132866305015527L;
20
21 /***
22 * Value of this <code>SimpleTag</code>.
23 */
24 private transient final String value;
25
26 /***
27 * Sequence of tags.
28 */
29 private transient final ExprTag[] tags;
30
31
32 /***
33 * Creates a new <code>SimpleTag</code> from a string and an array
34 * of tags.
35 *
36 * @param v type of this <code>SimpleTag</code>.
37 * @param t sequence of tags.
38 */
39 public SimpleTag(String v, ExprTag[] t) {
40 assert(v != null) : "null value";
41 assert(t != null) : "null tags";
42 value = v;
43 tags = t;
44 }
45
46 /***
47 * If <code>that</code> is a SimpleTag, returns
48 * <code>intersect((SimpleTag)that)</code>. If <code>that</code> is
49 * a SetTag, returns <code>that.intersect(this)</code>. Otherwise
50 * returns NULL_TAG.
51 *
52 * @see jsdsi.Tag#intersect(Tag)
53 **/
54 public Tag intersect(Tag that)
55 {
56 if (that instanceof SimpleTag) {
57 return intersect((SimpleTag)that);
58 }
59 if (that instanceof SetTag) {
60 return that.intersect(this);
61 }
62 return NULL_TAG;
63 }
64
65 /***
66 * If <code>that</code> has the same value as <code>this</code>,
67 * <code>intersect</code> returns a new SimpleTag as long as the
68 * longer of the two tags whose elements are the intersection of the
69 * corresponding elements. Elements past the end of the shorter tag
70 * are copied from the longer tag. If any of the intersections is
71 * not an ExprTag, or if <code>that</code> is not a SimpleTag, or if
72 * that's value differs from <code>this</code>'s value,
73 * <code>intersect</code> returns NULL_TAG.
74 */
75 public Tag intersect(SimpleTag that)
76 {
77 if (!this.value.equals(that.value)) {
78 return Tag.NULL_TAG;
79 }
80
81 // the intersection will be as long as the longer tag
82 ExprTag[] tagRes;
83 if (this.tags.length > that.tags.length) {
84 tagRes = new ExprTag[this.tags.length];
85 } else {
86 tagRes = new ExprTag[that.tags.length];
87 }
88 // each element is the intersection of this and that elements
89 for (int i = 0; i < tagRes.length; i++) {
90 if (i >= this.tags.length) {
91 tagRes[i] = that.tags[i];
92 } else if (i >= that.tags.length) {
93 tagRes[i] = this.tags[i];
94 } else {
95 Tag tag = this.tags[i].intersect(that.tags[i]);
96 if (!(tag instanceof ExprTag)) {
97 // this excludes NULL_TAG and ALL_TAG
98 return Tag.NULL_TAG;
99 }
100 tagRes[i] = (ExprTag)tag;
101 }
102 }
103 return new SimpleTag(value, tagRes);
104 }
105
106 /***
107 * @see java.lang.Object#equals(Object)
108 */
109 public boolean equals(Object that) {
110 return (that instanceof SimpleTag)
111 && this.value.equals(((SimpleTag) that).value)
112 && Util.equals(this.tags, ((SimpleTag) that).tags);
113 }
114
115 /***
116 * @see java.lang.Object#hashCode()
117 */
118 public int hashCode() {
119 return Util.hashCode(value) ^ Util.hashCode(tags);
120 }
121
122 /***
123 * @return the string value of this <code>SimpleTag</code>.
124 */
125 public String getValue() {
126 return value;
127 }
128
129 /***
130 * @return the tags of this <code>SimpleTag</code>.
131 */
132 public ExprTag[] getTags() {
133 return tags;
134 }
135
136 public Sexp toTagSexp() {
137 Sexp[] ss = new Sexp[tags.length];
138 for (int i = 0; i < tags.length; i++) {
139 ss[i] = tags[i].toTagSexp();
140 }
141 return SexpUtil.toSexp(getValue(), ss);
142 }
143
144 static SimpleTag parseSimpleTag(SexpList l) throws SexpParseException {
145 String value = l.getType();
146 Iterator lbody = SexpUtil.getBody(l);
147 ExprTag[] tags = new ExprTag[l.size() - 1];
148 for (int i = 0; i < tags.length; i++) {
149 tags[i] = ExprTag.parseExprTag(
150 SexpUtil.getNext(lbody, "simple tag"));
151 }
152 SexpUtil.checkDone(lbody, "simple tag");
153 return new SimpleTag(value, tags);
154 }
155 }
This page was automatically generated by Maven