fulmine.util.collection
Class TtlSet<COMPONENT>

java.lang.Object
  extended by fulmine.util.collection.TtlSet<COMPONENT>
Type Parameters:
COMPONENT - the element type contained by the list
All Implemented Interfaces:
Iterable<COMPONENT>, Collection<COMPONENT>, Set<COMPONENT>

public final class TtlSet<COMPONENT>
extends Object
implements Set<COMPONENT>

A Set implementation backed by an internal set. It tracks the 'time-to-live' of entries in the internal set. When entries are added, they are given a TTL (time-to-live) and on each successive call to Set.clear(), Set.remove(Object), Set.removeAll(Collection) or any other 'reducing' method, the TTL is decreased. When the TTL reaches 0, the entry is removed.

This is not thread safe.

Author:
Ramon Servadei

Field Summary
static int DEFAULT_TTL
          The default TTL
 
Constructor Summary
TtlSet()
          Standard constructor for the TtlSet.
TtlSet(int ttl)
          Construct the TtlSet with the specified TTL for any new entry added.
 
Method Summary
 boolean add(COMPONENT o)
           
 boolean addAll(Collection<? extends COMPONENT> c)
           
 void clear()
          Decreases the TTL of all entries by 1.
 boolean contains(Object o)
           
 boolean containsAll(Collection<?> c)
           
 boolean equals(Object o)
           
 int hashCode()
           
 boolean isEmpty()
           
 Iterator<COMPONENT> iterator()
           
 boolean remove(Object o)
          Decreases the TTL of the element by 1.
 boolean removeAll(Collection<?> c)
          For each element in the passed in collection, calls remove(Object).
 boolean retainAll(Collection<?> c)
          Unsupported.
 int size()
           
 Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_TTL

public static final int DEFAULT_TTL
The default TTL

See Also:
Constant Field Values
Constructor Detail

TtlSet

public TtlSet()
Standard constructor for the TtlSet. Sets the startingTtl to DEFAULT_TTL.


TtlSet

public TtlSet(int ttl)
Construct the TtlSet with the specified TTL for any new entry added.

Parameters:
ttl - the TTL for new entries
Method Detail

add

public boolean add(COMPONENT o)
Specified by:
add in interface Collection<COMPONENT>
Specified by:
add in interface Set<COMPONENT>

addAll

public boolean addAll(Collection<? extends COMPONENT> c)
Specified by:
addAll in interface Collection<COMPONENT>
Specified by:
addAll in interface Set<COMPONENT>

clear

public void clear()
Decreases the TTL of all entries by 1. Any entries with a TTL of 0 after this operation will be removed.

Specified by:
clear in interface Collection<COMPONENT>
Specified by:
clear in interface Set<COMPONENT>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<COMPONENT>
Specified by:
contains in interface Set<COMPONENT>

containsAll

public boolean containsAll(Collection<?> c)
Specified by:
containsAll in interface Collection<COMPONENT>
Specified by:
containsAll in interface Set<COMPONENT>

isEmpty

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

iterator

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

remove

public boolean remove(Object o)
Decreases the TTL of the element by 1. If the TTL is 0, the element is removed.

Specified by:
remove in interface Collection<COMPONENT>
Specified by:
remove in interface Set<COMPONENT>
Returns:
true if the element was removed, false otherwise
See Also:
Set.remove(Object)

removeAll

public boolean removeAll(Collection<?> c)
For each element in the passed in collection, calls remove(Object).

Specified by:
removeAll in interface Collection<COMPONENT>
Specified by:
removeAll in interface Set<COMPONENT>
Returns:
true if any element in this TtlSet was removed as a result of this call, false otherwise
See Also:
Set.removeAll(Collection)

retainAll

public boolean retainAll(Collection<?> c)
Unsupported.

Specified by:
retainAll in interface Collection<COMPONENT>
Specified by:
retainAll in interface Set<COMPONENT>
Throws:
UnsupportedOperationException

size

public int size()
Specified by:
size in interface Collection<COMPONENT>
Specified by:
size in interface Set<COMPONENT>

toArray

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

toArray

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

toString

public String toString()
Overrides:
toString in class Object

equals

public boolean equals(Object o)
Specified by:
equals in interface Collection<COMPONENT>
Specified by:
equals in interface Set<COMPONENT>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<COMPONENT>
Specified by:
hashCode in interface Set<COMPONENT>
Overrides:
hashCode in class Object


Copyright © 2007-2009. All Rights Reserved.