LimeWire collection component api

org.limewire.collection
Class FixedSizeExpiringSet<T>

java.lang.Object
  extended by org.limewire.collection.FixedSizeExpiringSet<T>
All Implemented Interfaces:
java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>

public class FixedSizeExpiringSet<T>
extends java.lang.Object
implements java.util.Set<T>, java.util.Collection<T>

Maintains, at most, a fixed size of objects in a Set for a specified time. FixedSizeExpiringSet never holds more entries than specified in the constructor. This class is a hash-based Set and therefore, objects must correctly contain a Object.hashCode() and Object.equals(Object).

Note: expiration times longer than Long.MAX_VALUE / 10^6 will be truncated.

    try{        
        FixedSizeExpiringSet<String> fses = new FixedSizeExpiringSet<String>(4, 2000);

        fses.add("Abby");
        fses.add("Bob");
        fses.add("Chris");
        fses.add("Dan");
        fses.add("Eric");   

        System.out.println("Size: " + fses.size());
        Thread.sleep(1000);
        System.out.println("Size: " + fses.size());
        Thread.sleep(2000);
        System.out.println("Size (after expiration): " + fses.size());
    } catch(Exception e) {
        e.printStackTrace();
    }

    Output:
        Size: 4
        Size: 4
        Size (after expiration): 0


Constructor Summary
FixedSizeExpiringSet()
          Simple constructor for the FixedSizeExpiringSet.
FixedSizeExpiringSet(int size)
          Constructor for the FixedSizeExpiringSet.
FixedSizeExpiringSet(int size, long expireTime)
          Constructor for the FixedSizeExpiringSet.
 
Method Summary
 boolean add(T arg0)
           
 boolean addAll(java.util.Collection<? extends T> coll)
          Adds all the elements in collection to this.
 void clear()
           
 boolean contains(java.lang.Object arg0)
           
 boolean containsAll(java.util.Collection<?> arg0)
           
 boolean isEmpty()
           
 java.util.Iterator<T> iterator()
           
 boolean remove(java.lang.Object arg0)
           
 boolean removeAll(java.util.Collection<?> arg0)
           
 boolean retainAll(java.util.Collection<?> arg0)
           
 int size()
           
 java.lang.Object[] toArray()
           
<B> B[]
toArray(B[] arg0)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Set
equals, hashCode
 

Constructor Detail

FixedSizeExpiringSet

public FixedSizeExpiringSet()
Simple constructor for the FixedSizeExpiringSet. Takes no arguments.


FixedSizeExpiringSet

public FixedSizeExpiringSet(int size)
Constructor for the FixedSizeExpiringSet.

Parameters:
size - the max size of the set

FixedSizeExpiringSet

public FixedSizeExpiringSet(int size,
                            long expireTime)
Constructor for the FixedSizeExpiringSet.

Parameters:
size - the max size of the set
expireTime - the time to keep an entry
Method Detail

size

public int size()
Specified by:
size in interface java.util.Collection<T>
Specified by:
size in interface java.util.Set<T>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Collection<T>
Specified by:
isEmpty in interface java.util.Set<T>

contains

public boolean contains(java.lang.Object arg0)
Specified by:
contains in interface java.util.Collection<T>
Specified by:
contains in interface java.util.Set<T>

iterator

public java.util.Iterator<T> iterator()
Specified by:
iterator in interface java.lang.Iterable<T>
Specified by:
iterator in interface java.util.Collection<T>
Specified by:
iterator in interface java.util.Set<T>

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection<T>
Specified by:
toArray in interface java.util.Set<T>

toArray

public <B> B[] toArray(B[] arg0)
Specified by:
toArray in interface java.util.Collection<T>
Specified by:
toArray in interface java.util.Set<T>

add

public boolean add(T arg0)
Specified by:
add in interface java.util.Collection<T>
Specified by:
add in interface java.util.Set<T>

remove

public boolean remove(java.lang.Object arg0)
Specified by:
remove in interface java.util.Collection<T>
Specified by:
remove in interface java.util.Set<T>

containsAll

public boolean containsAll(java.util.Collection<?> arg0)
Specified by:
containsAll in interface java.util.Collection<T>
Specified by:
containsAll in interface java.util.Set<T>

addAll

public boolean addAll(java.util.Collection<? extends T> coll)
Adds all the elements in collection to this. If the size of the collection is bigger than _maxSize only the first _maxSize elements are added.

Specified by:
addAll in interface java.util.Collection<T>
Specified by:
addAll in interface java.util.Set<T>
See Also:
(java.util.Collection)

retainAll

public boolean retainAll(java.util.Collection<?> arg0)
Specified by:
retainAll in interface java.util.Collection<T>
Specified by:
retainAll in interface java.util.Set<T>
See Also:
(java.util.Collection)

removeAll

public boolean removeAll(java.util.Collection<?> arg0)
Specified by:
removeAll in interface java.util.Collection<T>
Specified by:
removeAll in interface java.util.Set<T>

clear

public void clear()
Specified by:
clear in interface java.util.Collection<T>
Specified by:
clear in interface java.util.Set<T>

LimeWire collection component api

Copyright © 2009 Lime Wire LLC. All Rights Reserved.