Class Listifiers
This class provides built-in list conversion strategies that handle common Java collection
and iterable types. These listifiers are automatically registered when using
BasicBeanConverter.Builder.defaultSettings()
.
Purpose:
Listifiers convert various collection-like objects to List
instances for uniform
processing in BCT assertions. This enables consistent iteration and element access across
different data structures like arrays, iterators, streams, and custom collections.
Built-in Listifiers:
collectionListifier()
- ConvertsCollection
objects toArrayList
iterableListifier()
- ConvertsIterable
objects using streamsiteratorListifier()
- ConvertsIterator
objects to lists (consumes iterator)enumerationListifier()
- ConvertsEnumeration
objects to listsstreamListifier()
- ConvertsStream
objects to lists (terminates stream)mapListifier()
- ConvertsMap
to list ofMap.Entry
objects
Usage Example:
Iterator Consumption:
Warning: Some listifiers consume their input objects during conversion:
Iterator
: Elements are consumed and iterator becomes exhaustedEnumeration
: Elements are consumed and enumeration becomes exhaustedStream
: Stream is terminated and cannot be reused
Custom Listifier Development:
When creating custom listifiers, follow these patterns:
- Null Safety: Handle
null inputs gracefully - Immutability: Return new lists rather than modifying inputs
- Type Safety: Ensure proper generic type handling
- Performance: Consider memory usage for large collections
-
Method Summary
Modifier and TypeMethodDescriptionstatic Listifier<Collection>
Returns a listifier forCollection
objects that converts them toArrayList
.static Listifier<Enumeration>
Returns a listifier forEnumeration
objects that converts them to lists.Returns a listifier forIterable
objects that converts them using streams.Returns a listifier forIterator
objects that converts them to lists.Returns a listifier forStream
objects that converts them to lists.
-
Method Details
-
collectionListifier
Returns a listifier forCollection
objects that converts them toArrayList
.This listifier creates a new ArrayList containing all elements from the source collection. It works with any Collection subtype including List, Set, Queue, and Deque.
Behavior:
- Non-empty collections: Returns new ArrayList with all elements in iteration order
- Empty collections: Returns new empty ArrayList
- Preserves order: Maintains the iteration order of the source collection
- Set ordering: Converts unordered Sets (HashSet, etc.) to TreeSet for deterministic ordering
Set Ordering Behavior:
To ensure predictable test results, this listifier handles Sets with unreliable ordering:
SortedSet
(TreeSet, etc.): Preserves existing sort orderLinkedHashSet
: Preserves insertion orderHashSet
and other unordered Sets: Converts toTreeSet
for natural ordering
Usage Examples:
// Test with different collection types var list = List.of ("a" ,"b" ,"c" );assertList (list ,"a" ,"b" ,"c" );// HashSet converted to TreeSet for predictable ordering var set = Set.of ("z" ,"a" ,"m" );assertList (set ,"a" ,"m" ,"z" );// Natural ordering // LinkedHashSet preserves insertion order var linkedSet =new LinkedHashSet<>(Arrays.asList ("first" ,"second" ));assertList (linkedSet ,"first" ,"second" );var queue =new LinkedList<>(Arrays.asList ("first" ,"second" ));assertList (queue ,"first" ,"second" );Performance:
This listifier creates a new ArrayList and copies all elements, so it has O(n) time and space complexity. For unordered Sets, an additional TreeSet conversion adds O(n log n) sorting overhead. For large collections, consider the memory implications.
- Returns:
- A
Listifier
forCollection
objects - See Also:
-
iterableListifier
Returns a listifier forIterable
objects that converts them using streams.This listifier handles any Iterable implementation by creating a stream from its spliterator and collecting elements to a list. It works with custom iterables and collection types not covered by more specific listifiers.
Behavior:
- Standard iterables: Converts elements to list maintaining iteration order
- Custom iterables: Works with any object implementing Iterable interface
- Large iterables: Processes all elements regardless of size
Usage Examples:
// Test with custom iterable var range = IntStream.range (1 ,4 ).boxed ().collect (toList());assertList (range ,1 ,2 ,3 );// Test with custom Iterable implementation var fibonacci =new FibonacciIterable(5 );assertList (fibonacci ,1 ,1 ,2 ,3 ,5 ); -
iteratorListifier
Returns a listifier forIterator
objects that converts them to lists.Warning: This listifier consumes the iterator during conversion. After listification, the iterator will be exhausted and cannot be used again.
Behavior:
- Element extraction: Consumes all remaining elements from the iterator
- Order preservation: Maintains the iterator's order in the resulting list
- Iterator exhaustion: The iterator becomes unusable after conversion
Usage Examples:
// Test with list iterator var list = List.of ("a" ,"b" ,"c" );var iterator =list .iterator();assertList (iterator ,"a" ,"b" ,"c" );// iterator is now exhausted // Test with custom iterator var numbers = IntStream.range (1 ,4 ).iterator();assertList (numbers ,1 ,2 ,3 );Important Notes:
- One-time use: The iterator becomes exhausted and unusable after conversion
- Side effects: Any side effects in the iterator's next() method will occur
- Exception handling: Exceptions from the iterator are propagated
-
enumerationListifier
Returns a listifier forEnumeration
objects that converts them to lists.Warning: This listifier consumes the enumeration during conversion. After listification, the enumeration will be exhausted and cannot be used again.
Behavior:
- Element extraction: Consumes all remaining elements from the enumeration
- Order preservation: Maintains the enumeration's order in the resulting list
- Enumeration exhaustion: The enumeration becomes unusable after conversion
Usage Examples:
// Test with Vector enumeration var vector =new Vector<>(List.of ("x" ,"y" ,"z" ));var enumeration =vector .elements();assertList (enumeration ,"x" ,"y" ,"z" );// Test with Hashtable enumeration var table =new Hashtable<>(Map.of ("key1" ,"value1" ));var keys =table .keys();assertList (keys ,"key1" );- Returns:
- A
Listifier
forEnumeration
objects - See Also:
-
streamListifier
Returns a listifier forStream
objects that converts them to lists.Warning: This listifier terminates the stream during conversion. After listification, the stream is closed and cannot be used again.
Behavior:
- Stream termination: Calls toList() to collect all stream elements
- Order preservation: Maintains stream order in the resulting list
- Stream closure: The stream becomes unusable after conversion
Usage Examples:
// Test with filtered stream var numbers = IntStream.range (1 ,10 ) .filter(n ->n %2 ==0 ) .boxed();assertList (numbers ,2 ,4 ,6 ,8 );// Test with mapped stream var words = Stream.of ("hello" ,"world" ) .map(String::toUpperCase);assertList (words ,"HELLO" ,"WORLD" );Important Notes:
- One-time use: The stream is terminated and cannot be reused
- Lazy evaluation: Stream operations are executed during listification
- Exception handling: Stream operation exceptions are propagated
-
mapListifier
Returns a listifier forMap
objects that converts them to lists ofMap.Entry
objects.This listifier enables maps to be processed as lists in BCT assertions, making it easy to test map contents using list-based assertion methods.
Behavior:
- Entry conversion: Each key-value pair becomes a Map.Entry in the list
- Order preservation: Maintains the map's iteration order
- Empty maps: Returns empty list for empty maps
- Map ordering: Converts unordered Maps (HashMap, etc.) to TreeMap for deterministic ordering
Map Ordering Behavior:
To ensure predictable test results, this listifier handles Maps with unreliable ordering:
SortedMap
(TreeMap, etc.): Preserves existing sort orderLinkedHashMap
: Preserves insertion orderHashMap
and other unordered Maps: Converts toTreeMap
for natural key ordering
Usage Examples:
// Test map contents with deterministic ordering var map = Map.of ("z" ,"value1" ,"a" ,"value2" );assertSize (map ,2 );// Entries will be ordered by key: [a=value2, z=value1] // LinkedHashMap preserves insertion order var linkedMap =new LinkedHashMap<>();linkedMap .put("first" ,"1" );linkedMap .put("second" ,"2" );// Entries will maintain insertion order: [first=1, second=2] // Test empty map var emptyMap = Map.of ();assertEmpty (emptyMap );// Test map in object property var config =new Configuration().setProperties(map );assertSize (config ,"properties" ,2 );Entry Processing:
The resulting Map.Entry objects can be further processed by other parts of the conversion system, typically being stringified to
"key=value" format.Performance:
This listifier creates a new ArrayList from the map's entrySet. For unordered Maps, an additional TreeMap conversion adds O(n log n) sorting overhead based on key ordering. For large maps, consider the memory implications.
-