File Source: LRUCache2.java

         /* 
    P/P   *  Method: org.apache.roller.weblogger.util.LRUCache2__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  package org.apache.roller.weblogger.util;
    19  import java.util.ArrayList;
    20  import java.util.Collections;
    21  import java.util.Iterator;
    22  import java.util.LinkedHashMap;
    23  import java.util.List;
    24  import java.util.Map;
    25  /**
    26   * LRU cache with per-entry timeout logic.
    27   * 
    28   * @author Dave Johnson
    29   */
    30  public class LRUCache2
    31  {
    32      private long timeout;
    33      private Map cache = null;
    34      private Environment environment = null;
    35  
    36      /**
    37       * Create cache.
    38       * 
    39       * @param maxsize
    40       *            Maximum number of entries in cache.
    41       * @param timeout
    42       *            Entry timeout in milli-seconds.
    43       */
    44      public LRUCache2(int maxsize, long timeout)
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.util.LRUCache2(int, long)
              * 
              *  Preconditions:
              *    maxsize in -1_610_612_737..3_221_225_471
              * 
              *  Postconditions:
              *    this.cache == &new LRUCache2$LRULinkedHashMap(LRUCache2#2)
              *    this.environment == &new LRUCache2$DefaultEnvironment(LRUCache2#1)
              *    this.timeout == timeout
              *    init'ed(this.timeout)
              *    new LRUCache2$DefaultEnvironment(LRUCache2#1) num objects == 1
              *    new LRUCache2$LRULinkedHashMap(LRUCache2#2) num objects == 1
              *    this.cache.maxsize == maxsize
              *    this.cache.maxsize in -1_610_612_737..3_221_225_471
              */
    45      {
    46          this.environment = new DefaultEnvironment();
    47          this.timeout = timeout;
    48          this.cache = new LRULinkedHashMap(maxsize);
    49      }
    50  
    51      /**
    52       * Create cache that uses custom environment.
    53       * 
    54       * @param maxsize
    55       *            Maximum number of entries in cache.
    56       * @param timeout
    57       *            Entry timeout in milli-seconds.
    58       */
    59      public LRUCache2(Environment environment, int maxsize, long timeout)
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.util.LRUCache2(LRUCache2$Environment, int, long)
              * 
              *  Preconditions:
              *    maxsize in -1_610_612_737..3_221_225_471
              * 
              *  Postconditions:
              *    this.cache == &new LRUCache2$LRULinkedHashMap(LRUCache2#1)
              *    this.environment == environment
              *    init'ed(this.environment)
              *    this.timeout == timeout
              *    init'ed(this.timeout)
              *    new LRUCache2$LRULinkedHashMap(LRUCache2#1) num objects == 1
              *    this.cache.maxsize == maxsize
              *    this.cache.maxsize in -1_610_612_737..3_221_225_471
              */
    60      {
    61          this.environment = environment;
    62          this.timeout = timeout;
    63          this.cache = new LRULinkedHashMap(maxsize);
    64      }
    65  
    66      public synchronized void put(Object key, Object value)
    67      {
                 /* 
    P/P           *  Method: void put(Object, Object)
                  * 
                  *  Preconditions:
                  *    this.cache != null
                  *    this.environment != null
                  */
    68          CacheEntry entry = new CacheEntry(value, environment
    69                          .getCurrentTimeInMillis());
    70          cache.put(key, entry);
    71      }
    72  
    73      public Object get(Object key)
    74      {
                 /* 
    P/P           *  Method: Object get(Object)
                  * 
                  *  Preconditions:
                  *    this.cache != null
                  *    (soft) this.environment != null
                  *    (soft) init'ed(this.timeout)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.util.Map:get(...)@79: Addr_Set{null}, Inverse{null}
                  */
    75          Object value = null;
    76          CacheEntry entry = null;
    77          synchronized(this)
    78          {
    79              entry = (CacheEntry) cache.get(key);
    80          }
    81          if (entry != null)
    82          {
    83              if (environment.getCurrentTimeInMillis() - entry.getTimeCached() < timeout)
    84              {
    85                  value = entry.getValue();
    86              }
    87              else
    88              {
    89                  cache.remove(entry);
    90              }
    91          }
    92          return value;
    93      }
    94  
    95      public synchronized void purge()
    96      {
                 /* 
    P/P           *  Method: void purge()
                  * 
                  *  Preconditions:
                  *    this.cache != null
                  */
    97          cache.clear();
    98      }
    99  
   100      public synchronized void purge(String[] patterns)
   101      {
                 /* 
    P/P           *  Method: void purge(String[])
                  * 
                  *  Preconditions:
                  *    this.cache != null
                  *    (soft) patterns != null
                  *    (soft) patterns.length <= 232-1
                  *    (soft) init'ed(patterns[...])
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@106 != null
                  *    java.util.Map:keySet(...)@103 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:indexOf(...)@109: {-1}, {-231..-2, 0..232-1}
                  *    java.util.Iterator:hasNext(...)@104: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@117: {0}, {1}
                  */
   102          List purgeList = new ArrayList();
   103          Iterator keys = cache.keySet().iterator();
   104          while (keys.hasNext())
   105          {
   106              String key = (String) keys.next();
   107              for (int i = 0; i < patterns.length; i++)
   108              {
   109                  if (key.indexOf(patterns[i]) != -1)
   110                  {
   111                      purgeList.add(key);
   112                      break;
   113                  }
   114              }
   115          }
   116          Iterator purgeIter = purgeList.iterator();
   117          while (purgeIter.hasNext())
   118          {
   119              String key = (String) purgeIter.next();
   120              cache.remove(key);
   121          }
   122      }
   123  
   124      public int size()
   125      {
                 /* 
    P/P           *  Method: int size()
                  * 
                  *  Preconditions:
                  *    this.cache != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   126          return cache.size();
   127      }
   128      public interface Environment
   129      {
   130          public long getCurrentTimeInMillis();
   131      }
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.util.LRUCache2$DefaultEnvironment()
              */
   132      public static class DefaultEnvironment implements Environment
   133      {
   134          public long getCurrentTimeInMillis()
   135          {
                     /* 
    P/P               *  Method: long getCurrentTimeInMillis()
                      * 
                      *  Postconditions:
                      *    init'ed(return_value)
                      */
   136              return System.currentTimeMillis();
   137          }
   138      }
   139      private static class CacheEntry
   140      {
   141          private Object value;
   142          private long timeCached = -1;
   143  
   144          public CacheEntry(Object value, long timeCached)
                 /* 
    P/P           *  Method: void org.apache.roller.weblogger.util.LRUCache2$CacheEntry(Object, long)
                  * 
                  *  Postconditions:
                  *    this.timeCached == timeCached
                  *    init'ed(this.timeCached)
                  *    this.value == value
                  *    init'ed(this.value)
                  */
   145          {
   146              this.timeCached = timeCached;
   147              this.value = value;
   148          }
   149  
   150          public long getTimeCached()
   151          {
                     /* 
    P/P               *  Method: long getTimeCached()
                      * 
                      *  Preconditions:
                      *    init'ed(this.timeCached)
                      * 
                      *  Postconditions:
                      *    return_value == this.timeCached
                      *    init'ed(return_value)
                      */
   152              return timeCached;
   153          }
   154  
   155          public Object getValue()
   156          {
                     /* 
    P/P               *  Method: Object getValue()
                      * 
                      *  Preconditions:
                      *    init'ed(this.value)
                      * 
                      *  Postconditions:
                      *    return_value == this.value
                      *    init'ed(return_value)
                      */
   157              return value;
   158          }
   159      }
   160      
   161      // David Flanaghan: http://www.davidflanagan.com/blog/000014.html
   162      private static class LRULinkedHashMap extends LinkedHashMap
   163      {
   164          protected int maxsize;
   165  
   166          public LRULinkedHashMap(int maxsize)
   167          {
                     /* 
    P/P               *  Method: void org.apache.roller.weblogger.util.LRUCache2$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
                      */
   168              super(maxsize * 4 / 3 + 1, 0.75f, true);
   169              this.maxsize = maxsize;
   170          }
   171  
   172          protected boolean removeEldestEntry(Map.Entry eldest)
   173          {
                     /* 
    P/P               *  Method: bool removeEldestEntry(Map$Entry)
                      * 
                      *  Preconditions:
                      *    init'ed(this.maxsize)
                      * 
                      *  Postconditions:
                      *    init'ed(return_value)
                      */
   174              return this.size() > this.maxsize;
   175          }
   176      }
   177  }








SofCheck Inspector Build Version : 2.18479
LRUCache2.java 2009-Jan-02 14:25:30
LRUCache2.class 2009-Sep-04 03:12:32
LRUCache2$CacheEntry.class 2009-Sep-04 03:12:32
LRUCache2$DefaultEnvironment.class 2009-Sep-04 03:12:32
LRUCache2$Environment.class 2009-Sep-04 03:12:32
LRUCache2$LRULinkedHashMap.class 2009-Sep-04 03:12:32