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