Codebase list libonemind-commons-java-java / upstream/latest src / java / org / onemind / commons / java / datastructure / MruList.java
upstream/latest

Tree @upstream/latest (Download .tar.gz)

MruList.java @upstream/latestraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright (C) 2004 TiongHiang Lee
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not,  write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Email: thlee@onemindsoft.org
 */

package org.onemind.commons.java.datastructure;

import java.util.*;
/**
 * Most recently used list implementation. It support entries expiration
 * by access time.
 * @author TiongHiang Lee (thlee@onemindsoft.org)
 * @version $Id: MruList.java,v 1.4 2005/04/26 17:41:59 thlee Exp $ $Name:  $
 */
public class MruList implements Set
{

    /**
     * Represent an entry in the MruList
     * @author TiongHiang Lee
     */
    protected static class MruEntry implements Comparable
    {

        /** the last access time * */
        private long _lastAccessTime;

        /** the object * */
        private Object _obj;

        /**
         * Constructor
         * @param obj the object
         * @param time the time
         */
        public MruEntry(Object obj, long time)
        {
            _obj = obj;
            _lastAccessTime = time;
        }

        /**
         * Compare by the access time
         * @param e another entry
         * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the
         * specified object.
         */
        public int compareTo(MruEntry e)
        {
            if (_lastAccessTime > e._lastAccessTime)
            {
                return -1;
            } else if (_lastAccessTime < e._lastAccessTime)
            {
                return 1;
            } else
            {
                if (_obj.equals(e._obj))
                {
                    return 0;
                } else if (_obj.hashCode() > e._obj.hashCode())
                {
                    return 1;
                } else
                {
                    return -1;
                }
            }
        }

        /**
         * {@inheritDoc}
         */
        public int compareTo(Object o)
        {
            return compareTo((MruEntry) o);
        }

        /**
         * Return the lastAccessTime
         * @return the lastAccessTime.
         */
        public final long getLastAccessTime()
        {
            return _lastAccessTime;
        }

        /**
         * Set the lastAccessTime
         * @param lastAccessTime The lastAccessTime to set.
         */
        public final void setLastAccessTime(long lastAccessTime)
        {
            _lastAccessTime = lastAccessTime;
        }

        /**
         * Return the obj
         * @return the obj.
         */
        public final Object getObj()
        {
            return _obj;
        }

        /**
         * Set the obj
         * @param obj The obj to set.
         */
        public final void setObj(Object obj)
        {
            _obj = obj;
        }

        /**
         * {@inheritDoc}
         */
        public String toString()
        {
            return (_obj + ": " + _lastAccessTime);
        }
    }

    /**
     * An iterator to the entries
     * @author TiongHiang Lee
     */
    protected static class MruIterator implements Iterator
    {

        /** the iterator * */
        private Iterator _entryIterator;

        /**
         * Constructor
         * @param entryIterator the iterator
         */
        public MruIterator(Iterator entryIterator)
        {
            _entryIterator = entryIterator;
        }

        /**
         * {@inheritDoc}
         */
        public boolean hasNext()
        {
            return _entryIterator.hasNext();
        }

        /**
         * {@inheritDoc}
         */
        public Object next()
        {
            MruEntry entry = (MruEntry) _entryIterator.next();
            return entry._obj;
        }

        /**
         * {@inheritDoc}
         */
        public void remove()
        {
            _entryIterator.remove();
        }
    }

    /** the entries map * */
    private HashMap _entryMap = new HashMap();

    /** the last cleanup time * */
    private long _lastCleanupTime;

    /** the sorted mru list * */
    private TreeSet _mruList = new TreeSet();

    /** the size * */
    private long _sizeLimit;

    /** the timeout * */
    private long _timeout;

    /**
     * {@inheritDoc}
     */
    public MruList()
    {
        this(0, 0);
    }

    /**
     * {@inheritDoc}
     * @param sizeLimit the size limit of the MruList (0 for no size limit)
     * @param timeout the timeout (0 for never timeout)
     */
    public MruList(long sizeLimit, long timeout)
    {
        _sizeLimit = sizeLimit;
        _timeout = timeout;
        _lastCleanupTime = System.currentTimeMillis();
    }

