001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.core.util.key;
009
010/**
011 * This is an effectively hashed string key for use hashed data structures.
012 * @author Rusi Popov (popovr@mdatools.net)
013 */
014public final class StringKey {
015
016  private final String string;
017
018  /**
019   * caches the hash code so it is not calculated all the time
020   */
021  private final int hash;
022
023  public StringKey(String string) {
024    this.string = string;
025    this.hash = Hash.hash(string);
026  }
027
028  public int hashCode() {
029    return hash;
030  }
031
032  public boolean equals(Object obj) {
033    boolean result;
034
035    result = obj == this
036             || obj != null
037                && obj.getClass() == StringKey.class
038                && ( string == null && ((StringKey) obj).string == null
039                     || string != null && string.equals( ((StringKey) obj).string ) );
040    return result;
041  }
042
043  public String toString() {
044    return getClass().getSimpleName()+"{string:"+string+"}";
045  }
046}