1 package org.apache.torque.om;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 /***
21 * This class can be used as an ObjectKey to uniquely identify an
22 * object within an application where the id consists
23 * of a single entity such a GUID or the value of a db row's primary key.
24 *
25 * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
26 * @version $Id: StringKey.java,v 1.9 2005/01/31 19:43:56 tfischer Exp $
27 */
28 public class StringKey extends SimpleKey
29 {
30 /***
31 * Creates an SimpleKey whose internal representation will be
32 * set later, through a set method
33 */
34 public StringKey()
35 {
36 }
37
38 /***
39 * Creates a StringKey whose internal representation is a String
40 *
41 * @param key the key value
42 */
43 public StringKey(String key)
44 {
45 this.key = key;
46 }
47
48 /***
49 * Creates a StringKey that is equivalent to key.
50 *
51 * @param key the key value
52 */
53 public StringKey(StringKey key)
54 {
55 if (key != null)
56 {
57 this.key = key.getValue();
58 }
59 else
60 {
61 this.key = null;
62 }
63 }
64
65 /***
66 * Sets the internal representation to a String
67 *
68 * @param key the key value
69 */
70 public void setValue(String key)
71 {
72 this.key = key;
73 }
74
75 /***
76 * Sets the internal representation to the same object used by key.
77 *
78 * @param key the key value
79 */
80 public void setValue(StringKey key)
81 {
82 if (key != null)
83 {
84 this.key = key.getValue();
85 }
86 else
87 {
88 this.key = null;
89 }
90 }
91
92 /***
93 * Access the underlying String object.
94 *
95 * @return a <code>String</code> value
96 */
97 public String getString()
98 {
99 return (String) key;
100 }
101
102 /***
103 * keyObj is equal to this StringKey if keyObj is a StringKey or String
104 * that contains the same information this key contains. Two ObjectKeys
105 * that both contain null values are not considered equal.
106 *
107 * @param keyObj the comparison value
108 * @return whether the two objects are equal
109 */
110 public boolean equals(Object keyObj)
111 {
112 boolean isEqual = false;
113
114 if (key != null)
115 {
116 if (keyObj instanceof String)
117 {
118 isEqual = keyObj.equals(key);
119 }
120
121
122 else if (keyObj instanceof StringKey)
123 {
124 Object obj = ((StringKey) keyObj).getValue();
125 isEqual = key.equals(obj);
126 }
127 }
128 return isEqual;
129 }
130
131 /***
132 * get a String representation
133 *
134 * @return a String representation of an empty String if the value is null
135 */
136 public String toString()
137 {
138 if (key != null)
139 {
140 return (String) key;
141 }
142 return "";
143 }
144 }