1   package jsdsi;
2   
3   import java.util.Iterator;
4   
5   import jsdsi.sexp.Sexp;
6   import jsdsi.sexp.SexpParseException;
7   import jsdsi.sexp.SexpUtil;
8   
9   /***
10   * A tag that matches all strings with a given prefix.
11   * 
12   * @author Sameer Ajmani
13   * @author Sean Radford
14   * @version $Revision: 1.5.2.1 $ $Date: 2005/11/08 03:12:52 $
15   */
16  public class PrefixTag extends ExprTag {
17      
18      private static final long serialVersionUID = 4409242379275402071L;
19      
20      /***
21       * The Prefix.
22       */
23      private transient final String prefix;
24      
25      /***
26       * Creates a new <code>PrefixTag</code> wit ha given prefix string.
27       * 
28       * @param p the prefix string.
29       */
30      public PrefixTag(String p) {
31          assert(p != null) : "null prefix";
32          prefix = p;
33      }
34      
35      /***
36       * @see jsdsi.Tag#intersect(Tag)
37       */
38      public Tag intersect(Tag that)
39      {
40          if (that instanceof PrefixTag) {
41              return intersect((PrefixTag)that);
42          }
43          if (that instanceof StringTag) {
44              return intersect((StringTag)that);
45          }
46          if (that instanceof ReversePrefixTag) {
47              return that.intersect(this);
48          }
49          if (that instanceof SetTag) {
50              return that.intersect(this);
51          }
52          return Tag.NULL_TAG;
53      }
54      
55      /***
56       * @see jsdsi.Tag#intersect(Tag)
57       */
58      public Tag intersect(PrefixTag that)
59      {
60          if (this.prefix.startsWith(that.prefix)) {
61              return this;
62          }
63          if (that.prefix.startsWith(this.prefix)) {
64              return that;
65          }
66          return Tag.NULL_TAG;
67      }
68      
69      /***
70       * @see jsdsi.Tag#intersect(Tag)
71       */
72      public Tag intersect(StringTag that)
73      {
74          if (that.getValue().startsWith(this.prefix)) {
75              return that;
76          }
77          return Tag.NULL_TAG;
78      }
79      
80      /***
81       * @see java.lang.Object#equals(Object)
82       */
83      public boolean equals(Object that) {
84          return (that instanceof PrefixTag)
85          && this.prefix.equals(((PrefixTag) that).prefix);
86      }
87      
88      /***
89       * @see java.lang.Object#hashCode()
90       */
91      public int hashCode() {
92          return prefix.hashCode();
93      }
94      
95      /***
96       * Returns the prefix of this tag.
97       * 
98       * @return the prefix of this tag.
99       */
100     public String getPrefix() {
101         return prefix;
102     }
103     
104     public Sexp toTagSexp() {
105         Sexp[] ss = new Sexp[2];
106         ss[0] = SexpUtil.toSexp("prefix");
107         ss[1] = SexpUtil.toSexp(getPrefix());
108         return SexpUtil.toSexp("*", ss);
109     }
110     
111     static PrefixTag parsePrefixTag(Iterator tbody) throws SexpParseException {
112         String p = SexpUtil.getNextString(tbody, "prefix tag");
113         SexpUtil.checkDone(tbody, "prefix tag");
114         return new PrefixTag(p);
115     }
116 }
This page was automatically generated by Maven