org.assertj.swing.junit.xml.XmlAttribute Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of assertj-swing-junit Show documentation
Show all versions of assertj-swing-junit Show documentation
JUnit-specific extension for AssertJ-Swing
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2018 the original author or authors.
*/
package org.assertj.swing.junit.xml;
import static java.lang.String.valueOf;
import static org.assertj.core.util.Objects.HASH_CODE_PRIME;
import static org.assertj.core.util.Objects.areEqual;
import static org.assertj.core.util.Objects.hashCodeFor;
import static org.assertj.core.util.Strings.concat;
import static org.assertj.core.util.Strings.quote;
/**
* Understands an attribute of a {@link XmlNode}
. This class is intended for internal use only. It only
* provides the necessary functionality needed by the AssertJ-Swing JUnit extension.
*
* @author Alex Ruiz
*/
public class XmlAttribute {
private final String name;
private final String value;
/**
* Creates a new {@link XmlAttributeBuilder}
.
*
* @param name the name of the attribute that the created builder will build.
* @return the created XmlAttributeBuilder
.
*/
public static XmlAttributeBuilder name(String name) {
return new XmlAttributeBuilder(name);
}
/**
* Understands creation of {@link XmlAttribute}
s.
*
* @author Alex Ruiz
*/
public static class XmlAttributeBuilder {
private final String name;
XmlAttributeBuilder(String name) {
this.name = name;
}
/**
* Creates a new {@link XmlAttribute}
using the attribute name passed when this builder was created and
* the given value.
*
* @param value the value of the attribute to create.
* @return the created XmlAttribute
.
*/
public XmlAttribute value(String value) {
return new XmlAttribute(name, value);
}
/**
* Creates a new {@link XmlAttribute}
using the attribute name passed when this builder was created and
* the given value.
*
* @param value the value of the attribute to create.
* @return the created XmlAttribute
.
*/
public XmlAttribute value(long value) {
return new XmlAttribute(name, valueOf(value));
}
/**
* Creates a new {@link XmlAttribute}
using the attribute name passed when this builder was created and
* the given value.
*
* @param value the value of the attribute to create.
* @return the created XmlAttribute
.
*/
public XmlAttribute value(double value) {
return new XmlAttribute(name, valueOf(value));
}
}
XmlAttribute(String name, String value) {
this.name = name;
this.value = value;
}
/**
* Returns the name of this attribute.
*
* @return the name of this attribute.
*/
public String name() {
return name;
}
/**
* Returns the value of this attribute.
*
* @return the value of this attribute.
*/
public String value() {
return value;
}
/**
* Indicates whether the given Object
is equal to this attribute. To be equal, the given object should be
* a {@link XmlAttribute}
with its name and value equal to the ones in this attribute.
*
* @param obj the Object
to compare to.
* @return true
if the given Object
is equal to this attribute, false
* otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
XmlAttribute other = (XmlAttribute) obj;
if (!areEqual(name, other.name))
return false;
return areEqual(value, other.value);
}
/**
* Returns the hash code of this attribute, based on its name and value.
*
* @return the hash code of this attribute.
*/
@Override
public int hashCode() {
int result = 1;
result = HASH_CODE_PRIME * result + hashCodeFor(name);
result = HASH_CODE_PRIME * result + hashCodeFor(value);
return result;
}
/**
* Returns a String
representation of this attribute.
*
* @return a String
representation of this attribute.
*/
@Override
public String toString() {
return concat(getClass().getSimpleName(), "[", "name=", quote(name), ",", "value=", quote(value), "]");
}
}