kjerk / Immutuple

Immutable, typed tuples up to an arity of 16, with modern nomenclature utilities for instantiation and collection conversion. A simple library for a simple concept. Optional plugins for Jackson and Gson named tuples.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

Table of Contents generated with DocToc

Immutuple

A simple tuples/util library for immutable, type safe tuples, that tries to avoid some common pitfalls and annoyances of similar libraries.

tl;dr Usage

Simple Instantiation

final Tuple3<String, String, String> fullNameTuple1 = Tuples.of("John", "Missing", "Doe");
final Tuple3<String, String, String> fullNameTuple2 = Tuples.of("Jane", "Absent",  "Doe"); 

Grouping / Streaming

@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.
}

Conversion

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

Goals

  • 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.

Why Another Tuples Library?

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

Todo

  • 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.

License

See LICENSE.md Licensed under WTFNMFPLv3

About

Immutable, typed tuples up to an arity of 16, with modern nomenclature utilities for instantiation and collection conversion. A simple library for a simple concept. Optional plugins for Jackson and Gson named tuples.

License:Other


Languages

Language:Java 100.0%