001 /*
002 * Created on Apr 3, 2009
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2009 the original author or authors.
015 */
016 package org.fest.swing.junit.xml;
017
018 import static java.lang.String.valueOf;
019 import static org.fest.util.Objects.*;
020 import static org.fest.util.Strings.concat;
021 import static org.fest.util.Strings.quote;
022
023 /**
024 * Understands an attribute of a <code>{@link XmlNode}</code>. This class is intended for internal use only. It only
025 * provides the necessary functionality needed by the FEST-Swing JUnit extension.
026 *
027 * @author Alex Ruiz
028 */
029 public class XmlAttribute {
030
031 private final String name;
032 private final String value;
033
034 /**
035 * Creates a new <code>{@link XmlAttributeBuilder}</code>.
036 * @param name the name of the attribute that the created builder will build.
037 * @return the created <code>XmlAttributeBuilder</code>.
038 */
039 public static XmlAttributeBuilder name(String name) {
040 return new XmlAttributeBuilder(name);
041 }
042
043 /**
044 * Understands creation of <code>{@link XmlAttribute}</code>s.
045 *
046 * @author Alex Ruiz
047 */
048 public static class XmlAttributeBuilder {
049 private final String name;
050
051 XmlAttributeBuilder(String name) {
052 this.name = name;
053 }
054
055 /**
056 * Creates a new <code>{@link XmlAttribute}</code> using the attribute name passed when this builder was created and
057 * the given value.
058 * @param value the value of the attribute to create.
059 * @return the created <code>XmlAttribute</code>.
060 */
061 public XmlAttribute value(String value) {
062 return new XmlAttribute(name, value);
063 }
064
065 /**
066 * Creates a new <code>{@link XmlAttribute}</code> using the attribute name passed when this builder was created and
067 * the given value.
068 * @param value the value of the attribute to create.
069 * @return the created <code>XmlAttribute</code>.
070 */
071 public XmlAttribute value(long value) {
072 return new XmlAttribute(name, valueOf(value));
073 }
074
075 /**
076 * Creates a new <code>{@link XmlAttribute}</code> using the attribute name passed when this builder was created and
077 * the given value.
078 * @param value the value of the attribute to create.
079 * @return the created <code>XmlAttribute</code>.
080 */
081 public XmlAttribute value(double value) {
082 return new XmlAttribute(name, valueOf(value));
083 }
084 }
085
086 XmlAttribute(String name, String value) {
087 this.name = name;
088 this.value = value;
089 }
090
091 /**
092 * Returns the name of this attribute.
093 * @return the name of this attribute.
094 */
095 public String name() { return name; }
096
097 /**
098 * Returns the value of this attribute.
099 * @return the value of this attribute.
100 */
101 public String value() { return value; }
102
103 /**
104 * Indicates whether the given <code>Object</code> is equal to this attribute. To be equal, the given object should be
105 * a <code>{@link XmlAttribute}</code> with its name and value equal to the ones in this attribute.
106 * @param obj the <code>Object</code> to compare to.
107 * @return <code>true</code> if the given <code>Object</code> is equal to this attribute, <code>false</code>
108 * otherwise.
109 */
110 @Override public boolean equals(Object obj) {
111 if (this == obj) return true;
112 if (obj == null) return false;
113 if (getClass() != obj.getClass()) return false;
114 XmlAttribute other = (XmlAttribute) obj;
115 if (!areEqual(name, other.name)) return false;
116 return areEqual(value, other.value);
117 }
118
119 /**
120 * Returns the hash code of this attribute, based on its name and value.
121 * @return the hash code of this attribute.
122 */
123 @Override public int hashCode() {
124 int result = 1;
125 result = HASH_CODE_PRIME * result + hashCodeFor(name);
126 result = HASH_CODE_PRIME * result + hashCodeFor(value);
127 return result;
128 }
129
130 /**
131 * Returns a <code>String</code> representation of this attribute.
132 * @return a <code>String</code> representation of this attribute.
133 */
134 @Override public String toString() {
135 return concat(
136 getClass().getSimpleName(), "[",
137 "name=", quote(name), ",",
138 "value=", quote(value), "]"
139 );
140 }
141 }