https://github.com/g4s8/tuples
Object-oriented Java tuples
https://github.com/g4s8/tuples
java library pairs tuples utils
Last synced: about 1 year ago
JSON representation
Object-oriented Java tuples
- Host: GitHub
- URL: https://github.com/g4s8/tuples
- Owner: g4s8
- License: mit
- Created: 2021-08-27T07:30:54.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-29T18:16:58.000Z (about 2 years ago)
- Last Synced: 2025-04-09T22:53:03.921Z (about 1 year ago)
- Topics: java, library, pairs, tuples, utils
- Language: Java
- Homepage:
- Size: 68.4 KB
- Stars: 16
- Watchers: 3
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Object oriented Java tuples.
- No data accessors
- No ugly class or method names ~`pair._1()`~, ~`new Tuple3()`~
- All tuple types are interfaces
- Strong encapsulation of tuples data
- Standard tuple implementations are immutable
- Built-in support of `equals`, `hashCode`, `toString`, `Serializable` in standard implementation
[](https://github.com/g4s8/tuples/actions/workflows/ci-checks.yml)
[](https://maven-badges.herokuapp.com/maven-central/wtf.g4s8/tuples)
### Usage
Add dependency
```xml
wtf.g4s8
tuples
```
Using in code:
```java
// creating new tuples
var pair = Pair.of(1, "b"); // <1, "b">
var triplet = Triplet.of(4, false, 0.2); // <4, false, 0.2>
// applying tuple data
String result = pair.apply((i, s) -> String.format("%s - %d", s, i)); // b - 1
// accepting tuple data
triplet.accept((i, b, d) -> System.out.printf("%d - (%b) %f\n", i, b, d)); // 4 - (false) 0.2
// push and pop
Unit iUnit = Unit.of(1); // <1>
Pair pair = unit.push("a"); // <1, "a">
Unit sUnit = pair.apply((i, s) -> Pair.of(s, i)).pop(); // <"a">
Triplet triplet = sUnit.push(1.1).push(true); // <"a", 1.1, true>
// zipping iterables
var zip = Pair.zip(Arrays.asList(1, 2, 3), Arrays.asList("a", "b", "c")); // [<1, "a">, <2, "b">, <3, "c">]
```
### Design
Each tuple object specify the behavior and encapsulates the data.
Tuple primitives are interfaces with just a minimal required methods.
Data is strongly encapsulated and can not be accessed directly.
All types are immutable.
The only was yo access the data is to call `accept` or `apply` methods.
There are three objects exist:
- `Unit` - singleton tuple
- `Pair` - pair of two value
- `Triplet` - three values
### API
Each tuple has two primary methods:
- `apply(function)` - apply function to tuple data, return function result
- `accept(consumer)` - consume tuple data, return nothing
And additional methods to add or remove data:
- `push(item)` - change tuple type by adding new element (unit -> pair -> triplet)
- `pop()` - skip last element, reduce tuple size (triplet -> pair -> unit)
### Constructing
Each tuple type has factory method `of(...)`:
- `Unit.of(T) -> Unit`
- `Pair.of(T, U) -> Pair`
- `Triplet.of(T, U, V) -> Triplet`
### ZIP
Also, there are `zip` methods available to zip two or more lists:
- `Pair.zip(Iterable, Iterable) -> Iterable>`
- `Triplet.zip(Iterable, Iterable, Iterable) -> Iterable>`
### Optional
Unit tuple has `maybe` method to convert from optional:
- `Unit.maybe(Optional) -> Optional>`
### Extras
All standard tuple classes created from `of` factory method
implements `equals`, `hashCode` and `toString` methods.
### Testing
Each tuple type has Hamcrest matcher in `wtf.g4s8.tuples.hm` package
(to use it, add `org.hamcrest:hamcrest` Maven dependency):
- `UnitMatcher` for `Unit`
- `PairMatcher` for `Pair`
- `TripletMatcher` for `Triplet`