froehlich    02/01/27 09:41:49

  Modified:    simplestore/src/java/org/apache/commons/simplestore
                        SoftRefMemoryStore.java
  Added:       simplestore/src/java/org/apache/commons/simplestore
                        MRUMap.java
  Removed:     simplestore/src/java/org/apache/commons/simplestore
                        MRUMemoryStore.java
  Log:
  added into SoftRefMemoryStore new contructor which can
  handle now all Map based whateverhashmaps.
  Therefor MRUMemoryStore was renamed into MRUMap and
  extended from HashMap!
  
  Revision  Changes    Path
  1.9       +58 -54    
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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- SoftRefMemoryStore.java   27 Jan 2002 17:04:17 -0000      1.8
  +++ SoftRefMemoryStore.java   27 Jan 2002 17:41:48 -0000      1.9
  @@ -9,6 +9,9 @@
   package org.apache.commons.simplestore;
   
   import java.io.IOException;
  +import java.lang.ref.ReferenceQueue;
  +import java.lang.ref.SoftReference;
  +import java.util.Arrays;
   import java.util.Collection;
   import java.util.Map;
   import java.util.Enumeration;
  @@ -20,32 +23,33 @@
    *      [EMAIL PROTECTED]</a>
    * @author Gerhard Froehlich <a href="mailto:[EMAIL PROTECTED]";>
    *      [EMAIL PROTECTED]</a>
  - * @version $Id: SoftRefMemoryStore.java,v 1.8 2002/01/27 17:04:17 froehlich Exp $
  + * @version $Id: SoftRefMemoryStore.java,v 1.9 2002/01/27 17:41:48 froehlich Exp $
    */
   public class SoftRefMemoryStore
   implements Store {
       
       private static boolean DEBUG = true;
  -    private int maxStrongRefCount;
  -    private Object [] strongRefs;
  -    private int current = 0;
  -    private java.util.Map map = new java.util.HashMap();
  -    private java.lang.ref.ReferenceQueue queue = new java.lang.ref.ReferenceQueue();
  -    private Swap swap;
  +    private int m_maxStrongRefCount;
  +    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 java.lang.ref.SoftReference{
  +    static class SoftRef extends SoftReference {
           Object key;
  -        private  SoftRef(Object key,Object object,java.lang.ref.ReferenceQueue 
queue) {
  -            super(object,queue);
  +        private SoftRef(Object key, Object object, ReferenceQueue queue) {
  +            super(object, queue);
               this.key = key;
           }
       }
       
  -    static class StrongRef extends java.lang.Object{
  +    static class StrongRef extends Object {
           Reachable object;
           Object key;
           Swap queue;
  -        private  StrongRef(Object key,Reachable object, 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;
  @@ -55,68 +59,70 @@
               return object;
           }
           
  -        protected void finalize() throws java.lang.Throwable {
  +        protected void finalize() throws Throwable {
               super.finalize();
  -            queue.add(key,object);
  +            queue.add(key, object);
           }
       }
       
  -    private SoftRef makeValue(Object key, Object value,java.lang.ref.ReferenceQueue 
queue, Swap swap  ){
  -        if( swap == null ||  value == null  ){
  -            return new SoftRef( key, value, queue);
  -        }
  -        else{
  -            if ( !( value instanceof Reachable ) )
  +    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 );
  +            StrongRef strong = new StrongRef(key, val, swap);
  +            SoftRef   ref    = new SoftRef(key, strong, queue);
  +            val.setReference(strong);
               return ref;
           }
       }
       
  -    public static Store getInstance(Swap swap, int maxStrongRef) {
  -        
  -        return new SynchronizedStore( new  SoftRefMemoryStore( swap, maxStrongRef ) 
) ;
  -        
  +    public static Store getInstance(Map map, Swap swap, int maxStrongRef) {
  +        return new SynchronizedStore(new SoftRefMemoryStore(map, swap, 
maxStrongRef));
       }
       
       /** Creates new SoftRefMemoryStore */
  -    protected SoftRefMemoryStore(Swap swap, int maxStrongRefCount) {
  -        this.swap = swap;
  +    protected SoftRefMemoryStore(Map map, Swap swap, int maxStrongRefCount) {
  +        this.m_swap = swap;
  +        this.m_map = map;
  +        
           if(maxStrongRefCount < 0) {
               throw new java.lang.IllegalArgumentException();
           }
  -        this.maxStrongRefCount = maxStrongRefCount;
  +        this.m_maxStrongRefCount = maxStrongRefCount;
  +        
           if(maxStrongRefCount > 0) {
  -            strongRefs = new Object[ maxStrongRefCount ];
  +            m_strongRefs = new Object[maxStrongRefCount];
           }
       }
       
       // remove unused keys
       private void removeSoftRef(){
  -        SoftRef ref = (SoftRef)queue.poll();
  +        SoftRef ref = (SoftRef)m_queue.poll();
           
  -        while( ref != null ) {
  -            map.remove(ref.key);
  +        while(ref != null) {
  +            m_map.remove(ref.key);
  +            
               if(DEBUG) {
  -                System.out.println( "Key " + ref.key + " removed from Reference 
queue, map size is " + map.size());
  +                System.out.println( "Key " + ref.key + " removed from Reference 
queue, map size is " + m_map.size());
               }
  -            ref = (SoftRef)queue.poll();
  +            ref = (SoftRef)m_queue.poll();
           }
       }
       
       private void addStrongRef(Object object) {
  -        if( strongRefs != null ) {
  -            strongRefs[ ( current++ ) % maxStrongRefCount ] = object;
  +        if(m_strongRefs != null) {
  +            m_strongRefs[(m_current++) % m_maxStrongRefCount] = object;
           }
       }
       
       private void internalStoreObject(Object key, Object object) {
  -        SoftRef ref = makeValue(key,object,queue,swap);
  +        SoftRef ref = makeValue(key, object, m_queue, m_swap);
           addStrongRef(ref.get());
  -        map.put(key,ref);
  +        m_map.put(key,ref);
       }
       
       /**
  @@ -126,7 +132,7 @@
        */
       public Object remove(Object key) {
           removeSoftRef();
  -        return map.remove(key);
  +        return m_map.remove(key);
       }
       
       /**
  @@ -136,15 +142,16 @@
        */
       public boolean containsKey(Object key) {
           removeSoftRef();
  -        return map.containsKey(key);
  +        return m_map.containsKey(key);
       }
       
       /**
        * Frees some object out of the Store.
        */
       public void free() {
  -        if( strongRefs != null )
  -            java.util.Arrays.fill(strongRefs,null);
  +        if( m_strongRefs != null ) {
  +            Arrays.fill(m_strongRefs,null);
  +        }
           removeSoftRef();
       }
       
  @@ -169,34 +176,31 @@
       public Object get(Object key) {
           removeSoftRef();
           Object object = null;
  -        SoftRef ref = (SoftRef)map.get(key);
  +        SoftRef ref = (SoftRef)m_map.get(key);
           
           if(ref != null) {
  -            
               Object value = ref.get();
  -            
  -            if( value != null && value instanceof StrongRef ){
  +
  +            if(value != null && value instanceof StrongRef) {
                   object = ((StrongRef)value).object;
  -            }else{
  +            } else {
                   object = value;
               }
           }
  -        
           addStrongRef(object);
  -        
           return object;
       }
       
       public boolean isEmpty()  {
  -        return map.isEmpty();
  +        return m_map.isEmpty();
       }
       
       public int size()  {
  -        return map.size();
  +        return m_map.size();
       }
       
       public void clear()  {
  -        map.clear();
  +        m_map.clear();
       }
       
       public Store getNextStore() {
  
  
  
  1.1                  
jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/MRUMap.java
  
  Index: MRUMap.java
  ===================================================================
  /*****************************************************************************
   * 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.                                                         *
   *****************************************************************************/
  
  package org.apache.commons.simplestore;
  
  import java.util.Set;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.LinkedList;
  import java.util.Map;
  
  /**
   * This class provides a MRU cache algorithm. It combines a HashMap 
   * and a LinkedList to create a so called MRU (Most Recently Used) cache.
   *
   * @author Gerhard Froehlich <a href="mailto:[EMAIL PROTECTED]";>
   *      [EMAIL PROTECTED]</a>
   * @version $Id: MRUMap.java,v 1.1 2002/01/27 17:41:48 froehlich Exp $
   */
  public final class MRUMap
  extends HashMap {
  
      private int mMaxObjects = 100;
      private LinkedList mMRUList;
  
      /**
       * This method returns the current set of the object limit.
       *
       * @return value of the object limit.
       */
      public int getMaxObjects() {
          return this.mMaxObjects;
      }
  
      /**
       * This method initializes the MRUMemoryStore. NOTE: You should
       * first call the setMaxObjects(int maxobjects) method to set the
       * the limit of the Store. Default the limit is 100 Objects. 
       */
      public MRUMap(int maxobjects) {
          super((int) (maxobjects * 1.2));
          this.mMRUList = new LinkedList();
          this.mMaxObjects = maxobjects;
      }
  
      /**
       *  Returns the value to which this map maps the specified key.
       *
       * @param key whose associated value is to be returned
       * @return the value to which this map maps the specified key, 
       * or null if the map contains no mapping for this key
       */
      public Object get(Object key) {
          Object tmpobject = super.get(key);
          if (tmpobject != null) {
              this.mMRUList.remove(key);
              this.mMRUList.addFirst(key);
              return tmpobject;
          }
          return null;
      }
   
      /**
       *  Associates the specified value with the specified key in this map 
       * (optional operation). If the map previously contained a mapping for 
       * this key, the old value is replaced.
       *
       * @param key with which the specified value is to be associated.
       * @param value to be associated with the specified key.
       */
      public Object put(Object key, Object value) {
          while (this.mMRUList.size() >= this.mMaxObjects) {
              this.free();
          }
          Object obj = super.put(key, value);
          this.mMRUList.remove(key);
          this.mMRUList.addFirst(key);
          return obj;
      }
  
      /**
       *  Remove the object associated to the given key.
       *
       * @param key the Key object
       */
      public Object remove(Object key) {
          this.mMRUList.remove(key);
          return super.remove(key); 
      }
  
      
      /**
       *  Frees some of the fast memory used by this store. It removes the last
       *  element in the store.
       */
      private void free() {
          try {
              if (super.size() > 0) {
                  super.remove(this.mMRUList.getLast());
                  this.mMRUList.removeLast();
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to