%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
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 | 0 | { |
43 | 0 | } |
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 | 480 | { |
52 | 480 | this.key = key; |
53 | 480 | } |
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 | 0 | { |
62 | 0 | if (key != null) |
63 | { |
|
64 | 0 | this.key = key.getValue(); |
65 | } |
|
66 | else |
|
67 | { |
|
68 | 0 | this.key = null; |
69 | } |
|
70 | 0 | } |
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 | 0 | this.key = key; |
80 | 0 | } |
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 | 0 | if (key != null) |
90 | { |
|
91 | 0 | this.key = key.getValue(); |
92 | } |
|
93 | else |
|
94 | { |
|
95 | 0 | this.key = null; |
96 | } |
|
97 | 0 | } |
98 | ||
99 | /** |
|
100 | * Access the underlying String object. |
|
101 | * |
|
102 | * @return a <code>String</code> value |
|
103 | */ |
|
104 | public String getString() |
|
105 | { |
|
106 | 0 | 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 | 54 | boolean isEqual = false; |
120 | ||
121 | 54 | if (key != null) |
122 | { |
|
123 | 54 | if (keyObj instanceof String) |
124 | { |
|
125 | 0 | isEqual = keyObj.equals(key); |
126 | } |
|
127 | // check against a StringKey. Two keys are equal, if their |
|
128 | // internal keys equivalent. |
|
129 | 54 | else if (keyObj instanceof StringKey) |
130 | { |
|
131 | 54 | Object obj = ((StringKey) keyObj).getValue(); |
132 | 54 | isEqual = key.equals(obj); |
133 | } |
|
134 | } |
|
135 | 54 | 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 | 42 | if (key != null) |
146 | { |
|
147 | 42 | return (String) key; |
148 | } |
|
149 | 0 | return ""; |
150 | } |
|
151 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |