KλudJe

About

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.

Download

Binaries are published to the Central Repository.

Maven dependecy:

    <dependency>
        <groupId>uk.kludje</groupId>
        <artifactId>kludje-core</artifactId>
        <version>0.6</version>
    </dependency>

kludje-core-0.6.jar

Sources are available via the KλudJe project page.

API

Latest Javadoc: KλudJe 0.6 API.

Examples

Simple equals, hashCode and toString

The Meta type can be used to define simple implementations of three basic Java methods:

loading...

Significant properties can be defined a single time.

See the API documentation for more on usage and limitations.

Checked Exception Propagation

This code demonstrates how to throw and catch the checked IOException without using a throws declaration in a method:

loading...

With more than one checked exception type:

loading...

The next example demonstrates how this is useful.

Line Count Example

Consider this interface contract that takes a collection of files and returns the number of lines in them:

loading...

Here is a traditional imperative implementation:

loading...

Let's refactor to a parallel streaming implementation:

loading...

The above code is unsatisfactory. In order to obey the interface contract IOException must be wrapped and unwrapped using UncheckedIOException.

This problem can be addressed using a cast to KλudJe's UFunction to transparently pass the exception to the calling method:

loading...

UFunction extends java.util.function.Function. KλudJe provides types that extend many of the standard Java functional interfaces in the same way. See the API for more details.

The static UFunction.asUFunction method can be used for type inference to avoid having to specify the generic types:

loading...

Checked Exception Handling With Arbitrary Functional Interfaces

The previous example assumes the use of the standard Function interface. 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:

loading...

Explanation:

Here Be Dragons

When using this API there are a few things to be aware of.

When using 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.