Immutables library builder collection hint

77 views Asked by At

I'd like to use the Immutables java library to generate my pojo. The desire I have though is to use a different collection other that ArrayList or LinkedHashMap in the generated builder. Say for instance I have MyMap<K,V> extends LinkedHasMap<K,V> Id' like to be able to hint at the generator to use MyMap in the generated builder instead of the LinkedHashMap that the generated code would use. I've looked at several options including [encoding custom types][1] which seems very complex for what I'm trying to do, Is there a way to defaine @Value.Default or use the style annotation or some other mechanism to be able to specify the type of collection to use within the generated builder?

EDIT/Clarification:

If I have MyMap defined, I'd like to be able to define MyPojo in terms of the Map interface, but add some form of instruction to Immutables to use MyMap rather than LinkedHashMap in the builder. Once the object is instantiated it wont matter that the Map used by the builder was specialised as the values in the map will exist regardless of the backing map.

public class MyMap<K,V> extends LinkedHashMap<K,V> {
// Specialisations...
}

@Value.Immutable
public interface MyPojo {
    // Use MyMap<String, String> in builder Annotation/Hint?
    Map<String, String> myMap(); 
    String usage();
}

The subsequent generated code/builder that is generated would look like this:

@Generated(from = "MyPojo", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
public final class ImmutableMyPojo
    extends MyPojo {
  private final Map<String, String> myMap;

...

 /**
   * Builds instances of type {@link ImmutableMyPojo ImmutableMyPojo}.
   * Initialize attributes and then invoke the {@link #build()} method to create an
   * immutable instance.
   * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
   * but instead used immediately to create instances.</em>
   */
  @Generated(from = "MyPojo", generator = "Immutables")
  @NotThreadSafe
  public static final class Builder {
    private static final long INIT_BIT_ERRORS = 0x1L;
    private static final long OPT_BIT_PARAMETERS = 0x1L;
    private long initBits = 0x1L;
    private long optBits;

    
    private Map<String, String> myMap = new MyMap<String, String>();

The last line currently always generates the following which is not what I'd like:

 private Map<String, String> myMap = new LinkedHashMap<String, String>();
  [1]: https://immutables.github.io/encoding.html
0

There are 0 answers