Class BasicBeanConverter.Builder

java.lang.Object
org.apache.juneau.junit.bct.BasicBeanConverter.Builder
Enclosing class:
BasicBeanConverter

public static class BasicBeanConverter.Builder extends Object
Builder for creating customized BasicBeanConverter instances.

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:
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:

Custom extractors can be added to handle specialized property access patterns:

var converter = BasicBeanConverter.builder() .defaultSettings() .addPropertyExtractor(new MyCustomExtractor()) .addPropertyExtractor((obj, prop) -> { if (obj instanceof MySpecialType special) { return special.getCustomProperty(prop); } return null; // Try next extractor }) .build();

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 Details

  • 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 for
      s - 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 for
      l - 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 for
      s - 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 PropertyExtractor customExtractor = (obj, property) -> { if (obj instanceof DatabaseEntity entity) { 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