View Javadoc

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  import java.sql.Types;
23  
24  /**
25   * This class can be used as an ObjectKey to uniquely identify an
26   * object within an application where the id  consists
27   * of a single entity such a GUID or the value of a db row's primary key.
28   *
29   * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
30   * @version $Id: StringKey.java 1351125 2012-06-17 16:51:03Z tv $
31   */
32  public class StringKey extends SimpleKey
33  {
34      /**
35       * Serial version
36       */
37      private static final long serialVersionUID = 5109588772086713341L;
38  
39      /**
40       * Creates an SimpleKey whose internal representation will be
41       * set later, through a set method
42       */
43      public StringKey()
44      {
45          // empty
46      }
47  
48      /**
49       * Creates a StringKey whose internal representation is a String
50       *
51       * @param key the key value
52       */
53      public StringKey(String key)
54      {
55          this.key = key;
56      }
57  
58      /**
59       * Creates a StringKey that is equivalent to key.
60       *
61       * @param key the key value
62       */
63      public StringKey(StringKey key)
64      {
65          if (key != null)
66          {
67              this.key = key.getValue();
68          }
69          else
70          {
71              this.key = null;
72          }
73      }
74  
75      /**
76       * Sets the internal representation to a String
77       *
78       * @param key the key value
79       */
80      public void setValue(String key)
81      {
82          this.key = key;
83      }
84  
85      /**
86       * Sets the internal representation to the same object used by key.
87       *
88       * @param key the key value
89       */
90      public void setValue(StringKey key)
91      {
92          if (key != null)
93          {
94              this.key = key.getValue();
95          }
96          else
97          {
98              this.key = null;
99          }
100     }
101 
102     /**
103      * Access the underlying String object.
104      *
105      * @return a <code>String</code> value
106      */
107     public String getString()
108     {
109         return (String) key;
110     }
111 
112     /**
113      * Returns the JDBC type of the key
114      * as defined in <code>java.sql.Types</code>.
115      *
116      * @return <code>Types.VARCHAR</code>.
117      */
118     public int getJdbcType()
119     {
120         return Types.VARCHAR;
121     }
122 
123     /**
124      * Get a String representation of this key.
125      *
126      * @return a String representation of this key,
127      *         or an empty String if the value is null.
128      */
129     @Override
130     public String toString()
131     {
132         if (key != null)
133         {
134             return (String) key;
135         }
136         return "";
137     }
138 }