Table of Contents generated with DocToc
A simple tuples/util library for immutable, type safe tuples, that tries to avoid some common pitfalls and annoyances of similar libraries.
final Tuple3<String, String, String> fullNameTuple1 = Tuples.of("John", "Missing", "Doe");
final Tuple3<String, String, String> fullNameTuple2 = Tuples.of("Jane", "Absent", "Doe");
@Data
public class Person {
public final String PersonId;
public final String Name;
}
@Data
public class PersonTransaction {
public final String Id;
public final Double TransactionAmount;
}
final List<Person> persons = getPersons();
final List<Tuple3<Person, List<PersonTransaction>, Double>> personFinances = persons.stream()
.map(p -> Tuples.of(p, getPersonTransactions(p.PersonId))) // Group together person object and list of transactions.
.map(pt -> Tuples.of(pt.Item1, pt.Item2, pt.Item2.stream().mapToDouble(v -> v.TransactionAmount).sum())) // Group on sum of transactions.
.collect(Collectors.toList());
for(Tuple3<Person, List<PersonTransaction>, Double> personFinance : personFinances) {
System.out.println(personFinance.Item1); // Person Object
System.out.println(personFinance.Item2); // List of person's transactions.
System.out.println(personFinance.Item3); // Precalculated sum of transactions.
}
final Tuple2<String, String> pair = Tuples.of("Mike", "Jones");
final Map<String, Object> pairMap = pair.toMap(); // Unless named, keys become Arity indices.
System.out.println(pairMap.get("First")); // Mike
System.out.println(pairMap.get("Second")); // Jones
- Immutability: Friendly for working in threaded or stream environments by default.
- No dependencies. (except in addon modules where required IE: Jackson)
- Terse: Immutability means tuple fields can be public. Getters exist but are therefore optional.
- Java 8+ streams and lambdas as first class citizens.
- Predictable names: Centralized static utils.
There are several other small (and not so small) tuple libraries for the JVM, but they're solving the problem in a less than optimum way (to our tastes). So the attempt here is to avoid the following
- Having Any Dependencies
- Obtuse or unpredictable names
- Difficult maintenance. Lombok is used to keep code short.
- Being tied to an outer project's version march. (And being deprecated)
- Being too large for the provided functionality.
- Necessitating getters for what should be immutable objects.
- Not being Java first.
- Finish Maven Central deploy pipeline. (in progress)
- Add Java 9 module info files to ensure intended compatibility and restrictions.
- Finish Jackson and Gson custom serializer/deserializers for named tuples.
See LICENSE.md Licensed under WTFNMFPLv3