Class BasicBeanConverter.Builder
- Enclosing class:
- BasicBeanConverter
This builder provides a fluent API for configuring custom type handlers, settings, and property extraction logic. The builder supports registration of four main types of customizations:
Type Handlers:
addStringifier(Class, Stringifier)
- Custom string conversion logicaddListifier(Class, Listifier)
- Custom list conversion for collection-like objectsaddSwapper(Class, Swapper)
- Pre-processing and object transformationaddPropertyExtractor(PropertyExtractor)
- Custom property access strategies
PropertyExtractors:
Property extractors define how the converter accesses object properties during nested
field access (e.g., "user.address.city"
). The converter uses a chain-of-responsibility
pattern, trying each registered extractor until one succeeds:
PropertyExtractors.ObjectPropertyExtractor
- JavaBean-style properties via reflectionPropertyExtractors.ListPropertyExtractor
- Numeric indices and size/length for arrays/collectionsPropertyExtractors.MapPropertyExtractor
- Key-based access for Map objects
Custom extractors can be added to handle specialized property access patterns:
Default Configuration:
The defaultSettings()
method pre-registers comprehensive type handlers
and property extractors for common Java types, providing out-of-the-box functionality
for most use cases while still allowing full customization.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddListifier
(Class<T> c, Listifier<T> l) Registers a custom listifier for a specific type.Registers a custom property extractor for specialized property access logic.addSetting
(String key, Object value) Adds a configuration setting to the converter.addStringifier
(Class<T> c, Stringifier<T> s) Registers a custom stringifier for a specific type.addSwapper
(Class<T> c, Swapper<T> s) Registers a custom swapper for a specific type.build()
Builds the configured BasicBeanConverter instance.Adds default handlers and settings for common Java types.
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
addSetting
Adds a configuration setting to the converter.- Parameters:
key
- The setting key (use SETTING_* constants)value
- The setting value- Returns:
- This builder for method chaining
-
addStringifier
Registers a custom stringifier for a specific type.Stringifiers convert objects to their string representations. The BiFunction receives the object to convert and the converter instance for recursive calls.
- Type Parameters:
T
- The type to handle- Parameters:
c
- The class to register the stringifier fors
- The stringification function- Returns:
- This builder for method chaining
-
addListifier
Registers a custom listifier for a specific type.Listifiers convert collection-like objects to List<Object>. The BiFunction receives the object to convert and the converter instance for recursive calls.
- Type Parameters:
T
- The type to handle- Parameters:
c
- The class to register the listifier forl
- The listification function- Returns:
- This builder for method chaining
-
addSwapper
Registers a custom swapper for a specific type.Swappers pre-process objects before conversion. Common uses include unwrapping Optional values, calling Supplier methods, or extracting values from wrapper objects.
- Type Parameters:
T
- The type to handle- Parameters:
c
- The class to register the swapper fors
- The swapping function- Returns:
- This builder for method chaining
-
addPropertyExtractor
Registers a custom property extractor for specialized property access logic.Property extractors enable custom property access patterns beyond standard JavaBean conventions. The converter tries extractors in registration order until one returns a non-
null value. This allows for:- Custom data structures: Special property access for non-standard objects
- Database entities: Property access via entity-specific methods
- Dynamic properties: Computed or cached property values
- Legacy objects: Bridging older APIs with modern property access
Implementation Example:
// Custom extractor for a specialized data class PropertyExtractorcustomExtractor = (obj ,property ) -> {if (obj instanceof DatabaseEntityentity ) {switch (property ) {case "id" :return entity .getPrimaryKey();case "displayName" :return entity .computeDisplayName();case "metadata" :return entity .getMetadataAsMap(); } }return null ;// Let next extractor try };var converter = BasicBeanConverter.builder () .addPropertyExtractor(customExtractor ) .defaultSettings()// Adds standard extractors .build();Execution Order: Custom extractors are tried before the default extractors added by
defaultSettings()
, allowing overrides of standard behavior.- Parameters:
e
- The property extractor to register- Returns:
- This builder for method chaining
- See Also:
-
defaultSettings
Adds default handlers and settings for common Java types.This method registers comprehensive support for:
- Collections: List, Set, Collection → bracket format
- Maps: Map, Properties → brace format with key=value pairs
- Map Entries: Map.Entry →
"key=value" format - Dates: Date, Calendar → ISO-8601 format
- Files/Streams: File, InputStream, Reader → content extraction
- Reflection: Class, Method, Constructor → readable signatures
- Enums: All enum types → name() format
- Iterables: Iterable, Iterator, Enumeration, Stream → list conversion
- Wrappers: Optional, Supplier → unwrapping/evaluation
Default settings include:
nullValue
="<null>" emptyValue
="<empty>" classNameFormat
="simple"
Note: This should typically be called after custom handlers to avoid overriding your custom configurations, since handlers are processed in reverse order.
- Returns:
- This builder for method chaining
-
build
Builds the configured BasicBeanConverter instance.This method creates a new BasicBeanConverter with all registered handlers and settings. The builder can be reused to create multiple converters with the same configuration.
- Returns:
- A new BasicBeanConverter instance
-