01: /**
02: * Copyright (C) 2001-2005 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: *
19: *
20: * Authors: S.Chassande-Barrioz.
21: * Created on 19 mars 2005
22: *
23: */package org.objectweb.speedo.usercache.lib;
24:
25: /**
26: * Defines a key of an user cache. Several elements can compose the key.
27: * The comparaison (equals and hashCode) are based on the key elements.
28: *
29: * @author S.Chassande-Barrioz
30: */
31: public class UserCacheKey {
32:
33: /**
34: * The elements composing the key
35: */
36: private Object[] elements;
37:
38: public UserCacheKey(int size) {
39: elements = new Object[size];
40: }
41:
42: public UserCacheKey(Object[] elements) {
43: this .elements = elements;
44: }
45:
46: public void setPart(int index, Object o) {
47: elements[index] = o;
48: }
49:
50: /**
51: *
52: * @return the hashCode of the first element
53: */
54: public int hashCode() {
55: if (elements.length > 0 && elements[0] != null) {
56: return elements[0].hashCode();
57: } else {
58: return elements.length;
59: }
60: }
61:
62: /**
63: * Compares to an UserCacheKey instance.
64: * @see java.lang.Object#equals(java.lang.Object)
65: */
66: public boolean equals(Object o) {
67: if (!(o instanceof UserCacheKey)) {
68: return false;
69: }
70: UserCacheKey uck = (UserCacheKey) o;
71: if (uck.elements.length != elements.length) {
72: return false;
73: }
74: for (int i = 0; i < elements.length; i++) {
75: if ((elements[i] == null && uck.elements[i] != null)
76: || (elements[i] != null && !elements[i]
77: .equals(uck.elements[i]))) {
78: return false;
79: }
80: }
81: return true;
82: }
83:
84: }
|