    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.model;
    003
    004/**
    005 * An object that is identified amongst objects of the same class by a {@link String} key.
    006 */
    007public class KeyIndexedObject {
    008  private final String key;
    009
    010  protected KeyIndexedObject(final String key) {
    011    if (key == null) {
    012      throw new IllegalArgumentException();
    013    }
    014    this.key = key;
    015  }
    016
    017  /**
    018   * @return the unique key that identifies this object among other instances of the same class
    019   */
    020  public String getKey() {
    021    return key;
    022  }
    023
    024  /* (non-Javadoc)
    025   * @see java.lang.Object#hashCode()
    026   */
    027  @Override
    028  public int hashCode() {
    029    final int prime = 31;
    030    return prime * (prime + getClass().getName().hashCode()) + key.hashCode();
    031  }
    032
    033  /* (non-Javadoc)
    034   * @see java.lang.Object#equals(java.lang.Object)
    035   */
    036  @Override
    037  public boolean equals(Object obj) {
    038    if (this == obj) {
    039      return true;
    040    }
    041    if (obj == null) {
    042      return false;
    043    }
    044    if (getClass() != obj.getClass()) {
    045      return false;
    046    }
    047    KeyIndexedObject other = (KeyIndexedObject) obj;
    048    if (!key.equals(other.key)) {
    049      return false;
    050    }
    051    return true;
    052  }
    053
    054}