kludge n. Slang
A system, especially a computer system, that is constituted of poorly matched elements or of elements originally intended for other applications.
KλudJe is a Java lambda API for use with Java 8. If you aren't familiar with lambdas see the Java lambda expression tutorial.
KλudJe is published under the Apache 2.0 license.
Binaries are published to the Central Repository.
<dependency> <groupId>uk.kludje</groupId> <artifactId>kludje-core</artifactId> <version>0.6</version> </dependency>
Sources are available via the KλudJe project page.
Latest Javadoc: KλudJe 0.6 API.
Meta type can be used to define simple implementations of
three basic Java methods:
Significant properties can be defined a single time.
See the API documentation for more on usage and limitations.
This code demonstrates how to throw and catch the checked
IOException without using
throws declaration in a method:
With more than one checked exception type:
The next example demonstrates how this is useful.
Consider this interface contract that takes a collection of files and returns the number of lines in them:
Here is a traditional imperative implementation:
Let's refactor to a parallel streaming implementation:
The above code is unsatisfactory. In order to obey the interface contract
IOException must be wrapped and unwrapped using
This problem can be addressed using a cast to KλudJe's
UFunction to transparently pass the exception to the calling method:
KλudJe provides types that extend many of the standard Java functional interfaces in the same way.
See the API for more details.
UFunction.asUFunction method can be used for type inference to avoid having to specify the generic types:
The previous example assumes the use of the standard
However, arbitrary interfaces can be provided using method references.
KλudJe provides a few n-ary interface types to add exception handling to cover cases where the standard API doesn't.
An example of using a method handle that throws a checked exception with an arbitrary 3-argument functional interface:
callmethod takes a functional interface and the method
Foo.execdeclares no exceptions
ArbitraryInterfaces.downloadmethod reference to
asUTriFunction(this::download)to create an arbitrary functional interface instance that rethrows any declared exception
UTriFunction.applymethod reference to
When using this API there are a few things to be aware of.
Exceptions.throwChecked or code that uses it (e.g.
UFunction) you cannot rely on
the compiler to flag uncaught checked exceptions. This will defy the expectations of many Java developers.
It is recommended to document usage and intent.
The n-ary types provided can be used with functional interfaces/method references that use primitives. Autoboxing concerns are left to the developer.