001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.usercache.lib;
018:
019: import org.objectweb.speedo.usercache.api.UserCache;
020:
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: /**
025: * Is a basic UserCacheView implementation using WeakReference in order to
026: * not disturb the real cache of persistent object.
027: *
028: * @author S.Chassande-Barrioz
029: */
030: public class UserCacheImpl implements UserCache {
031:
032: public final static int DEFAULT_SIZE = 50;
033:
034: /**
035: * Contains the association user key TO object identifier
036: */
037: protected Map key2oid;
038:
039: /**
040: * Contains the association object identifier TO user key
041: */
042: protected Map oid2key;
043:
044: /**
045: * The name of the user cache
046: */
047: protected String name;
048:
049: /**
050: * The identifier of the user cache (light weight pattern)
051: */
052: protected int id;
053:
054: /**
055: * Indicates if the user cache is active
056: */
057: protected boolean active;
058:
059: /**
060: * is the list of field name used for the cache index
061: */
062: protected String[] fields;
063:
064: /**
065: * Allocates an user cache with the default size.
066: * @see #DEFAULT_SIZE
067: * @see #UserCacheImpl(int)
068: */
069: public UserCacheImpl() {
070: this (DEFAULT_SIZE);
071: }
072:
073: /**
074: * Allocates an user cache with a particular size. The size is not a maximal
075: * size, but serves only for the underlying underlying hash structure.
076: *
077: * @param size is the size of the cache.
078: */
079: public UserCacheImpl(int size) {
080: key2oid = new HashMap(size);
081: oid2key = new HashMap(size);
082: }
083:
084: public synchronized Object bind(Object key, Object oid) {
085: Object oldKey = oid2key.put(oid, key);
086: if (oldKey != null && !oldKey.equals(key)) {
087: //rebind with another key
088: key2oid.remove(oldKey);
089: } else {
090: key2oid.put(key, oid);
091: }
092: return oldKey;
093: }
094:
095: public synchronized Object unbindFromKey(Object key) {
096: Object oldOID = key2oid.remove(key);
097: if (oldOID != null) {
098: oid2key.remove(oldOID);
099: }
100: return oldOID;
101: }
102:
103: public synchronized Object unbindFromOID(Object oid) {
104: Object oldKey = oid2key.remove(oid);
105: if (oldKey != null) {
106: key2oid.remove(oldKey);
107: }
108: return oldKey;
109: }
110:
111: public synchronized Object lookup(Object key) {
112: return key2oid.get(key);
113: }
114:
115: public int getId() {
116: return id;
117: }
118:
119: public void setId(int _id) {
120: this .id = _id;
121: }
122:
123: public String[] getIndexFieldNames() {
124: return fields;
125: }
126:
127: public void setIndexFieldNames(String[] fns) {
128: this .fields = fns;
129: }
130:
131: public String getName() {
132: return name;
133: }
134:
135: public void setName(String n) {
136: this .name = n;
137: }
138:
139: public boolean isActive() {
140: return active;
141: }
142:
143: public void setActive(boolean a) {
144: this.active = a;
145: }
146: }
|