KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2001-2004 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 package org.objectweb.speedo.usercache.lib;
19
20 import org.objectweb.speedo.usercache.api.UserCache;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * Is a basic UserCacheView implementation using WeakReference in order to
27  * not disturb the real cache of persistent object.
28  *
29  * @author S.Chassande-Barrioz
30  */

31 public class UserCacheImpl implements UserCache {
32
33     public final static int DEFAULT_SIZE = 50;
34     
35     /**
36      * Contains the association user key TO object identifier
37      */

38     protected Map JavaDoc key2oid;
39
40     /**
41      * Contains the association object identifier TO user key
42      */

43     protected Map JavaDoc oid2key;
44
45     /**
46      * The name of the user cache
47      */

48     protected String JavaDoc name;
49     
50     /**
51      * The identifier of the user cache (light weight pattern)
52      */

53     protected int id;
54     
55     /**
56      * Indicates if the user cache is active
57      */

58     protected boolean active;
59     
60     /**
61      * is the list of field name used for the cache index
62      */

63     protected String JavaDoc[] fields;
64
65     /**
66      * Allocates an user cache with the default size.
67      * @see #DEFAULT_SIZE
68      * @see #UserCacheImpl(int)
69      */

70     public UserCacheImpl() {
71         this(DEFAULT_SIZE);
72     }
73
74     /**
75      * Allocates an user cache with a particular size. The size is not a maximal
76      * size, but serves only for the underlying underlying hash structure.
77      *
78      * @param size is the size of the cache.
79      */

80     public UserCacheImpl(int size) {
81         key2oid = new HashMap JavaDoc(size);
82         oid2key = new HashMap JavaDoc(size);
83     }
84
85     public synchronized Object JavaDoc bind(Object JavaDoc key, Object JavaDoc oid) {
86         Object JavaDoc oldKey = oid2key.put(oid, key);
87         if (oldKey != null && !oldKey.equals(key)) {
88             //rebind with another key
89
key2oid.remove(oldKey);
90         } else {
91             key2oid.put(key, oid);
92         }
93         return oldKey;
94     }
95
96     public synchronized Object JavaDoc unbindFromKey(Object JavaDoc key) {
97         Object JavaDoc oldOID = key2oid.remove(key);
98         if (oldOID != null) {
99             oid2key.remove(oldOID);
100         }
101         return oldOID;
102     }
103     
104     public synchronized Object JavaDoc unbindFromOID(Object JavaDoc oid) {
105         Object JavaDoc oldKey = oid2key.remove(oid);
106         if (oldKey != null) {
107             key2oid.remove(oldKey);
108         }
109         return oldKey;
110     }
111     
112     public synchronized Object JavaDoc lookup(Object JavaDoc key) {
113         return key2oid.get(key);
114     }
115     
116     public int getId() {
117         return id;
118     }
119     
120     public void setId(int _id) {
121         this.id = _id;
122     }
123     public String JavaDoc[] getIndexFieldNames() {
124         return fields;
125     }
126     public void setIndexFieldNames(String JavaDoc[] fns) {
127         this.fields = fns;
128     }
129     public String JavaDoc getName() {
130         return name;
131     }
132     public void setName(String JavaDoc n) {
133         this.name = n;
134     }
135     public boolean isActive() {
136         return active;
137     }
138     public void setActive(boolean a) {
139         this.active = a;
140     }
141 }
142
Popular Tags