ossgang / ossgang-commons

Apache License 2.0
4 stars 0 forks source link

Preinit tostring listelements #55

Closed kaifox closed 4 years ago

kaifox commented 4 years ago

Dear all,

these are some improvements for the Mapbackeds, which would help us in FESA testing at GSI.

The main changes are:

@ToString annotation

As it is not allowed to implement any method of Object in an interface (as toString), this is an attempt to provide custom toString methods for mapbacked objects: This annotation can be put on one of the interface methods (field method or default method). If this is present, then this method will be used as toString method. If none is present the fallback is as the old version: printing out the interface name and the map content.

init values for the builder

Very often it is useful to start off a builder with some print fields. These can come from a simple map of from another mapbacked object (imagine, doing a get on a fesa property, changing one value and doing a set again) Still changing a value of a field is only allowed once per builder instance. e.g.:

Shortcuts to changing elements of lists

In a similar scenario as above, it is useful to change e.g. one element of a list (or a few). This now can be done through the new element() method in the builder. E.g.

builder(AnInterface.class, oldObject).element(AnInterface::stringList, 2, "hello").build(); 

Any feedback welcome

kaifox commented 4 years ago

Hi @michi42 , I just realized the List.of() problem ...

michi42 commented 4 years ago

A more general comment - more an idea - for the builder ...

Wouldn't it be simpler to just allow setting any field any number of times? Perhaps I'm a bit biased by what IntelliJ and Immutables generate ... but this is the behaviour I would naively expect. Then you could just have a from(T other) method on the builder to initialize all fields from an existing Mapbacked, e.g.

builder(AnInterface.class).from(oldObject)
   .element(AnInterface::someField, "hello")
   .element(AnInterface::anotherField, 42)
.build(); 

This would also allow storing and re-using the builder to build multiple similar Mapbackeds where e.g. 1 field is different.

Of course this could also be a second step later ...

kaifox commented 4 years ago

Hi @michi I also implemented the suggested changes for the builer. It is now allowed to override a field as often as you want. Further the from() method is now part of the builder (and not of the builder constructor) anymore..

michi42 commented 4 years ago

Perfect :-)

kaifox commented 4 years ago

Concerning the ToString: It is even a bit more annoying ... For java 8 one needs an ugly hack, and the one I found for the moment seems not to support polymorphism ... so this means that 'Overriding' of methods annotated with @ToString are not supported...

I will still merge now... I hope that this is not critical for the current usage ....