View Javadoc

1   /**********************************************************************
2    * LRUMap.java
3    * created on 11.07.2006 by netseeker
4    * $Source$
5    * $Date$
6    * $Revision$
7    *
8    * ====================================================================
9    *
10   *  Copyright 2006 netseeker aka Michael Manske
11   *
12   *  Licensed under the Apache License, Version 2.0 (the "License");
13   *  you may not use this file except in compliance with the License.
14   *  You may obtain a copy of the License at
15   *
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the License for the specific language governing permissions and
22   *  limitations under the License.
23   * ====================================================================
24   *
25   * This file is part of the de.netseeker.ejoe.cache framework.
26   * For more information on the author, please see
27   * <http://www.manskes.de/>.
28   *
29   *********************************************************************/
30  
31  package de.netseeker.ejoe.cache;
32  
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.LinkedHashMap;
36  import java.util.Map;
37  import java.util.Set;
38  
39  /***
40   * Simple synchronized implementation of a least recently used cache (LRU). This class implements the Map interface
41   * hence it can be used easily as replacement for Collections.synchronizedMap(new HashMap()).
42   * 
43   * @author netseeker
44   * @since 0.3.9.1
45   */
46  public class LRUMap implements Map
47  {
48      private static final long serialVersionUID = 1L;
49  
50      public static final int   MAX_ENTRIES      = 1000;
51  
52      private int               _maxEntries;
53  
54      private Map               _lruMap;
55  
56      /***
57       * Creates a new LRUMap
58       */
59      public LRUMap()
60      {
61          this( MAX_ENTRIES );
62      }
63  
64      /***
65       * Creates a new LRUMap
66       * 
67       * @param maxEntries max size of the cache
68       */
69      public LRUMap(int maxEntries)
70      {
71          _maxEntries = maxEntries;
72          _lruMap = Collections.synchronizedMap( new LinkedHashMap( maxEntries + 1, 75F, true )
73              {
74                  private static final long serialVersionUID = 1L;
75  
76                  protected boolean removeEldestEntry( Entry entry )
77                  {
78                      return size() > _maxEntries;
79                  }
80              } );
81      }
82  
83      public int size()
84      {
85          return _lruMap.size();
86      }
87  
88      public boolean isEmpty()
89      {
90          return _lruMap.isEmpty();
91      }
92  
93      public boolean containsKey( Object key )
94      {
95          return _lruMap.containsKey( key );
96      }
97  
98      public boolean containsValue( Object value )
99      {
100         return _lruMap.containsValue( value );
101     }
102 
103     public Object get( Object obj )
104     {
105         return _lruMap.get( obj );
106     }
107 
108     public Object put( Object key, Object value )
109     {
110         return _lruMap.put( key, value );
111     }
112 
113     public Object remove( Object key )
114     {
115         return _lruMap.remove( key );
116     }
117 
118     public void putAll( Map map )
119     {
120         _lruMap.putAll( map );
121     }
122 
123     public void clear()
124     {
125         _lruMap.clear();
126     }
127 
128     public Set keySet()
129     {
130         return _lruMap.keySet();
131     }
132 
133     public Collection values()
134     {
135         return _lruMap.values();
136     }
137 
138     public Set entrySet()
139     {
140         return _lruMap.entrySet();
141     }
142 }