001: /*
002: * $RCSfile: CaselessStringKey.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.2 $
009: * $Date: 2006/06/16 22:52:04 $
010: * $State: Exp $
011: */
012: package javax.media.jai.util;
013:
014: import java.io.Serializable;
015: import java.util.Locale;
016:
017: /**
018: * Class to use as the key in a <code>java.util.Map</code>.
019: * The case of the name is maintained but the <code>equals()</code>
020: * method performs case-insensitive comparison.
021: *
022: * @see javax.media.jai.PropertySourceImpl
023: * @see java.util.Map
024: *
025: * @since JAI 1.1
026: */
027: public final class CaselessStringKey implements Cloneable, Serializable {
028:
029: private String name;
030: private String lowerCaseName;
031:
032: /**
033: * Creates a <code>CaselessStringKey</code> for the given name.
034: * The parameter <code>name</code> is stored by reference.
035: *
036: * @throws IllegalArgumentException if <code>name</code> is
037: * <code>null</code>.
038: */
039: public CaselessStringKey(String name) {
040: setName(name);
041: }
042:
043: /**
044: * Returns a hash code value for the <code>CaselessStringKey</code>.
045: */
046: public int hashCode() {
047: return lowerCaseName.hashCode();
048: }
049:
050: /**
051: * Returns the internal name by reference.
052: */
053: public String getName() {
054: return name;
055: }
056:
057: /**
058: * Returns a lower case version of the internal name.
059: */
060: private String getLowerCaseName() {
061: return lowerCaseName;
062: }
063:
064: /**
065: * Stores the parameter by reference in the internal name.
066: *
067: * @throws IllegalArgumentException if <code>name</code> is
068: * <code>null</code>.
069: */
070: public void setName(String name) {
071: if (name == null) {
072: throw new IllegalArgumentException(JaiI18N
073: .getString("CaselessStringKey0"));
074: }
075: this .name = name;
076: lowerCaseName = name.toLowerCase(Locale.ENGLISH);
077: }
078:
079: /**
080: * Returns a clone of the <code>CaselessStringKey</code> as an
081: * <code>Object</code>.
082: */
083: public Object clone() {
084: try {
085: return super .clone();
086: } catch (CloneNotSupportedException e) {
087: // this shouldn't happen, since object is Cloneable
088: throw new InternalError();
089: }
090: }
091:
092: /**
093: * Whether another <code>Object</code> equals this one. This will obtain
094: * if and only if the parameter is non-<code>null</code> and is a
095: * <code>CaselessStringKey</code> whose lower case name equals the
096: * lower case name of this <code>CaselessStringKey</code>.
097: */
098: public boolean equals(Object o) {
099: if (o != null && o instanceof CaselessStringKey) {
100: return lowerCaseName.equals(((CaselessStringKey) o)
101: .getLowerCaseName());
102: }
103: return false;
104: }
105:
106: /**
107: * Returns the value returned by <code>getName()</code>.
108: */
109: public String toString() {
110: return getName();
111: }
112: }
|