KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > usercache > lib > UserCacheKey


1 /**
2  * Copyright (C) 2001-2005 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * 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  */

24 package org.objectweb.speedo.usercache.lib;
25
26 /**
27  * Defines a key of an user cache. Several elements can compose the key.
28  * The comparaison (equals and hashCode) are based on the key elements.
29  *
30  * @author S.Chassande-Barrioz
31  */

32 public class UserCacheKey {
33
34     /**
35      * The elements composing the key
36      */

37     private Object JavaDoc[] elements;
38     
39     public UserCacheKey(int size) {
40         elements = new Object JavaDoc[size];
41     }
42     
43     public UserCacheKey(Object JavaDoc[] elements) {
44         this.elements = elements;
45     }
46     
47     public void setPart(int index, Object JavaDoc o) {
48         elements[index] = o;
49     }
50     
51     /**
52      *
53      * @return the hashCode of the first element
54      */

55     public int hashCode() {
56         if (elements.length > 0 && elements[0] != null) {
57             return elements[0].hashCode();
58         } else {
59             return elements.length;
60         }
61     }
62     
63     /**
64      * Compares to an UserCacheKey instance.
65      * @see java.lang.Object#equals(java.lang.Object)
66      */

67     public boolean equals(Object JavaDoc o) {
68         if (!(o instanceof UserCacheKey)) {
69             return false;
70         }
71         UserCacheKey uck = (UserCacheKey) o;
72         if (uck.elements.length != elements.length) {
73             return false;
74         }
75         for (int i = 0; i < elements.length; i++) {
76             if ((elements[i] == null && uck.elements[i] != null)
77                     || (elements[i] != null && !elements[i].equals(uck.elements[i]))) {
78                 return false;
79             }
80         }
81         return true;
82     }
83     
84
85 }
86
Popular Tags