LimeWire Collection Component API

org.limewire.collection
Class FixedSizeExpiringSet<T>

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

public class FixedSizeExpiringSet<T>
extends Object
implements Set<T>, 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

Author:
Gregorio Roper

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(Collection<? extends T> coll)
          Adds all the elements in collection to this.
 void clear()
           
 boolean contains(Object arg0)
           
 boolean containsAll(Collection<?> arg0)
          (non-Javadoc)
 boolean isEmpty()
           
 Iterator<T> iterator()
           
 boolean remove(Object arg0)
           
 boolean removeAll(Collection<?> arg0)
           
 boolean retainAll(Collection<?> arg0)
           
 int size()
           
 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 Collection<T>
Specified by:
size in interface Set<T>

isEmpty

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

contains

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

iterator

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

toArray

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

toArray

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

add

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

remove

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

containsAll

public boolean containsAll(Collection<?> arg0)
(non-Javadoc)

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

addAll

public boolean addAll(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 Collection<T>
Specified by:
addAll in interface Set<T>
See Also:
(java.util.Collection)

retainAll

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

removeAll

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

clear

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

LimeWire Collection Component API

Copyright © 2009 Lime Wire LLC. All Rights Reserved.