Some utility classes around java records
-
MapTrait
Transform any record to a
java.util.Map
just by implementing the interfaceMapTrait
record Person(String name, int age) implements MapTrait {} ... Map<String, Object> map = new Person("Bob", 42);
-
WithTrait
Implementing the interface
WithTrait
adds several methodswith
that allow to duplicate a record instance and update several record components in the processrecord Person(String name, int age) implements WithTrait<Person> {} ... var bob = new Person("Bob", 42); var ana = bob.with("name", "Ana");
-
Wither
A very fast but more cumbersome way to duplicate/update a record instance
record Person(String name, int age) {} ... private static final Wither<Person> wither = Wither.of(MethodHandles.lookup(), Person.class); ... var bob = new Person("Bob", 42); var ana = wither.with(bob, "name", "Ana");
-
JSONTrait
Implementing the interface
JSONTrait
adds two methodstoJSON
andtoHumanReadableJSON
that enable to output a record instance using the JSON formatrecord Person(String name, int age) implements JSONTrait { } ... var person = new Person("Bob", 42); System.out.println(person.toHumanReadableJSON());
JSONTrait
also defines a methodparse(reader, recordType)
to decode a JSON file to a record instancevar reader = ... // a FileReader or a StringReader var person = JSONTrait.parse(reader, Person.class);
mvn package