Class BasicBeanConverter
- All Implemented Interfaces:
BeanConverter
BeanConverter for Bean-Centric Test (BCT) object conversion.
This class provides a comprehensive, extensible framework for converting Java objects to strings and lists, with sophisticated property access capabilities. It's the core engine behind BCT testing assertions, handling complex object introspection and value extraction with high performance through intelligent caching and optimized lookup strategies.
Key Features:
- Extensible Type Handlers: Pluggable stringifiers, listifiers, and swappers for custom types
- Performance Optimization: ConcurrentHashMap caching for type-to-handler mappings
- Comprehensive Defaults: Built-in support for all common Java types and structures
- Configurable Settings: Customizable formatting, delimiters, and display options
- Thread Safety: Fully thread-safe implementation suitable for concurrent testing
Architecture Overview:
The converter uses four types of pluggable handlers:
- Stringifiers:
- Convert objects to string representations with custom formatting rules
- Listifiers:
- Convert collection-like objects to List<Object> for uniform iteration
- Swappers:
- Pre-process objects before conversion (unwrap Optional, call Supplier, etc.)
- PropertyExtractors:
- Define custom property access strategies for nested field navigation (e.g.,
"user.address.city" )
PropertyExtractors use a chain-of-responsibility pattern, where each extractor in the chain is tried until one can handle the property access. The framework includes built-in extractors for:
- JavaBean properties: Standard getter methods and public fields
- Collection/Array access: Numeric indices and size/length properties
- Map access: Key-based property retrieval and size property
Default Type Support:
Out-of-the-box stringification support includes:
- Collections: List, Set, Queue →
"[item1,item2,item3]" format - Maps: Map, Properties →
"{key1=value1,key2=value2}" format - Map Entries: Map.Entry →
"key=value" format - Arrays: All array types →
"[element1,element2]" format - Dates: Date, Calendar → ISO-8601 format
- Files/Streams: File, InputStream, Reader → content as hex or text
- Reflection: Class, Method, Constructor → human-readable signatures
- Enums: Enum values → name() format
Default listification support includes:
- Collection types: List, Set, Queue, and all subtypes
- Iterable objects: Any Iterable implementation
- Iterators: Iterator and Enumeration (consumed to list)
- Streams: Stream objects (terminated to list)
- Optional: Empty list or single-element list
- Maps: Converted to list of Map.Entry objects
Default swapping support includes:
- Optional: Unwrapped to contained value or
null - Supplier: Called to get supplied value
- Future: Extracts completed result or returns
"<pending>" for incomplete futures (viaSwappers.futureSwapper())
Configuration Settings:
The converter supports extensive customization via settings:
nullValue- String representation for null values (default:
"<null>" ) selfValue- Special property name that returns the object itself (default:
"<self>" ) emptyValue- String representation for empty collections (default:
"<empty>" ) fieldSeparator- Delimiter between collection elements and map entries (default:
"," ) collectionPrefix/Suffix- Brackets around collection content (default:
"[" and"]" ) mapPrefix/Suffix- Brackets around map content (default:
"{" and"}" ) mapEntrySeparator- Separator between map keys and values (default:
"=" ) calendarFormat- DateTimeFormatter for calendar objects (default:
ISO_INSTANT ) classNameFormat- Format for class names:
"simple" ,"canonical" , or"full" (default:"simple" )
Usage Examples:
Basic Usage with Defaults:
Custom Configuration:
Complex Property Access:
Special Property Values:
Performance Characteristics:
- Handler Lookup: O(1) average case via ConcurrentHashMap caching
- Type Registration: Handlers checked in reverse registration order (last wins)
- Inheritance Support: Handlers support class inheritance and interface implementation
- Thread Safety: Full concurrency support with no locking overhead after initialization
- Memory Efficiency: Minimal object allocation during normal operation
Extension Patterns:
Custom Type Stringification:
Custom Collection Handling:
Custom Object Transformation:
Integration with BCT:
This class is used internally by all BCT assertion methods in BctAssertions:
BctAssertions.assertBean(Object, String, String)- Uses getEntry() for property accessBctAssertions.assertList(List, Object...)- Uses stringify() for element comparisonBctAssertions.assertBeans(Collection, String, String...)- Uses both getEntry() and stringify()
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating customized BasicBeanConverter instances.(package private) static class(package private) static class(package private) static class -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BasicBeanConverter.Builderbuilder()Creates a new builder for configuring a BasicBeanConverter instance.booleancanListify(Object o) Determines if an object can be converted to a list.getNested(Object o, NestedTokenizer.Token token) Extracts a nested property value using structured field access syntax.getProperty(Object object, String name) Accesses a named property or field from an object.<T> TgetSetting(String key, T def) Retrieves a configuration setting value with a fallback default.Converts a collection-like object to a standardized List<Object> format.Converts an object to its string representation for testing purposes.Pre-processes objects before conversion operations.
-
Field Details
-
DEFAULT
-
SETTING_nullValue
- See Also:
-
SETTING_selfValue
- See Also:
-
SETTING_fieldSeparator
- See Also:
-
SETTING_collectionPrefix
- See Also:
-
SETTING_collectionSuffix
- See Also:
-
SETTING_mapPrefix
- See Also:
-
SETTING_mapSuffix
- See Also:
-
SETTING_mapEntrySeparator
- See Also:
-
SETTING_calendarFormat
- See Also:
-
SETTING_classNameFormat
- See Also:
-
-
Constructor Details
-
BasicBeanConverter
-
-
Method Details
-
builder
Creates a new builder for configuring a BasicBeanConverter instance.The builder allows registration of custom stringifiers, listifiers, and swappers, as well as configuration of various formatting settings before building the converter.
- Returns:
- A new Builder instance
-
stringify
Description copied from interface:BeanConverterConverts an object to its string representation for testing purposes.- Specified by:
stringifyin interfaceBeanConverter- Parameters:
o- The object to stringify- Returns:
- The string representation of the object
-
swap
Description copied from interface:BeanConverterPre-processes objects before conversion operations.- Specified by:
swapin interfaceBeanConverter- Parameters:
o- The object to swap- Returns:
- The swapped object, or the original object if no swapping is needed
-
listify
Description copied from interface:BeanConverterConverts a collection-like object to a standardized List<Object> format.- Specified by:
listifyin interfaceBeanConverter- Parameters:
o- The object to convert to a list. Must not be null.- Returns:
- A List containing the elements
-
canListify
Description copied from interface:BeanConverterDetermines if an object can be converted to a list.- Specified by:
canListifyin interfaceBeanConverter- Parameters:
o- The object to test. May be null.- Returns:
- True if the object can be listified, false if null or cannot be listified
-
getProperty
Description copied from interface:BeanConverterAccesses a named property or field from an object.- Specified by:
getPropertyin interfaceBeanConverter- Parameters:
object- The object to access properties fromname- The property/field name to access- Returns:
- The property value
-
getNested
Description copied from interface:BeanConverterExtracts a nested property value using structured field access syntax.- Specified by:
getNestedin interfaceBeanConverter- Parameters:
o- The object to extract nested properties from. May be null.token- The parsed token containing the property access structure. Must not be null.- Returns:
- A formatted string representation of the extracted nested values
-
getSetting
Description copied from interface:BeanConverterRetrieves a configuration setting value with a fallback default.- Specified by:
getSettingin interfaceBeanConverter- Type Parameters:
T- The type of the setting value- Parameters:
key- The setting key to retrievedef- The value to return if the setting is not found- Returns:
- The setting value if found, otherwise the default value
-