View Javadoc

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