public class NbCollections extends Object
Note that there is no checkedListByFilter
method currently.
If constant-time operation is important (e.g. your raw list is large and RandomAccess
)
you can use NbCollections.checkedListByCopy(java.util.List, java.lang.Class<E>, boolean)
, assuming you do not need to modify the underlying list.
If you are only interested in an iterator anyway, try NbCollections.checkedIteratorByFilter(java.util.Iterator, java.lang.Class<E>, boolean)
.
Modifier and Type | Method and Description |
---|---|
static <E> Enumeration<E> |
checkedEnumerationByFilter(Enumeration<?> rawEnum,
Class<E> type,
boolean strict)
Create a typesafe filter of an unchecked enumeration.
|
static <E> Iterator<E> |
checkedIteratorByFilter(Iterator rawIterator,
Class<E> type,
boolean strict)
Create a typesafe filter of an unchecked iterator.
|
static <E> List<E> |
checkedListByCopy(List rawList,
Class<E> type,
boolean strict)
Create a typesafe copy of a raw list.
|
static <K,V> Map<K,V> |
checkedMapByCopy(Map rawMap,
Class<K> keyType,
Class<V> valueType,
boolean strict)
Create a typesafe copy of a raw map.
|
static <K,V> Map<K,V> |
checkedMapByFilter(Map rawMap,
Class<K> keyType,
Class<V> valueType,
boolean strict)
Create a typesafe view over an underlying raw map.
|
static <E> Set<E> |
checkedSetByCopy(Set rawSet,
Class<E> type,
boolean strict)
Create a typesafe copy of a raw set.
|
static <E> Set<E> |
checkedSetByFilter(Set rawSet,
Class<E> type,
boolean strict)
Create a typesafe view over an underlying raw set.
|
static <E> Iterable<E> |
iterable(Enumeration<E> enumeration)
Treat an
Enumeration as an Iterable so it can be used in an enhanced for-loop. |
static <E> Iterable<E> |
iterable(Iterator<E> iterator)
|
public static <E> Set<E> checkedSetByCopy(Set rawSet, Class<E> type, boolean strict) throws ClassCastException
rawSet
- an unchecked settype
- the desired supertype of the entriesstrict
- true to throw a ClassCastException
if the raw set has an invalid entry,
false to skip over such entries (warnings may be logged)ClassCastException
- if some entry in the raw set was not well-typed, and only if strict
was truepublic static <E> List<E> checkedListByCopy(List rawList, Class<E> type, boolean strict) throws ClassCastException
rawList
- an unchecked listtype
- the desired supertype of the entriesstrict
- true to throw a ClassCastException
if the raw list has an invalid entry,
false to skip over such entries (warnings may be logged)ClassCastException
- if some entry in the raw list was not well-typed, and only if strict
was truepublic static <K,V> Map<K,V> checkedMapByCopy(Map rawMap, Class<K> keyType, Class<V> valueType, boolean strict) throws ClassCastException
rawMap
- an unchecked mapkeyType
- the desired supertype of the keysvalueType
- the desired supertype of the valuesstrict
- true to throw a ClassCastException
if the raw map has an invalid key or value,
false to skip over such map entries (warnings may be logged)ClassCastException
- if some key or value in the raw map was not well-typed, and only if strict
was truepublic static <E> Iterator<E> checkedIteratorByFilter(Iterator rawIterator, Class<E> type, boolean strict)
Iterator.remove()
will work if it does in the unchecked iterator.rawIterator
- an unchecked iteratortype
- the desired enumeration typestrict
- if false, elements which are not null but not assignable to the requested type are omitted;
if true, ClassCastException
may be thrown from an iterator operationpublic static <E> Set<E> checkedSetByFilter(Set rawSet, Class<E> type, boolean strict)
Set.clear()
will make the view empty but may not clear the underlying set.
You may add elements only of the requested type.
Set.contains(java.lang.Object)
also performs a type check and will throw ClassCastException
for an illegal argument.
The view is serializable if the underlying set is.rawSet
- an unchecked settype
- the desired element typestrict
- if false, elements in the underlying set which are not null and which are not assignable
to the requested type are omitted from the view;
if true, a ClassCastException
may arise during some set operationpublic static <K,V> Map<K,V> checkedMapByFilter(Map rawMap, Class<K> keyType, Class<V> valueType, boolean strict)
Map.clear()
will make the view empty but may not clear the underlying map.
You may add entries only of the requested type pair.
Map.get(java.lang.Object)
, Map.containsKey(java.lang.Object)
, and Map.containsValue(java.lang.Object)
also perform a type check
and will throw ClassCastException
for an illegal argument.
The view is serializable if the underlying map is.rawMap
- an unchecked mapkeyType
- the desired entry key typevalueType
- the desired entry value typestrict
- if false, entries in the underlying map for which the key is not null but not assignable
to the requested key type, and/or the value is not null but not assignable to
the requested value type, are omitted from the view;
if true, a ClassCastException
may arise during some map operationpublic static <E> Enumeration<E> checkedEnumerationByFilter(Enumeration<?> rawEnum, Class<E> type, boolean strict)
rawEnum
- an unchecked enumerationtype
- the desired enumeration typestrict
- if false, elements which are not null but not assignable to the requested type are omitted;
if true, ClassCastException
may be thrown from an enumeration operationpublic static <E> Iterable<E> iterable(Iterator<E> iterator)
Iterator
as an Iterable
so it can be used in an enhanced for-loop.
Bear in mind that the iterator is "consumed" by the loop and so should be used only once.
Generally it is best to put the code which obtains the iterator inside the loop header.
Example of correct usage:
String text = ...;
for (String token : NbCollections.iterable(new Scanner
(text))) {
// ...
}
iterator
- an iteratorNullPointerException
- if the iterator is nullpublic static <E> Iterable<E> iterable(Enumeration<E> enumeration)
Enumeration
as an Iterable
so it can be used in an enhanced for-loop.
Bear in mind that the enumeration is "consumed" by the loop and so should be used only once.
Generally it is best to put the code which obtains the enumeration inside the loop header.
Example of correct usage:
ClassLoader loader = ...;
String name = ...;
for (URL resource : NbCollections.iterable(loader.getResources
(name))) {
// ...
}
enumeration
- an enumerationIterator.remove()
is not supported)NullPointerException
- if the enumeration is null