001: package org.apache.torque.om;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: /**
023: * This class can be used as an ObjectKey to uniquely identify an
024: * object within an application where the id consists
025: * of a single entity such a GUID or the value of a db row's primary key.
026: *
027: * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
028: * @version $Id: StringKey.java 473821 2006-11-11 22:37:25Z tv $
029: */
030: public class StringKey extends SimpleKey {
031: /**
032: * Serial version
033: */
034: private static final long serialVersionUID = 5109588772086713341L;
035:
036: /**
037: * Creates an SimpleKey whose internal representation will be
038: * set later, through a set method
039: */
040: public StringKey() {
041: }
042:
043: /**
044: * Creates a StringKey whose internal representation is a String
045: *
046: * @param key the key value
047: */
048: public StringKey(String key) {
049: this .key = key;
050: }
051:
052: /**
053: * Creates a StringKey that is equivalent to key.
054: *
055: * @param key the key value
056: */
057: public StringKey(StringKey key) {
058: if (key != null) {
059: this .key = key.getValue();
060: } else {
061: this .key = null;
062: }
063: }
064:
065: /**
066: * Sets the internal representation to a String
067: *
068: * @param key the key value
069: */
070: public void setValue(String key) {
071: this .key = key;
072: }
073:
074: /**
075: * Sets the internal representation to the same object used by key.
076: *
077: * @param key the key value
078: */
079: public void setValue(StringKey key) {
080: if (key != null) {
081: this .key = key.getValue();
082: } else {
083: this .key = null;
084: }
085: }
086:
087: /**
088: * Access the underlying String object.
089: *
090: * @return a <code>String</code> value
091: */
092: public String getString() {
093: return (String) key;
094: }
095:
096: /**
097: * keyObj is equal to this StringKey if keyObj is a StringKey or String
098: * that contains the same information this key contains. Two ObjectKeys
099: * that both contain null values are not considered equal.
100: *
101: * @param keyObj the comparison value
102: * @return whether the two objects are equal
103: */
104: public boolean equals(Object keyObj) {
105: boolean isEqual = false;
106:
107: if (key != null) {
108: if (keyObj instanceof String) {
109: isEqual = keyObj.equals(key);
110: }
111: // check against a StringKey. Two keys are equal, if their
112: // internal keys equivalent.
113: else if (keyObj instanceof StringKey) {
114: Object obj = ((StringKey) keyObj).getValue();
115: isEqual = key.equals(obj);
116: }
117: }
118: return isEqual;
119: }
120:
121: /**
122: * get a String representation
123: *
124: * @return a String representation of an empty String if the value is null
125: */
126: public String toString() {
127: if (key != null) {
128: return (String) key;
129: }
130: return "";
131: }
132: }
|