KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > om > StringKey


1 package org.apache.torque.om;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements. See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership. The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied. See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
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 JavaDoc 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 JavaDoc 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 JavaDoc getString()
105     {
106         return (String JavaDoc) 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 JavaDoc keyObj)
118     {
119         boolean isEqual = false;
120
121         if (key != null)
122         {
123             if (keyObj instanceof String JavaDoc)
124             {
125                 isEqual = keyObj.equals(key);
126             }
127             // check against a StringKey. Two keys are equal, if their
128
// internal keys equivalent.
129
else if (keyObj instanceof StringKey)
130             {
131                 Object JavaDoc 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 JavaDoc toString()
144     {
145         if (key != null)
146         {
147             return (String JavaDoc) key;
148         }
149         return "";
150     }
151 }
152
Popular Tags