All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.assertj.swing.junit.xml.XmlAttribute Maven / Gradle / Ivy

The newest version!
/*
 * 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 java.util.Objects;

import static org.assertj.core.util.Objects.HASH_CODE_PRIME;
import static org.assertj.core.util.Objects.hashCodeFor;

/**
 * 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, String.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, String.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 (!Objects.deepEquals(name, other.name))
      return false;
    return Objects.deepEquals(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 getClass().getSimpleName() + "[" +
           "name='" + name + '\'' +
           ",value='" + value + '\'' +
           ']';
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy