Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuple convertor methods #17

Merged
merged 8 commits into from
Sep 14, 2020
Merged

Tuple convertor methods #17

merged 8 commits into from
Sep 14, 2020

Conversation

runeflobakk
Copy link
Member

@runeflobakk runeflobakk commented Sep 9, 2020

New feature:

Add facility to convert tuples (triples, quadruples, etc) to arbitrary instances using either method references (typically a constructor A::new) having the same parameter type signature as the parameterized types of the tuple, or a lambda. Using a lambda enables arbitrary naming when referring to the constituents of the tuple, and a method reference offers a succinct conversion without needing to refer to internals of the tuple at all.

Tuple tuple = Tuple.of("x", 1);

A a1 = tuple.to((text, num) -> new A(text, num));
// or:
A a2 = tuple.to(A::new);

// without convertor method:
A a3 = new A(tuple.first(), tuple.second())

As usual, the use of tuples should only be used by particularly general code. Most often a custom class with fields offering more semantics of the values is in order.

To only view these changes, look at 921a2de

Additional changes

All of the .java sources are touched because of a change in the license-maven-plugin, and can basically be ignored, as they all have this as their only diff:

- /**
+ /*

To get a head-start with the review, paste this script into devtools console, hit Enter, and it will mark all this noise as "viewed" and collapse these files.

['no/digipost/Digg', 'no/digipost/collection', 'no/digipost/concurrent', 'no/digipost/function', 'no/digipost/io', 'no/digipost/jdbc', 'no/digipost/text', 'no/digipost/time', 'tuple/ViewableAs', 'no/digipost/util','/TupleTest','/TripleTest','/QuadrupleTest','/PentupleTest','/HextupleTest','/SeptupleTest','/OctupleTest','/NonupleTest','/DecupleTest'].forEach(fileName => document.querySelectorAll("div[data-path*='" + fileName + "'] input.js-reviewed-checkbox").forEach(viewedCheckbox => viewedCheckbox.click()))

A tuple, e.g. a 2-ary Tuple, can be converterted to a an arbitrary
instance using tuple.to((v1, v2) -> new Something(v1, v2), or more
succinct: tuple.to(Something::new). This is a convenience API which
eliminates the need to "manually extract" each value of the tuple, and
instead either freely name the values with a lambda, or directly have
the values passed to a method reference with the equivalent parameter
signature as the parameterized types of the tuple.
Changes in license headers in .java files is because mathieucarbou/license-maven-plugin#118
It's is not available to resolve previous version itself when we use
non-numeric LOCAL-SNAPSHOT for building

Run with:
mvn japicmp:cmp -Djapicmp.oldversion=<last released version>
@runeflobakk runeflobakk marked this pull request as ready for review September 9, 2020 23:35
import java.util.List;
import java.util.UUID;

final class Compound {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a dummy class used to test converting from XTuple to an arbitrary type.

@eivinhb
Copy link
Member

eivinhb commented Sep 10, 2020

Kult det console-scriptet! :D

@runeflobakk
Copy link
Member Author

Jepp, det er aktuelt i noen sammenhenger 😅

@eivinhb
Copy link
Member

eivinhb commented Sep 10, 2020

I Digg this. 🥇

Copy link

@johnksv johnksv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@runeflobakk runeflobakk changed the base branch from master to main September 14, 2020 11:52
@runeflobakk runeflobakk merged commit 1261b29 into main Sep 14, 2020
@runeflobakk runeflobakk deleted the tuple-convertor-methods branch September 14, 2020 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants