File Source: LRUCacheImpl.java

         /* 
    P/P   *  Method: org.apache.roller.weblogger.util.cache.LRUCacheImpl$LRULinkedHashMap__static_init
          */
     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.util.cache;
    20  
    21  import java.util.Collections;
    22  import java.util.Date;
    23  import java.util.HashMap;
    24  import java.util.LinkedHashMap;
    25  import java.util.Map;
    26  import org.apache.commons.logging.Log;
    27  import org.apache.commons.logging.LogFactory;
    28  
    29  
    30  /**
    31   * A simple LRU Cache.
    32   */
    33  public class LRUCacheImpl implements Cache {
    34      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.util.cache.LRUCacheImpl__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    35      private static Log log = LogFactory.getLog(LRUCacheImpl.class);
    36      
    37      private String id = null;
    38      private Map cache = null;
    39      
    40      // for metrics
    41      protected double hits = 0;
    42      protected double misses = 0;
    43      protected double puts = 0;
    44      protected double removes = 0;
    45      protected Date startTime = new Date();
    46      
    47      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.util.cache.LRUCacheImpl(String)
              * 
              *  Postconditions:
              *    init'ed(this.cache)
              *    this.hits == +0
              *    this.misses == +0
              *    this.puts == +0
              *    this.removes == +0
              *    this.id == id
              *    init'ed(this.id)
              *    this.startTime == &new Date(LRUCacheImpl#1)
              *    new Date(LRUCacheImpl#1) num objects == 1
              */
    48      protected LRUCacheImpl(String id) {
    49          
    50          this.id = id;
    51          this.cache = Collections.synchronizedMap(new LRULinkedHashMap(100));
    52      }
    53      
    54      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.util.cache.LRUCacheImpl(String, int)
              * 
              *  Preconditions:
              *    maxsize in -1_610_612_737..3_221_225_471
              * 
              *  Postconditions:
              *    init'ed(this.cache)
              *    this.hits == +0
              *    this.misses == +0
              *    this.puts == +0
              *    this.removes == +0
              *    this.id == id
              *    init'ed(this.id)
              *    this.startTime == &new Date(LRUCacheImpl#1)
              *    new Date(LRUCacheImpl#1) num objects == 1
              */
    55      protected LRUCacheImpl(String id, int maxsize) {
    56          
    57          this.id = id;
    58          this.cache = Collections.synchronizedMap(new LRULinkedHashMap(maxsize));
    59      }
    60      
    61      
    62      public String getId() {
                 /* 
    P/P           *  Method: String getId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.id)
                  * 
                  *  Postconditions:
                  *    return_value == this.id
                  *    init'ed(return_value)
                  */
    63          return this.id;
    64      }
    65      
    66      
    67      /**
    68       * Store an entry in the cache.
    69       */
    70      public synchronized void put(String key, Object value) {
    71          
                 /* 
    P/P           *  Method: void put(String, Object)
                  * 
                  *  Preconditions:
                  *    init'ed(this.puts)
                  *    this.cache != null
                  * 
                  *  Postconditions:
                  *    this.puts == old this.puts + 1
                  *    init'ed(this.puts)
                  */
    72          this.cache.put(key, value);
    73          puts++;
    74      }
    75      
    76      
    77      /**
    78       * Retrieve an entry from the cache.
    79       */
    80      public synchronized Object get(String key) {
    81          
                 /* 
    P/P           *  Method: Object get(String)
                  * 
                  *  Preconditions:
                  *    this.cache != null
                  *    (soft) init'ed(this.hits)
                  *    (soft) init'ed(this.misses)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  *    this.hits == One-of{old this.hits, old this.hits + 1}
                  *    (soft) init'ed(this.hits)
                  *    this.misses == One-of{old this.misses + 1, old this.misses}
                  *    (soft) init'ed(this.misses)
                  * 
                  *  Test Vectors:
                  *    java.util.Map:get(...)@82: Inverse{null}, Addr_Set{null}
                  */
    82          Object obj = this.cache.get(key);
    83          
    84          // for metrics
    85          if(obj == null) {
    86              misses++;
    87          } else {
    88              hits++;
    89          }
    90          
    91          return obj;
    92      }
    93      
    94      
    95      public synchronized void remove(String key) {
    96          
                 /* 
    P/P           *  Method: void remove(String)
                  * 
                  *  Preconditions:
                  *    init'ed(this.removes)
                  *    this.cache != null
                  * 
                  *  Postconditions:
                  *    this.removes == old this.removes + 1
                  *    init'ed(this.removes)
                  */
    97          this.cache.remove(key);
    98          removes++;
    99      }
   100      
   101      
   102      public synchronized void clear() {
   103          
                 /* 
    P/P           *  Method: void clear()
                  * 
                  *  Preconditions:
                  *    this.cache != null
                  * 
                  *  Postconditions:
                  *    this.hits == +0
                  *    this.misses == +0
                  *    this.puts == +0
                  *    this.removes == +0
                  *    this.startTime == &new Date(clear#1)
                  *    new Date(clear#1) num objects == 1
                  */
   104          this.cache.clear();
   105          
   106          // clear metrics
   107          hits = 0;
   108          misses = 0;
   109          puts = 0;
   110          removes = 0;
   111          startTime = new Date();
   112      }
   113      
   114      
   115      public Map getStats() {
   116          
                 /* 
    P/P           *  Method: Map getStats()
                  * 
                  *  Preconditions:
                  *    init'ed(this.hits)
                  *    init'ed(this.misses)
                  *    init'ed(this.puts)
                  *    init'ed(this.removes)
                  *    init'ed(this.startTime)
                  *    (soft) this.hits + this.misses != +0
                  * 
                  *  Postconditions:
                  *    return_value == &new HashMap(getStats#1)
                  *    new HashMap(getStats#1) num objects == 1
                  */
   117          Map stats = new HashMap();
   118          stats.put("startTime", this.startTime);
   119          stats.put("hits", new Double(this.hits));
   120          stats.put("misses", new Double(this.misses));
   121          stats.put("puts", new Double(this.puts));
   122          stats.put("removes", new Double(this.removes));
   123          
   124          // calculate efficiency
   125          if((misses - removes) > 0) {
   126              double efficiency = hits / (misses + hits);
   127              stats.put("efficiency", new Double(efficiency * 100));
   128          }
   129          
   130          return stats;
   131      }
   132      
   133      
   134      // David Flanaghan: http://www.davidflanagan.com/blog/000014.html
   135      private static class LRULinkedHashMap extends LinkedHashMap {
   136          protected int maxsize;
   137          
   138          public LRULinkedHashMap(int maxsize) {
                     /* 
    P/P               *  Method: void org.apache.roller.weblogger.util.cache.LRUCacheImpl$LRULinkedHashMap(int)
                      * 
                      *  Preconditions:
                      *    maxsize in -1_610_612_737..3_221_225_471
                      * 
                      *  Postconditions:
                      *    this.maxsize == maxsize
                      *    this.maxsize in -1_610_612_737..3_221_225_471
                      */
   139              super(maxsize * 4 / 3 + 1, 0.75f, true);
   140              this.maxsize = maxsize;
   141          }
   142          
   143          protected boolean removeEldestEntry(Map.Entry eldest) {
                     /* 
    P/P               *  Method: bool removeEldestEntry(Map$Entry)
                      * 
                      *  Preconditions:
                      *    init'ed(this.maxsize)
                      * 
                      *  Postconditions:
                      *    init'ed(return_value)
                      */
   144              return this.size() > this.maxsize;
   145          }
   146      }
   147      
   148  }








SofCheck Inspector Build Version : 2.18479
LRUCacheImpl.java 2009-Jan-02 14:24:50
LRUCacheImpl.class 2009-Sep-04 03:12:32
LRUCacheImpl$LRULinkedHashMap.class 2009-Sep-04 03:12:32