fulmine.util.collection
Class ReferenceCountingList<COMPONENT>

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

public final class ReferenceCountingList<COMPONENT>
extends Object
implements IReferenceCountingList<COMPONENT>

A List implementation backed by an internal list. It only allows unique entries but also provides counts per entry added/removed. If an entry is added twice, it is only added once but will have a count of 2.

This is not thread safe.

Author:
Ramon Servadei

Constructor Summary
ReferenceCountingList()
           
ReferenceCountingList(int size)
           
ReferenceCountingList(List<COMPONENT> copy)
           
ReferenceCountingList(ReferenceCountingList<COMPONENT> copy)
           
 
Method Summary
 boolean add(COMPONENT o)
          Add the element to the list.
 void add(int index, COMPONENT element)
           
 boolean addAll(Collection<? extends COMPONENT> c)
           
 boolean addAll(int index, Collection<? extends COMPONENT> c)
           
 void clear()
           
 boolean contains(Object o)
           
 boolean containsAll(Collection<?> c)
           
 boolean equals(Object o)
           
 COMPONENT get(int index)
           
 int getCount(COMPONENT element)
          Get the number of references this list maintains for the element.
 int hashCode()
           
 int indexOf(Object o)
           
 boolean isEmpty()
           
 Iterator<COMPONENT> iterator()
           
 int lastIndexOf(Object o)
           
 ListIterator<COMPONENT> listIterator()
           
 ListIterator<COMPONENT> listIterator(int index)
           
 COMPONENT remove(int index)
           
 boolean remove(Object o)
          Remove the element from the list.
 boolean removeAll(Collection<?> c)
           
 boolean retainAll(Collection<?> c)
           
 COMPONENT set(int index, COMPONENT element)
           
 int size()
           
 List<COMPONENT> subList(int fromIndex, int toIndex)
           
 Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ReferenceCountingList

public ReferenceCountingList()

ReferenceCountingList

public ReferenceCountingList(int size)

ReferenceCountingList

public ReferenceCountingList(List<COMPONENT> copy)

ReferenceCountingList

public ReferenceCountingList(ReferenceCountingList<COMPONENT> copy)
Method Detail

add

public boolean add(COMPONENT o)
Add the element to the list. If it is already in the list, the reference count is incremented

Specified by:
add in interface Collection<COMPONENT>
Specified by:
add in interface List<COMPONENT>
Parameters:
o - the element to add
Returns:
true only if the element is actually added
See Also:
List.add(Object)

add

public void add(int index,
                COMPONENT element)
Specified by:
add in interface List<COMPONENT>

addAll

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

addAll

public boolean addAll(int index,
                      Collection<? extends COMPONENT> c)
Specified by:
addAll in interface List<COMPONENT>

clear

public void clear()
Specified by:
clear in interface Collection<COMPONENT>
Specified by:
clear in interface List<COMPONENT>

contains

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

containsAll

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

get

public COMPONENT get(int index)
Specified by:
get in interface List<COMPONENT>

indexOf

public int indexOf(Object o)
Specified by:
indexOf in interface List<COMPONENT>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<COMPONENT>
Specified by:
isEmpty in interface List<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 List<COMPONENT>

lastIndexOf

public int lastIndexOf(Object o)
Specified by:
lastIndexOf in interface List<COMPONENT>

listIterator

public ListIterator<COMPONENT> listIterator()
Specified by:
listIterator in interface List<COMPONENT>

listIterator

public ListIterator<COMPONENT> listIterator(int index)
Specified by:
listIterator in interface List<COMPONENT>

remove

public COMPONENT remove(int index)
Specified by:
remove in interface List<COMPONENT>

remove

public boolean remove(Object o)
Remove the element from the list. The reference count is decremented if it is found. If the reference count is 0, the element is actually removed from the list.

Specified by:
remove in interface Collection<COMPONENT>
Specified by:
remove in interface List<COMPONENT>
Parameters:
o - the element to remove
Returns:
true only if the element is actually removed
See Also:
List#remove(o)

removeAll

public boolean removeAll(Collection<?> c)
Specified by:
removeAll in interface Collection<COMPONENT>
Specified by:
removeAll in interface List<COMPONENT>

retainAll

public boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<COMPONENT>
Specified by:
retainAll in interface List<COMPONENT>

set

public COMPONENT set(int index,
                     COMPONENT element)
Specified by:
set in interface List<COMPONENT>

size

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

subList

public List<COMPONENT> subList(int fromIndex,
                               int toIndex)
Specified by:
subList in interface List<COMPONENT>

toArray

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

toArray

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

getCount

public int getCount(COMPONENT element)
Description copied from interface: IReferenceCountingList
Get the number of references this list maintains for the element. Calls to List.add(Object) increments the count, calls to List.remove(Object) decrements the count.

Specified by:
getCount in interface IReferenceCountingList<COMPONENT>
Parameters:
element - the element to query for the reference count
Returns:
the number of references to the element for this list

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 List<COMPONENT>
Overrides:
equals in class Object

hashCode

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


Copyright © 2007-2009. All Rights Reserved.