projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.81k stars 2.37k forks source link

[FEATURE] Add a suffix parameter to @Delegate #2763

Open CC007 opened 3 years ago

CC007 commented 3 years ago

Description At the moment you can use a delegate like this

interface CollectionAdders {
    boolean add(Object e);
    boolean addAll(Collection<? extends Object> c);
}
class A {
    @Delegate{types=CollectionAdders.class)
    Set<Object> objects;
}

This is nice and all, but if you have 2 Sets in class A, then you can't use it for both.

That is why I propose adding a suffix parameter to the @Delegate annotation (and optionally add a @Plural annotation for cases where this suffix needs to be pluralized).

interface Adders {
    boolean add(Object e);

    @Plural
    boolean addAll(Collection<? extends Object> c);
}
class A {
    @Delegate{types=Adders.class, suffix="cat")
    Set<Object> cats;

    @Delegate{types=Adders.class, suffix="dog")
    Set<Object> dogs;
}

Or (without @Plural):

interface Adder {
    boolean add(Object e);
}
interface AllAdder {
    boolean addAll(Collection<? extends Object> c);
}
class A {
    @Delegate{types=Adder.class, suffix="cat")
    @Delegate{types=AllAdder.class, suffix="cats")
    Set<Object> cats;

    @Delegate{types=Adder.class, suffix="dog")
    @Delegate{types=AllAdder.class, suffix="dogs")
    Set<Object> dogs;
}

This would generate the A.addCat(...), A.addDog(...), A.addAllCats(...) and A.addAllDogs(...)

Describe the target audience While I am using a Set as an example, this could be widely applicable for delegating methods from more than one field. The example of a Set was purely used to also explain the @Plural concept.

Additional context To implement the @Plural functionality, it might be possible to reuse parts of the code that is used for creating a singular form for the @Signular annotation.

This suffix feature has been mentioned by me before in #1905, but I felt that this feature was more generally applicable and deserved a feature request of its own.

Lilianne-Blaze commented 9 months ago

It would be nice to have both suffix and prefix params