View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.datastructure;
22  
23  import java.util.*;
24  /***
25   * Most recently used map (implementation based on mrulist)
26   * 
27   * @author TiongHiang Lee (thlee@onemindsoft.org)
28   * @version $Id: MruMap.java,v 1.3 2004/10/31 15:57:55 thlee Exp $ $Name:  $
29   */
30  public class MruMap extends HashMap implements Map
31  {
32  
33      /***
34       * For MruMap implementation
35       * @author TiongHiang Lee (thlee@onemindsoft.org)
36       * @version $Id: MruMap.java,v 1.3 2004/10/31 15:57:55 thlee Exp $ $Name:  $
37       */
38      private class InnerMruList extends MruList
39      {
40          /***
41           * Constructor
42           * @param size the size
43           * @param timeout the timeout
44           */
45          public InnerMruList(long size, long timeout)
46          {
47              super(size, timeout);
48          }
49          
50          /***
51           * {@inheritDoc}
52           */
53          protected void truncateEntry(Object obj)
54          {
55              super.truncateEntry(obj);
56              MruMap.this.remove(obj);
57          }
58  
59          /***
60           * {@inheritDoc}
61           */
62          protected void expireEntry(Object obj)
63          {
64              super.expireEntry(obj);
65              MruMap.this.remove(obj);
66          }
67      }
68  
69      /*** the list * */
70      private MruList _mruList;
71  
72      /***
73       * {@inheritDoc}
74       */
75      public MruMap()
76      {
77          this(0, 0);
78      }
79  
80      /***
81       * Constructor
82       * @param size the limit of the map (0 for never timeout)
83       * @param timeout the timeout (0 for never expire)
84       */
85      public MruMap(long size, long timeout)
86      {
87          _mruList = new InnerMruList(size, timeout);
88      }
89  
90      /***
91       * {@inheritDoc}
92       */
93      public void clear()
94      {
95          super.clear();
96          _mruList.clear();
97      }
98  
99      /***
100      * {@inheritDoc}
101      */
102     public Object get(Object key)
103     {
104         _mruList.access(key);
105         return super.get(key);
106     }
107 
108     /***
109      * {@inheritDoc}
110      */
111     public Object put(Object key, Object value)
112     {
113         Object result = super.put(key, value);
114         _mruList.access(key); //this must be done second
115         return result;
116     }
117 
118     /***
119      * {@inheritDoc}
120      */
121     public void putAll(Map t)
122     {
123         super.putAll(t);//this must be done second
124         _mruList.addAll(t.keySet());
125         
126     }
127 
128     /***
129      * {@inheritDoc}
130      */
131     public Object remove(Object key)
132     {
133         _mruList.remove(key); 
134         return super.remove(key);
135     }
136 
137     /***
138      * {@inheritDoc}
139      */
140     public void expire(MruList list, Object obj)
141     {
142         super.remove(obj);
143     }
144 
145     /***
146      * {@inheritDoc}
147      */
148     public void truncate(MruList list, Object obj)
149     {
150         super.remove(obj);
151     }
152 }