I am trying to polish my data-mapping library and struggle with my fluent API design as it feels clumsy in some basic use-cases.

The library focuses on defining mapping objects from one type into another in a fluent way, even for immutable objects. The immutable API works as follows: Every constructor parameter has its mapping process defined in declaration order. Said mapping process does not need to start from the current constructor parameters type which leads to the following code (excerpt from here), there are more test-cases that show said problem there) :

public class Item {
    private final String id;

    public Item(String id) {
        this.id = id;

    public String getId() {
        return id;

//simplified example: the main use case of the library is to map different types to another
Mapper<Item, Item> mapper = new ConstructorBuilder1<Item, String, Item>(Item::new)

As you can see even though I have already reached the desired constructor parameters type I still have to call the terminal operation mapTo(Function.identity()). Imagine another case where the .from calls getter return type is mismatching from the get go.

Is there another, better or more clear approach to avoid this? I want to avoid the unnecessary mapTo-call if possible. Other approaches that circumvent the outlined problem are welcome too.

I already tried overloading which doesn't work well with lambdas and I have documented several other (subjectively less fitting) approaches here.

This and its .immutable-functions serve as the main entry point into the immutable API.

This should give a more broad overview of the libraries usage.

Please let me know if you need any more information.

  • What is your desired goal? You don't want to have to call the terminal .mapTo function at the end? – Robert Harvey May 17 at 22:55
  • That would be for the best yes. I tried like 5 different approaches that didn't work. I am open for different approaches altogether though. Will edit the post. – roookeee May 17 at 22:55
  • Why don't you just add the mapTo functionality to your build() function or make mapTo the last thing you call and skip the build() call? – Robert Harvey May 17 at 22:59
  • That would work in the single constructor parameter case, I am supporting up to 8 constructor parameters for now so this is not an option. The main headache I have is that I have to to this unnecessary Function.identity() mapping even though the types fit :/ Javas type system is in my way here but I don't want to lose compile time correctness. – roookeee May 17 at 23:00
  • I don't understand why you think that. You would simply add an additional fluent call for each parameter. – Robert Harvey May 17 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.