    /**
     * Record that object o is being accessed. This will put a timestamp to the object
     * @param o the object
     * @return true if the object was in the list before
     */
    public boolean access(Object o)
    {
        long now = System.currentTimeMillis();
        if ((_timeout > 0) && ((now - _lastCleanupTime) > _timeout))
        {
            expireEntries(_timeout);
        }
        boolean flag = false;
        MruEntry entry = (MruEntry) _entryMap.get(o);
        if (entry != null)
        { //exist already
            _mruList.remove(entry);//must remove to add again later
            entry._lastAccessTime = now;
            flag = true;
        } else
        {
            entry = new MruEntry(o, now);
            _entryMap.put(o, entry);
        }
        _mruList.add(entry);
        if ((_sizeLimit > 0) && (size() > _sizeLimit))
        {
            truncateEntries(_sizeLimit);
        }
        return flag;
    }

    /**
     * @see access(Object o)
     */
    public boolean add(Object o)
    {
        return access(o);
    }

    /**
     * {@inheritDoc}
     */
    public boolean addAll(Collection c)
    {
        Iterator it = c.iterator();
        while (it.hasNext())
        {
            add(it.next());
        }
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public void clear()
    {
        _entryMap.clear();
        _mruList.clear();
    }

    /**
     * {@inheritDoc}
     */
    public boolean contains(Object o)
    {
        return _entryMap.containsKey(o);
    }

    /**
     * {@inheritDoc}
     */
    public boolean containsAll(Collection c)
    {
        return _entryMap.keySet().containsAll(c);
    }

    /**
     * Expire the entries that was last access longer that time t Document this method.
     * @param t the elapse time
     */
    public void expireEntries(long t)
    {
        long now = System.currentTimeMillis();
        _lastCleanupTime = now;
        do
        {
            MruEntry entry = (MruEntry) _mruList.last();
            if (entry == null)
            {
                break;
            } else if ((now - entry._lastAccessTime) > t)
            {
                expireEntry(entry._obj);                
            } else
            {
                break;
            }
        } while (true);
    }

    /**
     * Get the last access time object obj
     * @param obj the object
     * @return the access time, or -1 if the object is not in the cache
     */
    public long getLastAccessTime(Object obj)
    {
        MruEntry entry = (MruEntry) _entryMap.get(obj);
        if (entry != null)
        {
            return entry._lastAccessTime;
        } else
        {
            return -1;
        }
    }

    /**
     * {@inheritDoc}
     */
    public boolean isEmpty()
    {
        return _entryMap.size() == 0;
    }

    /**
     * {@inheritDoc}
     */
    public Iterator iterator()
    {
        return new MruIterator(_mruList.iterator());
    }

    /**
     * {@inheritDoc}
     */
    public boolean remove(Object o)
    {
        MruEntry entry = (MruEntry) _entryMap.remove(o);
        boolean flag = false;
        if (entry != null)
        {
            _mruList.remove(entry);
            flag = true;
        }
        return flag;
    }

    /**
     * {@inheritDoc}
     */
    public boolean removeAll(Collection c)
    {
        boolean flag = false;
        Iterator it = c.iterator();
        while (it.hasNext())
        {
            if (remove(it.next()))
            {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * {@inheritDoc}
     */
    public boolean retainAll(Collection c)
    {
        Iterator it = _entryMap.keySet().iterator();
        boolean flag = false;
        while (it.hasNext())
        {
            Object obj = it.next();
            if (!c.contains(obj))
            {
                remove(obj);
                flag = true;
            }
        }
        return flag;
    }

    /**
     * {@inheritDoc}
     */
    public int size()
    {
        return _entryMap.size();
    }

    /**
     * {@inheritDoc}
     */
    public Object[] toArray()
    {
        throw new UnsupportedOperationException("Not implemented");
    }

    /**
     * {@inheritDoc}
     */
    public Object[] toArray(Object[] a)
    {
        throw new UnsupportedOperationException("Not implemented");
    }

    /**
     * Truncate the entries to specific size
     * @param size the size
     */
    public void truncateEntries(long size)
    {
        while (size() > size)
        {
            MruEntry entry = (MruEntry) _mruList.last();
            truncateEntry(entry._obj);            
        }
    }        

    /**
     * Remove the object from the MruList
     * @param obj the object
     */
    protected void truncateEntry(Object obj)
    {
        remove(obj);
    }
    
    /**
     * Remove the entry from the MruList
     * @param obj expire the entry
     */
    protected void expireEntry(Object obj)
    {
        remove(obj);
    }        
}