View Javadoc

1   package org.apache.torque.om;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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.7.4.2 2004/05/20 04:36:05 seade 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             // check against a StringKey. Two keys are equal, if their
121             // internal keys equivalent.
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 }