You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by fr...@apache.org on 2002/02/09 19:45:11 UTC
cvs commit: jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore SynchronizedStore.java Swap.java Store.java SoftRefMemoryStore.java Reachable.java
froehlich 02/02/09 10:45:11
Modified: simplestore/src/java/org/apache/commons/simplestore
SynchronizedStore.java Swap.java Store.java
SoftRefMemoryStore.java Reachable.java
Log:
removed unnecessary method and added full license
Revision Changes Path
1.5 +110 -72 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SynchronizedStore.java
Index: SynchronizedStore.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SynchronizedStore.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SynchronizedStore.java 21 Jan 2002 22:02:36 -0000 1.4
+++ SynchronizedStore.java 9 Feb 2002 18:45:11 -0000 1.5
@@ -1,108 +1,146 @@
-/*****************************************************************************
- * Copyright (C) The Apache Software Foundation. All rights reserved. *
- * ------------------------------------------------------------------------- *
- * This software is published under the terms of the Apache Software License *
- * version 1.1, a copy of which has been included with this distribution in *
- * the LICENSE file. *
- *****************************************************************************/
-
+/*
+ * The Apache Software License, Version 1.1
+ *
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * nor may "Apache" appear in their name, without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
package org.apache.commons.simplestore;
-import java.util.Set;
+import java.io.IOException;
import java.util.Collection;
import java.util.Map;
-import java.io.IOException;
+import java.util.Set;
/**
- *
- * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
+ *@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- * @author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
+ *@author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
* g-froehlich@gmx.de</a>
- * @version $Id: SynchronizedStore.java,v 1.4 2002/01/21 22:02:36 froehlich Exp $
+ *@version $Id: SynchronizedStore.java,v 1.5 2002/02/09 18:45:11 froehlich Exp $
*/
-final class SynchronizedStore
-implements Store {
-
+final class SynchronizedStore
+ implements Store {
+
private Store store;
-
- /** Creates new SynchronizedStore */
+
+ /**
+ * Creates new SynchronizedStore
+ *
+ *@param store
+ */
public SynchronizedStore(Store store) {
- if(store == null) {
+ if (store == null) {
throw new NullPointerException();
}
this.store = store;
}
-
+
+ /**
+ * Get the object associated to the given unique key.
+ *
+ *@param key the Key Object
+ *@return
+ */
+ public Object get(Object key) {
+ synchronized (store) {
+ return store.get(key);
+ }
+ }
+
+ public boolean isEmpty() {
+ synchronized (store) {
+ return store.isEmpty();
+ }
+ }
+
/**
* Remove the object associated to the given key.
*
- * @param key the Key Object
+ *@param key the Key Object
+ *@return
*/
public Object remove(Object key) {
- synchronized(store) {
+ synchronized (store) {
return store.remove(key);
- }
+ }
}
-
+
/**
* Indicates if the given key is associated to a contained object.
*
- * @param key the Key Object
+ *@param key the Key Object
+ *@return
*/
public boolean containsKey(Object key) {
- synchronized(store) {
- return store.containsKey(key);
+ synchronized (store) {
+ return store.containsKey(key);
}
}
-
- /**
- * Frees some object out of the Store.
- */
- public void free() {
- synchronized(store) {
- store.free();
- }
- }
-
+
public void put(Object key, Object value) {
- synchronized(store) {
- store.put(key,value);
+ synchronized (store) {
+ store.put(key, value);
}
}
-
- /**
- * Get the object associated to the given unique key.
- *
- * @param key the Key Object
- */
- public Object get(Object key) {
- synchronized(store) {
- return store.get(key);
+
+ public int size() {
+ synchronized (store) {
+ return store.size();
}
}
-
- public boolean isEmpty() {
- synchronized(store) {
- return store.isEmpty();
- }
- }
-
- public int size() {
- synchronized(store) {
- return store.size();
- }
- }
-
- public void clear() {
- synchronized(store) {
- store.clear();
- }
- }
-
- public Store getNextStore() {
- synchronized(store) {
- return store.getNextStore();
- }
+
+ public void clear() {
+ synchronized (store) {
+ store.clear();
+ }
}
}
+
1.3 +66 -18 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/Swap.java
Index: Swap.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/Swap.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Swap.java 27 Jan 2002 17:04:17 -0000 1.2
+++ Swap.java 9 Feb 2002 18:45:11 -0000 1.3
@@ -1,31 +1,79 @@
-/*****************************************************************************
- * Copyright (C) The Apache Software Foundation. All rights reserved. *
- * ------------------------------------------------------------------------- *
- * This software is published under the terms of the Apache Software License *
- * version 1.1, a copy of which has been included with this distribution in *
- * the LICENSE file. *
- *****************************************************************************/
-
+/*
+ * The Apache Software License, Version 1.1
+ *
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * nor may "Apache" appear in their name, without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
package org.apache.commons.simplestore;
import java.io.IOException;
import java.util.Collection;
-import java.util.Map;
import java.util.Enumeration;
+import java.util.Map;
import java.util.Set;
/**
- *
- * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
+ *@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- * @version $Id: Swap.java,v 1.2 2002/01/27 17:04:17 froehlich Exp $
+ *@version $Id: Swap.java,v 1.3 2002/02/09 18:45:11 froehlich Exp $
*/
public interface Swap {
-
- /** Adds object to Queue
- * @param object not null refence
- */
- public void add(Object key,Reachable object);
-
+
+ /**
+ * Adds object to Queue
+ *
+ *@param object not null refence
+ *@param key
+ */
+ public void add(Object key, Reachable object);
+
}
1.11 +57 -16 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/Store.java
Index: Store.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/Store.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- Store.java 21 Jan 2002 22:02:36 -0000 1.10
+++ Store.java 9 Feb 2002 18:45:11 -0000 1.11
@@ -1,11 +1,57 @@
-/*****************************************************************************
- * Copyright (C) The Apache Software Foundation. All rights reserved. *
- * ------------------------------------------------------------------------- *
- * This software is published under the terms of the Apache Software License *
- * version 1.1, a copy of which has been included with this distribution in *
- * the LICENSE file. *
- *****************************************************************************/
-
+/*
+ * The Apache Software License, Version 1.1
+ *
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * nor may "Apache" appear in their name, without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
package org.apache.commons.simplestore;
/**
@@ -13,13 +59,10 @@
*
* @author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
* g-froehlich@gmx.de</a>
- * @version $Id: Store.java,v 1.10 2002/01/21 22:02:36 froehlich Exp $
+ * @version $Id: Store.java,v 1.11 2002/02/09 18:45:11 froehlich Exp $
*/
public interface Store {
- Store getNextStore();
-
- void free();
-
+
void clear();
Object get(Object key);
@@ -29,9 +72,7 @@
void put(Object key, Object value);
Object remove(Object key);
-
- //put (Map m); //?
-
+
boolean containsKey(Object key);
int size();
1.10 +184 -141 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SoftRefMemoryStore.java
Index: SoftRefMemoryStore.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SoftRefMemoryStore.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- SoftRefMemoryStore.java 27 Jan 2002 17:41:48 -0000 1.9
+++ SoftRefMemoryStore.java 9 Feb 2002 18:45:11 -0000 1.10
@@ -1,11 +1,57 @@
-/*****************************************************************************
- * Copyright (C) The Apache Software Foundation. All rights reserved. *
- * ------------------------------------------------------------------------- *
- * This software is published under the terms of the Apache Software License *
- * version 1.1, a copy of which has been included with this distribution in *
- * the LICENSE file. *
- *****************************************************************************/
-
+/*
+ * The Apache Software License, Version 1.1
+ *
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * nor may "Apache" appear in their name, without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
package org.apache.commons.simplestore;
import java.io.IOException;
@@ -13,197 +59,194 @@
import java.lang.ref.SoftReference;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Map;
import java.util.Enumeration;
+import java.util.Map;
import java.util.Set;
/**
- *
- * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
+ *@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- * @author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
+ *@author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
* g-froehlich@gmx.de</a>
- * @version $Id: SoftRefMemoryStore.java,v 1.9 2002/01/27 17:41:48 froehlich Exp $
+ *@version $Id: SoftRefMemoryStore.java,v 1.10 2002/02/09 18:45:11 froehlich Exp $
*/
public class SoftRefMemoryStore
-implements Store {
-
+ implements Store {
+
private static boolean DEBUG = true;
private int m_maxStrongRefCount;
- private Object [] m_strongRefs;
+ private Object[] m_strongRefs;
private int m_current = 0;
private Map m_map;
private ReferenceQueue m_queue = new ReferenceQueue();
private Swap m_swap;
-
- static class SoftRef extends SoftReference {
- Object key;
- private SoftRef(Object key, Object object, ReferenceQueue queue) {
- super(object, queue);
- this.key = key;
- }
- }
-
- static class StrongRef extends Object {
- Reachable object;
- Object key;
- Swap queue;
-
- private StrongRef(Object key, Reachable object, Swap queue) {
- this.queue = queue;// used in finalize
- this.object = object;// add strong reference to value
- this.key = key;
- }
-
- public Object get(){
- return object;
- }
-
- protected void finalize() throws Throwable {
- super.finalize();
- queue.add(key, object);
- }
- }
-
- private SoftRef makeValue(Object key, Object value, ReferenceQueue queue, Swap swap) {
- if(swap == null || value == null) {
- return new SoftRef(key, value, queue);
- } else {
- if (!(value instanceof Reachable)) {
- throw new java.lang.IllegalStateException("Value not Reachable in Swap ");
- }
- Reachable val = (Reachable)value;
- StrongRef strong = new StrongRef(key, val, swap);
- SoftRef ref = new SoftRef(key, strong, queue);
- val.setReference(strong);
- return ref;
- }
- }
-
- public static Store getInstance(Map map, Swap swap, int maxStrongRef) {
- return new SynchronizedStore(new SoftRefMemoryStore(map, swap, maxStrongRef));
- }
-
- /** Creates new SoftRefMemoryStore */
+
+ /**
+ * Creates new SoftRefMemoryStore
+ *
+ *@param map Description of Parameter
+ *@param swap Description of Parameter
+ *@param maxStrongRefCount Description of Parameter
+ */
protected SoftRefMemoryStore(Map map, Swap swap, int maxStrongRefCount) {
this.m_swap = swap;
this.m_map = map;
-
- if(maxStrongRefCount < 0) {
+
+ if (maxStrongRefCount < 0) {
throw new java.lang.IllegalArgumentException();
}
this.m_maxStrongRefCount = maxStrongRefCount;
-
- if(maxStrongRefCount > 0) {
+
+ if (maxStrongRefCount > 0) {
m_strongRefs = new Object[maxStrongRefCount];
}
}
-
- // remove unused keys
- private void removeSoftRef(){
- SoftRef ref = (SoftRef)m_queue.poll();
-
- while(ref != null) {
- m_map.remove(ref.key);
-
- if(DEBUG) {
- System.out.println( "Key " + ref.key + " removed from Reference queue, map size is " + m_map.size());
- }
- ref = (SoftRef)m_queue.poll();
- }
+
+ public static Store getInstance(Map map, Swap swap, int maxStrongRef) {
+ return new SynchronizedStore(new SoftRefMemoryStore(map, swap, maxStrongRef));
}
-
- private void addStrongRef(Object object) {
- if(m_strongRefs != null) {
- m_strongRefs[(m_current++) % m_maxStrongRefCount] = object;
+
+ /**
+ * Get the object associated to the given unique key.
+ *
+ *@param key the Key Object
+ *@return Description of the Returned Value
+ */
+ public Object get(Object key) {
+ removeSoftRef();
+ Object object = null;
+ SoftRef ref = (SoftRef) m_map.get(key);
+
+ if (ref != null) {
+ Object value = ref.get();
+
+ if (value != null && value instanceof StrongRef) {
+ object = ((StrongRef) value).object;
+ } else {
+ object = value;
+ }
}
+ addStrongRef(object);
+ return object;
}
-
- private void internalStoreObject(Object key, Object object) {
- SoftRef ref = makeValue(key, object, m_queue, m_swap);
- addStrongRef(ref.get());
- m_map.put(key,ref);
+
+ public boolean isEmpty() {
+ return m_map.isEmpty();
}
-
+
/**
* Remove the object associated to the given key.
*
- * @param key the Key Object
+ *@param key the Key Object
+ *@return Description of the Returned Value
*/
public Object remove(Object key) {
removeSoftRef();
return m_map.remove(key);
}
-
+
/**
* Indicates if the given key is associated to a contained object.
*
- * @param key the Key Object
+ *@param key the Key Object
+ *@return Description of the Returned Value
*/
public boolean containsKey(Object key) {
removeSoftRef();
return m_map.containsKey(key);
}
-
- /**
- * Frees some object out of the Store.
- */
- public void free() {
- if( m_strongRefs != null ) {
- Arrays.fill(m_strongRefs,null);
- }
- removeSoftRef();
- }
-
+
/**
* Store the given object in a persistent state. It is up to the
* caller to ensure that the key has a persistent state across
* different JVM executions.
*
- * @param key the Key Object
- * @param value the Value Object
+ *@param key the Key Object
+ *@param object Description of Parameter
+ *@since
*/
public void put(Object key, Object object) {
removeSoftRef();
- internalStoreObject(key,object);
+ internalStoreObject(key, object);
}
-
- /**
- * Get the object associated to the given unique key.
- *
- * @param key the Key Object
- */
- public Object get(Object key) {
- removeSoftRef();
- Object object = null;
- SoftRef ref = (SoftRef)m_map.get(key);
-
- if(ref != null) {
- Object value = ref.get();
- if(value != null && value instanceof StrongRef) {
- object = ((StrongRef)value).object;
- } else {
- object = value;
+ public int size() {
+ return m_map.size();
+ }
+
+ public void clear() {
+ m_map.clear();
+ }
+
+ private SoftRef makeValue(Object key, Object value, ReferenceQueue queue, Swap swap) {
+ if (swap == null || value == null) {
+ return new SoftRef(key, value, queue);
+ } else {
+ if (!(value instanceof Reachable)) {
+ throw new java.lang.IllegalStateException("Value not Reachable in Swap ");
}
+ Reachable val = (Reachable) value;
+ StrongRef strong = new StrongRef(key, val, swap);
+ SoftRef ref = new SoftRef(key, strong, queue);
+ val.setReference(strong);
+ return ref;
}
- addStrongRef(object);
- return object;
}
-
- public boolean isEmpty() {
- return m_map.isEmpty();
+
+ // remove unused keys
+ private void removeSoftRef() {
+ SoftRef ref = (SoftRef) m_queue.poll();
+
+ while (ref != null) {
+ m_map.remove(ref.key);
+
+ if (DEBUG) {
+ System.out.println("Key " + ref.key + " removed from Reference queue, map size is " + m_map.size());
+ }
+ ref = (SoftRef) m_queue.poll();
+ }
}
-
- public int size() {
- return m_map.size();
+
+ private void addStrongRef(Object object) {
+ if (m_strongRefs != null) {
+ m_strongRefs[(m_current++) % m_maxStrongRefCount] = object;
+ }
}
-
- public void clear() {
- m_map.clear();
+
+ private void internalStoreObject(Object key, Object object) {
+ SoftRef ref = makeValue(key, object, m_queue, m_swap);
+ addStrongRef(ref.get());
+ m_map.put(key, ref);
+ }
+
+ static class SoftRef extends SoftReference {
+ Object key;
+
+ private SoftRef(Object key, Object object, ReferenceQueue queue) {
+ super(object, queue);
+ this.key = key;
+ }
}
-
- public Store getNextStore() {
- throw new UnsupportedOperationException("method not implemented yet");
+
+ static class StrongRef extends Object {
+ Reachable object;
+ Object key;
+ Swap queue;
+
+ private StrongRef(Object key, Reachable object, Swap queue) {
+ this.queue = queue;// used in finalize
+ this.object = object;// add strong reference to value
+ this.key = key;
+ }
+
+ public Object get() {
+ return object;
+ }
+
+ protected void finalize() throws Throwable {
+ super.finalize();
+ queue.add(key, object);
+ }
}
}
+
1.2 +62 -16 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/Reachable.java
Index: Reachable.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/Reachable.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Reachable.java 27 Jan 2002 17:04:17 -0000 1.1
+++ Reachable.java 9 Feb 2002 18:45:11 -0000 1.2
@@ -1,21 +1,67 @@
-/*****************************************************************************
- * Copyright (C) The Apache Software Foundation. All rights reserved. *
- * ------------------------------------------------------------------------- *
- * This software is published under the terms of the Apache Software License *
- * version 1.1, a copy of which has been included with this distribution in *
- * the LICENSE file. *
- *****************************************************************************/
-
+/*
+ * The Apache Software License, Version 1.1
+ *
+ *
+ * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * nor may "Apache" appear in their name, without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
package org.apache.commons.simplestore;
/**
- *
- * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
+ *@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- * @version $Id: Reachable.java,v 1.1 2002/01/27 17:04:17 froehlich Exp $
+ *@version $Id: Reachable.java,v 1.2 2002/02/09 18:45:11 froehlich Exp $
*/
public interface Reachable {
- /** Must save strong refense on parameter,
+ /**
+ * Must save strong refense on parameter,
* This interface is used for values then Store is used with
* Swap.
* Sample impl :
@@ -33,9 +79,9 @@
* }
* }
*
- * @param object value object must save strong refernce on parameter.
- */
- public void setReference(Object object);
-
+ *@param object value object must save strong refernce on parameter.
+ */
+ public void setReference(Object object);
+
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>