View Javadoc

1   package org.apache.torque.generator.variable;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.torque.generator.qname.QualifiedName;
23  
24  /**
25   * A variable which can be used to transport data in and between outlets.
26   * A variable has a name (possibly with a name space),
27   * a value (which may be null),
28   * and a scope (determining in which outlets this variable will be visible).
29   * if the scope is <code>CHILDREN</code>, the outlet in which this
30   * variable was set is also stored in the variable.
31   */
32  public class Variable
33  {
34      /**
35       * The scope of a variable.
36       */
37      public enum Scope
38      {
39          /**
40           * The scope of the variable is limited to the current outlet.
41           */
42          OUTLET,
43          /**
44           * The scope of the variable is the current outlet and
45           * recursively all the outlets in the merge points accessed
46           * in the generation process.
47           */
48          CHILDREN,
49          /**
50           * The variable is visible throughout the generation process of this
51           * file.
52           */
53          FILE,
54          /**
55           * The variable is visible throughout the whole generation process.
56           */
57          GLOBAL
58      }
59  
60      /**
61       * The name of the variable.
62       */
63      private QualifiedName name;
64  
65      /**
66       * The value fo the variable. May be null.
67       */
68      private Object value;
69  
70      /**
71       * The scope of this variable. Is never null.
72       */
73      private Scope scope;
74  
75      /**
76       * Constructor.
77       *
78       * @param name the name of the variable, not null.
79       * @param value the value of the variable.
80       * @param scope the scope of the variable, not null.
81       */
82      public Variable(
83              QualifiedName name,
84              Object value,
85              Scope scope)
86      {
87          if (name == null)
88          {
89              throw new NullPointerException("name must not be null");
90          }
91          if (scope == null)
92          {
93              throw new NullPointerException("scope must not be null");
94          }
95          this.name = name;
96          this.value = value;
97          this.scope = scope;
98      }
99  
100     /**
101      * Returns the name of the variable.
102      *
103      * @return the name of the variable, not null.
104      */
105     public QualifiedName getName()
106     {
107         return name;
108     }
109 
110     /**
111      * Returns the value of the variable,
112      * @return the value of the variable, may be null.
113      */
114     public Object getValue()
115     {
116         return value;
117     }
118 
119     /**
120      * Sets the value of the variable.
121      *
122      * @param value the value to set in the variable.
123      */
124     public void setValue(Object value)
125     {
126         this.value = value;
127     }
128 
129     /**
130      * Returns the scope of the variable.
131      *
132      * @return the scope of the variable, not null.
133      */
134     public Scope getScope()
135     {
136         return scope;
137     }
138 
139     @Override
140     public int hashCode()
141     {
142         final int prime = 31;
143         int result = 1;
144         result = prime * result + ((name == null) ? 0 : name.hashCode());
145         result = prime * result + ((scope == null) ? 0 : scope.hashCode());
146         result = prime * result + ((value == null) ? 0 : value.hashCode());
147         return result;
148     }
149 
150     @Override
151     public boolean equals(Object obj)
152     {
153         if (this == obj)
154         {
155             return true;
156         }
157         if (obj == null)
158         {
159             return false;
160         }
161         if (getClass() != obj.getClass())
162         {
163             return false;
164         }
165 
166         final Variable other = (Variable) obj;
167 
168         if (name == null)
169         {
170             if (other.name != null)
171             {
172                 return false;
173             }
174         }
175         else if (!name.equals(other.name))
176         {
177             return false;
178         }
179 
180         if (scope == null)
181         {
182             if (other.scope != null)
183             {
184                 return false;
185             }
186         }
187         else if (!scope.equals(other.scope))
188         {
189             return false;
190         }
191 
192         if (value == null)
193         {
194             if (other.value != null)
195             {
196                 return false;
197             }
198         }
199         else if (!value.equals(other.value))
200         {
201             return false;
202         }
203 
204         return true;
205     }
206 
207     @Override
208     public String toString()
209     {
210         StringBuilder result = new StringBuilder();
211         result.append("(name=").append(name)
212               .append(", value=").append(value)
213               .append(", scope=").append(scope)
214               .append(")");
215         return result.toString();
216     }
217 }