Intuitive generics based functional interfaces with support for void, returns, multiple params and exceptions.
- Zero. JDK8 compatible. Tested on JDK15.
Maven dependencies available on maven central search.maven.org
<dependency>
<groupId>com.momomo</groupId>
<artifactId>momomo.com.platform.Lambda</artifactId>
<version>5.0.2</version>
</dependency>
<repository>
<id>maven-central</id>
<url>http://repo1.maven.org/maven2</url>
</repository>
Our other, highlighted repositories
-
momomo.com.platform.Core
Is essentially what makes the our the core of several of momomo.com's public releases and contains a bunch of Java utility. -
momomo.com.platform.Obj
Intuitive library that makes it easier for you to return multiple, fully defined objects on the fly from any method, any time rather than being limited to the default maximum of one. -
momomo.com.platform.Nanotime
Allows for nanosecond time resolution when asking for time from Java Runtime in contrast withSystem.currentTimeMillis()
. -
momomo.com.platform.DB.transactional.Hibernate
A library to execute database commands in transactions without having to use annotations based on Hibernate libraries. No Spring! -
momomo.com.platform.DB.transactional.Spring
A library to execute database commands in transactions without having to use annotations based on Spring libraries.
Ever confused by names like Supplier
, Function
, BiFunction
, Consumer
, ... and so forth? Do you find them limiting?
Are you easily able to keep track of them? Can your brain parse them quickly enough when scanning method signatures?
Can you easily convert and/or go from one method to another?
With this intuitive interface
based naming strategy you can use:
Lambda.R
for areturn
lambda with definedreturn
type that takes0
defined parameters. ImplementsSupplier<Return>
.Lambda.R1
for areturn
lambda with definedreturn
type that takes1
defined parameter. ImplementsFunction<Param1, Return>
.Lambda.R2
for areturn
lambda with definedreturn
type that takes2
defined parameter. ImplementsBiFunction<Param1, Param2, Return>
.Lambda.R2E
for areturn
lambda with definedreturn
type that takes2
defined parameters, and allows you to define the thrown exception type as well.- ...
Lambda.V
for avoid
lambda that takes0
defined parameters. ImplementsRunnable
.Lambda.V1
for avoid
lambda that takes1
defined parameter. ImplementsConsumer<Param1>
.Lambda.V2
for avoid
lambda that takes2
defined parameters. ImplementsBiConsumer<Param1>
.Lambda.V2E
for avoid
lambda that takes2
defined parameters, and allows you to define the thrown exception type as well.- ...
- Also take a look at less common use
varargs
based
Lambda.RP
,Lambda.RPE
,Lambda.RO
,Lambda.ROE
,Lambda.OO
,Lambda.OOE
Lambda.VP
,Lambda.VPE
,Lambda.VO
,Lambda.VOE
which are commented in the source code ofLambda.java
.
With this library you can also safely:
- Go from
Lambda.R1E
to aLambda.V1E
by simply callinglambda.V1E()
where we ignore making use of thereturn
value for the desiredvoid
lambda. - Go from
Lambda.V1E
to aLambda.R1E
by also simply callinglambda.R1E()
but comes with a small caveat where wereturn null
instead which is fine as long as the invoker of thelambda.call()
handlesreturn null
properly. - Go from
R
toR1
,R1E
,R2
,R2E
,R3
,R3E
,R4
,R4E
,R5
,R5E
by simply calling the method with the same name, such aslambda.R4E()
. - ...
Documentation is provided through comments within the class itself and we recommend you start try to use it, and you will immediately figure out its use.
-
Nice compact, partial view over signatures from Intellij over
Lambda.java
:
-
Since a
Lambda.V extends Lambda.VE<RuntimeException>
,Lambda.R extends Lambda.RE<RuntimeException>
,Lambda.V1<P1> extends Lambda.V1E<P1, RuntimeException>
we recommend that you always try to use athrow
capable Lambda for your declared methods which ultimately depends on intetions like if you wantlambda.call()
to bubble nonRuntimeExceptions
or if you want to force the handlement ofexception
inside the lambda body (thinkRunnable
andThread
)? This way, you have the option to handle any exception in the Lambda body, as well as being able to simply throw it.Take the the
Runnable
interface as an example where you normally, if you say read from a file which would throw anIOException
by default you would be forced to handle that exception. However, using theLambda
interface, and calling a method that expects a throw capableLambda
, that lambda will automatically throwE
if there are any by the caller oflambda.call()
. -
For your base methods, try to use an
R
lambda withE
whenever possible, such asLambda.R[1-5]E
. This is versitale and would allow you to add delegating methods of most flavours more easily.
-
A
Lambda.R
can automatically and safely be converted to to anyLambda.R1
-Lambda.R5
as well asLambda.V
-Lambda.V5
and there are util methods for these things. ALambda.R5
can not be safely converted to aLambda.R4
since the body inLambda.R5
expects 5 params and converting it toLambda.R4
would leave at least onenull
.
To make conversions fromLambda.R5
toLambda.R4
orLambda.V4
you would have to do it manually.On a similar note,
Lambda.V1
can be converted to aLambda.R1
and there are util methods for that. This is achieved by simply returning null, which is always possible even when supplying aLambda.R1
to begin with. Therefore thelambda.call()
should handlereturn null
. -
Whenever possible, the interfaces implement equivalent interfaces such as
Runnable
,Consumer
,BiConsumer
,Function
,BiFunction
,Supplier
and can thus be used and fed to methods expecting those allowing you to stick with using this consistent naming pattern.As an example,
Lambda.V
extends
Runnable
withrun()
implemented to delegate tocall()
,Lambda.R<R>
extends
Supplier<R>
withget()
implemented to delegate toreturn call()
,Lambda.R1<R,P>
extends
Function<P, R>
withapply(P param)
implemented to delegate toreturn call(param)
and so forth. This means you can opt to supply aLambda.V
to anything that accepts aRunnable
,Lambda.R
to anything that accepts aSupplier
making it possible to use as a standard for everything. -
We have two namespaces,
Lambda
andClosure
and can be used interoperable. The default namespace isLambda
so you could doLambda.V1<String>
, orLambda.R1<Boolean, String>
. But since the Lambda namespace is sometimes difficult to complete (intellij) due to its wide use, you could instead doClosure.V1
.
Closure.V1
==Lambda.V1
,Closure.R2E
is the same asLambda.R2E
, it just access to it is provided though another class.
The full license can be found here MoL9
Send an email to opensource{at}momomo.com
if you would like to contribute in any way, make changes or otherwise have thoughts and/or ideas on things to improve.
-
Add
E[2-5]
as well to allow for more defined exception types, in cases where multiple exception types are thrown fromLambda
. Normally this would be handled by throwing the least commonException
denominator but this is not always ideal to do. -
Add
V[6-10]
,VE[6-10]
,R[6-10]
,RE[6-10]
as well to cover all sceptics out there thinking they will ever need more than5
.