KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > distribution > GlobalID


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.distribution;
57
58 import java.io.Serializable JavaDoc;
59 import java.util.Arrays JavaDoc;
60 import java.util.Collections JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import org.apache.commons.lang.builder.EqualsBuilder;
65 import org.apache.commons.lang.builder.HashCodeBuilder;
66 import org.apache.commons.lang.builder.ToStringBuilder;
67 import org.objectstyle.cayenne.util.IDUtil;
68 import org.objectstyle.cayenne.util.Util;
69
70 /**
71  * A portable global identifier for persistent objects.
72  *
73  * @since 1.2
74  * @author Andrus Adamchik
75  */

76 // TODO: this implementation is likely to change
77
public class GlobalID implements Serializable JavaDoc {
78
79     protected String JavaDoc entityName;
80     protected Map JavaDoc objectIdKeys;
81
82     protected byte[] key;
83
84     /**
85      * Creates a TEMPORARY id.
86      */

87     // TODO: this may be confusing - there is nothing in constructor that hints that this
88
// is a temp id
89
public GlobalID(String JavaDoc entityName) {
90         this.entityName = entityName;
91         this.key = IDUtil.pseudoUniqueByteSequence16();
92     }
93
94     public GlobalID(String JavaDoc entityName, byte[] key) {
95         this.entityName = entityName;
96         this.key = key;
97     }
98
99     /**
100      * Creates a portable permanent GlobalID.
101      */

102     public GlobalID(String JavaDoc entityName, Map JavaDoc idKeys) {
103         this.entityName = entityName;
104         this.objectIdKeys = Collections.unmodifiableMap(idKeys);
105     }
106
107     public boolean isTemporary() {
108         return key != null;
109     }
110
111     public String JavaDoc getEntityName() {
112         return entityName;
113     }
114
115     public byte[] getKey() {
116         return key;
117     }
118
119     public Map JavaDoc getObjectIdKeys() {
120         return objectIdKeys;
121     }
122
123     public boolean equals(Object JavaDoc object) {
124         if (this == object) {
125             return true;
126         }
127
128         if (!(object instanceof GlobalID)) {
129             return false;
130         }
131
132         GlobalID id = (GlobalID) object;
133
134         if (isTemporary()) {
135             return new EqualsBuilder()
136                     .append(entityName, entityName)
137                     .append(key, id.key)
138                     .isEquals();
139         }
140
141         if (!Util.nullSafeEquals(entityName, id.entityName)) {
142             return false;
143         }
144
145         if (id.objectIdKeys == null && objectIdKeys == null) {
146             return true;
147         }
148
149         if (id.objectIdKeys == null || objectIdKeys == null) {
150             return false;
151         }
152
153         if (id.objectIdKeys.size() != objectIdKeys.size()) {
154             return false;
155         }
156
157         EqualsBuilder builder = new EqualsBuilder();
158         Iterator JavaDoc entries = objectIdKeys.entrySet().iterator();
159         while (entries.hasNext()) {
160             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entries.next();
161
162             Object JavaDoc key = entry.getKey();
163             Object JavaDoc value = entry.getValue();
164             if (value == null) {
165                 if (id.objectIdKeys.get(key) != null || !id.objectIdKeys.containsKey(key)) {
166                     return false;
167                 }
168             }
169             else {
170                 // takes care of comparing primitive arrays, such as byte[]
171
builder.append(value, id.objectIdKeys.get(key));
172                 if (!builder.isEquals()) {
173                     return false;
174                 }
175             }
176         }
177
178         return true;
179     }
180
181     public int hashCode() {
182
183         // TODO: cache hashCode the way ObjectId does
184

185         HashCodeBuilder builder = new HashCodeBuilder(3, 5);
186         builder.append(entityName.hashCode());
187
188         if (key != null) {
189             builder.append(key);
190         }
191
192         if (objectIdKeys != null) {
193             int len = objectIdKeys.size();
194
195             // handle cheap and most common case - single key
196
if (len == 1) {
197                 Iterator JavaDoc entries = objectIdKeys.entrySet().iterator();
198                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entries.next();
199                 builder.append(entry.getKey()).append(entry.getValue());
200             }
201             // handle multiple keys - must sort the keys to use with HashCodeBuilder
202
else {
203                 Object JavaDoc[] keys = objectIdKeys.keySet().toArray();
204                 Arrays.sort(keys);
205
206                 for (int i = 0; i < len; i++) {
207                     // HashCodeBuilder will take care of processing object if it
208
// happens to be a primitive array such as byte[]
209

210                     // also we don't have to append the key hashcode, its index will
211
// work
212
builder.append(i).append(objectIdKeys.get(keys[i]));
213                 }
214             }
215         }
216
217         return builder.toHashCode();
218     }
219
220     public String JavaDoc toString() {
221         ToStringBuilder builder = new ToStringBuilder(this);
222
223         builder.append("entityName", entityName);
224         builder.append("temporary", isTemporary());
225
226         if (isTemporary()) {
227             builder.append("key", key);
228         }
229         else if (objectIdKeys != null) {
230             Iterator JavaDoc it = objectIdKeys.entrySet().iterator();
231             while (it.hasNext()) {
232                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
233                 builder.append(String.valueOf(entry.getKey()), entry.getValue());
234             }
235         }
236         return builder.toString();
237     }
238 }
239
Popular Tags