Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic constructor matching in default method

I have a PersonFactory interface as follows:

@FunctionalInterface
public interface PersonFactory<P extends Person> {
    P create(String firstname, String lastname);

    // Return a person with no args
    default P create() {
        // Is there a way I could make this work?
    }
}

The Person class:

public class Person {
    public String firstname;
    public String lastname;

    public Person() {}

    public Person(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }
}

I want to be able to instantiate my Persons like this:

PersonFactory<Person> personFactory = Person::new;

Person p = personFactory.create(); // does not work
Person p = personFactory.create("firstname", "lastname"); // works

Is there a way I could make the Java compiler automatically choose the right constructor by matching the signature of PersonFactory.create() ?

like image 527
Mohammed Aouf Zouag Avatar asked Dec 13 '15 17:12

Mohammed Aouf Zouag


1 Answers

One way would be to have the following:

default P create() {
    return create(null, null);
}

But I'm not sure that's what you wanted. The problem is that you can't make a method reference refer to 2 different methods (or constructors). In this case, you want Person::new to refer to the constructor taking no parameters and the constructor taking 2 parameters, which is not possible.

When you have:

@FunctionalInterface
public interface PersonFactory<P extends Person> {
    P create(String firstname, String lastname);
}

and use it like

PersonFactory<Person> personFactory = Person::new;
Person p = personFactory.create("firstname", "lastname");

you have to realize that the method-reference Person::new refers to the constructor taking 2 parameters. The next line just invokes it by passing the parameters.

You could also write it more explicitely using a lambda expression:

PersonFactory<Person> personFactory = (s1, s2) -> new Person(s1, s2); // see, we have the 2 Strings here
Person p = personFactory.create("firstname", "lastname");
like image 113
Tunaki Avatar answered Sep 28 '22 08:09

Tunaki