001 /*
002 * The MIT License
003 * Copyright (c) 2012 Microsoft Corporation
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023
024 package microsoft.exchange.webservices.data.security;
025
026 import javax.xml.stream.XMLStreamConstants;
027
028 /**
029 * The Class XmlNodeType.
030 */
031 public class XmlNodeType implements XMLStreamConstants {
032
033 /**
034 * The node type.
035 */
036 public int nodeType;
037
038 /**
039 * Instantiates a new Xml node type.
040 *
041 * @param nodeType The node type.
042 */
043 public XmlNodeType(int nodeType) {
044 this.nodeType = nodeType;
045 }
046
047 /**
048 * Returns a string representation of the object. In general, the
049 * <code>toString</code> method returns a string that "textually represents"
050 * this object. The result should be a concise but informative
051 * representation that is easy for a person to read. It is recommended that
052 * all subclasses override this method.
053 * <p/>
054 * The <code>toString</code> method for class <code>Object</code> returns a
055 * string consisting of the name of the class of which the object is an
056 * instance, the at-sign character `<code>@</code>', and the unsigned
057 * hexadecimal representation of the hash code of the object. In other
058 * words, this method returns a string equal to the value of: <blockquote>
059 * <p/>
060 * <pre>
061 * getClass().getName() + '@' + Integer.toHexString(hashCode())
062 * </pre>
063 * <p/>
064 * </blockquote>
065 *
066 * @return a string representation of the object.
067 */
068 @Override
069 public String toString() {
070 return getString(nodeType);
071 }
072
073 /**
074 * Sets the node type.
075 *
076 * @param nodeType the new node type
077 */
078 public void setNodeType(int nodeType) {
079 this.nodeType = nodeType;
080 }
081
082 /**
083 * Gets the node type.
084 *
085 * @return the node type
086 */
087 public int getNodeType() {
088 return nodeType;
089 }
090
091 /**
092 * Gets the string.
093 *
094 * @param nodeType the node type
095 * @return the string
096 */
097 public static String getString(int nodeType) {
098 switch (nodeType) {
099 case XMLStreamConstants.ATTRIBUTE:
100 return "ATTRIBUTE";
101 case XMLStreamConstants.CDATA:
102 return "CDATA";
103 case XMLStreamConstants.CHARACTERS:
104 return "CHARACTERS";
105 case XMLStreamConstants.COMMENT:
106 return "COMMENT";
107 case XMLStreamConstants.DTD:
108 return "DTD";
109 case XMLStreamConstants.END_DOCUMENT:
110 return "END_DOCUMENT";
111 case XMLStreamConstants.END_ELEMENT:
112 return "END_ELEMENT";
113 case XMLStreamConstants.ENTITY_DECLARATION:
114 return "ENTITY_DECLARATION";
115 case XMLStreamConstants.ENTITY_REFERENCE:
116 return "ENTITY_REFERENCE";
117 case XMLStreamConstants.NAMESPACE:
118 return "NAMESPACE";
119 case XMLStreamConstants.NOTATION_DECLARATION:
120 return "NOTATION_DECLARATION";
121 case XMLStreamConstants.PROCESSING_INSTRUCTION:
122 return "PROCESSING_INSTRUCTION";
123 case XMLStreamConstants.SPACE:
124 return "SPACE";
125 case XMLStreamConstants.START_DOCUMENT:
126 return "START_DOCUMENT";
127 case XMLStreamConstants.START_ELEMENT:
128 return "START_ELEMENT";
129 case 0:
130 return "NONE";
131 default:
132 return "UNKNOWN";
133 }
134 }
135
136 /**
137 * Indicates whether some other object is "equal to" this one.
138 * <p/>
139 * The <code>equals</code> method implements an equivalence relation on
140 * non-null object references:
141 * <ul>
142 * <li>It is <i>reflexive</i>: for any non-null reference value
143 * <code>x</code>, <code>x.equals(x)</code> should return <code>true</code>.
144 * <li>It is <i>symmetric</i>: for any non-null reference values
145 * <code>x</code> and <code>y</code>, <code>x.equals(y)</code> should return
146 * <code>true</code> if and only if <code>y.equals(x)</code> returns
147 * <code>true</code>.
148 * <li>It is <i>transitive</i>: for any non-null reference values
149 * <code>x</code>, <code>y</code>, and <code>z</code>, if
150 * <code>x.equals(y)</code> returns <code>true</code> and
151 * <code>y.equals(z)</code> returns <code>true</code>, then
152 * <code>x.equals(z)</code> should return <code>true</code>.
153 * <li>It is <i>consistent</i>: for any non-null reference values
154 * <code>x</code> and <code>y</code>, multiple invocations of
155 * <tt>x.equals(y)</tt> consistently return <code>true</code> or
156 * consistently return <code>false</code>, provided no information used in
157 * <code>equals</code> comparisons on the objects is modified.
158 * <li>For any non-null reference value <code>x</code>,
159 * <code>x.equals(null)</code> should return <code>false</code>.
160 * </ul>
161 * <p/>
162 * The <tt>equals</tt> method for class <code>Object</code> implements the
163 * most discriminating possible equivalence relation on objects; that is,
164 * for any non-null reference values <code>x</code> and <code>y</code>, this
165 * method returns <code>true</code> if and only if <code>x</code> and
166 * <code>y</code> refer to the same object (<code>x == y</code> has the
167 * value <code>true</code>).
168 * <p/>
169 * Note that it is generally necessary to override the <tt>hashCode</tt>
170 * method whenever this method is overridden, so as to maintain the general
171 * contract for the <tt>hashCode</tt> method, which states that equal
172 * objects must have equal hash codes.
173 *
174 * @param obj the reference object with which to compare.
175 * @return if this object is the same as the obj argument; otherwise.
176 * @see #hashCode()
177 * @see java.util.Hashtable
178 */
179 @Override
180 public boolean equals(Object obj) {
181
182 if (this == obj) {
183 return true;
184 }
185 if (obj instanceof XmlNodeType) {
186 XmlNodeType other = (XmlNodeType) obj;
187 return this.nodeType == other.nodeType;
188 } else {
189 return super.equals(obj);
190 }
191 }
192
193 /**
194 * Returns a hash code value for the object. This method is supported for
195 * the benefit of hashtables such as those provided by
196 * <code>java.util.Hashtable</code>.
197 * <p/>
198 * The general contract of <code>hashCode</code> is:
199 * <ul>
200 * <li>Whenever it is invoked on the same object more than once during an
201 * execution of a Java application, the <tt>hashCode</tt> method must
202 * consistently return the same integer, provided no information used in
203 * <tt>equals</tt> comparisons on the object is modified. This integer need
204 * not remain consistent from one execution of an application to another
205 * execution of the same application.
206 * <li>If two objects are equal according to the <tt>equals(Object)</tt>
207 * method, then calling the <code>hashCode</code> method on each of the two
208 * objects must produce the same integer result.
209 * <li>It is <em>not</em> required that if two objects are unequal according
210 * to the {@link Object#equals(Object)} method, then
211 * calling the <tt>hashCode</tt> method on each of the two objects must
212 * produce distinct integer results. However, the programmer should be aware
213 * that producing distinct integer results for unequal objects may improve
214 * the performance of hashtables.
215 * </ul>
216 * <p/>
217 * As much as is reasonably practical, the hashCode method defined by class
218 * <tt>Object</tt> does return distinct integers for distinct objects. (This
219 * is typically implemented by converting the internal address of the object
220 * into an integer, but this implementation technique is not required by the
221 * Java<font size="-2"><sup>TM</sup></font> programming language.)
222 *
223 * @return a hash code value for this object.
224 * @see Object#equals(Object)
225 * @see java.util.Hashtable
226 */
227 @Override
228 public int hashCode() {
229 return this.nodeType;
230 }
231 }