1 /*
2 * Copyright (c) 2001, Zoltan Farkas All Rights Reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 package net.sf.zel.vm;
20
21 import java.util.*;
22
23 public class Variable
24 {
25
26 /**
27 * Variable string representation
28 */
29 private final String name;
30 /**
31 * the reference chain
32 */
33 private List referenceChain;
34
35 /**
36 * Construct a variable having the referencing chain
37 *
38 * @param var List
39 */
40 public Variable(List var)
41 {
42 final Iterator iter = var.iterator();
43 Object item = iter.next();
44 final StringBuilder name = new StringBuilder();
45 name.append(item);
46 while (iter.hasNext())
47 {
48 item = iter.next();
49 if (item instanceof TmpParsingContext)
50 {
51 name.append("[*]");
52 } else if (item instanceof java.lang.Number)
53 {
54 name.append('[').append(item).append(']');
55 } else
56 {
57 name.append('.').append(item);
58 }
59 }
60 this.name = name.toString();
61 referenceChain = var;
62 }
63
64 /**
65 * Construct with String name
66 *
67 * @param name String
68 */
69 public Variable(String name)
70 {
71 this.name = name;
72 }
73
74 /**
75 * Get the variable name
76 *
77 * @return String
78 */
79 public final String getName()
80 {
81 return name;
82 }
83
84 /**
85 * return the reference chain of this variable
86 * lazy implement
87 *
88 * @return
89 */
90 public final List getReferenceChain()
91 {
92 return referenceChain;
93 }
94
95 /**
96 * return the string value of the variable
97 *
98 * @return String
99 */
100 @Override
101 public final String toString()
102 {
103 return this.name;
104 }
105
106 /**
107 * rewrite equals
108 *
109 * @param o Object
110 * @return boolean
111 */
112 @Override
113 public boolean equals(final Object o)
114 {
115 if (o instanceof Variable)
116 {
117 return name.equals(((Variable) o).name);
118 } else
119 {
120 return false;
121 }
122 }
123
124 /**
125 * the hashcode
126 *
127 * @return
128 */
129 @Override
130 public int hashCode()
131 {
132 return name.hashCode();
133 }
134
135 /**
136 * return if this variable object is a exact mach or a set of variables
137 * @return true if this variable is a wildcard matcher, false otherwise
138 */
139 public final boolean isComplete()
140 {
141 return !WildCardMatcher.isWildcardMatcher(this.name);
142 }
143 }