Package com.globalmentor.collections
Class AbstractConverterList<S,D>
- java.lang.Object
-
- com.globalmentor.collections.CollectionDecorator<E>
-
- com.globalmentor.collections.ListDecorator<D>
-
- com.globalmentor.collections.AbstractConverterList<S,D>
-
- Type Parameters:
S
- The type of element contained in the source list.D
- The type of element contained in the destination list.
- All Implemented Interfaces:
java.lang.Iterable<D>
,java.util.Collection<D>
,java.util.List<D>
public abstract class AbstractConverterList<S,D> extends ListDecorator<D>
A list that provides access to another list, automatically converting elements to objects possibly of a different type. The conversion is done on the fly as elements are requested, and not before.- Author:
- Garret Wilson
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AbstractConverterList.ConverterIterator
A custom proxied iterator that converts returned elements on the fly.protected class
AbstractConverterList.ConverterListIterator
A custom proxied list iterator that converts returned elements on the fly.
-
Field Summary
-
Fields inherited from class com.globalmentor.collections.ListDecorator
list
-
Fields inherited from class com.globalmentor.collections.CollectionDecorator
collection
-
-
Constructor Summary
Constructors Constructor Description AbstractConverterList(java.util.List<S> list)
List constructor that converts objects from scratch on each request.AbstractConverterList(java.util.List<S> list, boolean reuse)
List and reuse constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract D
convert(S source)
Converts an object in the list to another object.D
get(int index)
Converts and returns the element at the specified position in this list.java.util.Iterator<D>
iterator()
java.util.ListIterator<D>
listIterator()
java.util.ListIterator<D>
listIterator(int index)
Returns a custom proxied list iterator that will convert returned elements on the fly.-
Methods inherited from class com.globalmentor.collections.ListDecorator
add, addAll, indexOf, lastIndexOf, remove, replaceAll, set, sort, subList
-
Methods inherited from class com.globalmentor.collections.CollectionDecorator
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray
-
-
-
-
Constructor Detail
-
AbstractConverterList
public AbstractConverterList(java.util.List<S> list)
List constructor that converts objects from scratch on each request.- Parameters:
list
- The list the elements of which this list should convert.
-
AbstractConverterList
public AbstractConverterList(java.util.List<S> list, boolean reuse)
List and reuse constructor.- Parameters:
list
- The list the elements of which this list should convert.reuse
-true
if the converter should remember and reuse converted objects, orfalse
if the conversion should take place from scratch upon each request.
-
-
Method Detail
-
convert
protected abstract D convert(S source)
Converts an object in the list to another object.- Parameters:
source
- The object to convert.- Returns:
- The converted object.
-
get
public D get(int index)
Converts and returns the element at the specified position in this list.- Specified by:
get
in interfacejava.util.List<S>
- Overrides:
get
in classListDecorator<D>
- Parameters:
index
- The index of element to convert and return.- Returns:
- The converted element at the specified position in this list.
- Throws:
java.lang.IndexOutOfBoundsException
- Thrown if the index is out of range (index < 0 || index > size()).
-
iterator
public java.util.Iterator<D> iterator()
- Specified by:
iterator
in interfacejava.util.Collection<S>
- Specified by:
iterator
in interfacejava.lang.Iterable<S>
- Specified by:
iterator
in interfacejava.util.List<S>
- Overrides:
iterator
in classCollectionDecorator<D>
- Returns:
- A custom proxied iterator that will convert returned elements on the fly.
-
listIterator
public java.util.ListIterator<D> listIterator()
- Specified by:
listIterator
in interfacejava.util.List<S>
- Overrides:
listIterator
in classListDecorator<D>
- Returns:
- A custom proxied list iterator that will convert returned elements on the fly.
-
listIterator
public java.util.ListIterator<D> listIterator(int index)
Returns a custom proxied list iterator that will convert returned elements on the fly.- Specified by:
listIterator
in interfacejava.util.List<S>
- Overrides:
listIterator
in classListDecorator<D>
- Parameters:
index
- The index of first element to be returned from the list iterator (by a call to thenext
method).- Returns:
- A custom proxied list iterator that will convert returned elements.
- Throws:
java.lang.IndexOutOfBoundsException
- Thrown if the index is out of range (index < 0 || index > size()).
-
-