Skip to: Site menu | Main content

Jedi

Decluttering Java

Examples Print

Does A Collection Contain an Item With a Property of a Particular Value?

This example uses FirstOrderLogic.exists(java.util.Collection, Filter).

Conventional Java

Foo.java
SomeClass.java

Jedi Java

Foo.java
SomeClass.java

The getBarEqualsFilter method was generated by the @JediFilter annotation and statically imported.

Select All Items From a Collection Where The Item's Property Matches a Given Value

This example uses FunctionalPrimitives.select(java.util.Collection, Filter).

Conventional Java

Conventional Foo.java as in previous example.

SomeClass.java

Jedi Java

Jedi Foo.java as in previous example.

SomeClass.java

Get The Values of A Property For All Items In A Collection

This example uses FunctionalPrimitives.collect(java.util.Collection, Functor).

Conventional Java

Conventional Foo.java as in previous example.

SomeClass.java

Jedi Java

Foo.java
SomeClass.java

The getBarFunctor method was generated by the @JediFunctor annotation and statically imported.

Composeable Functors

Functors can be composed as follows:

Compose.java

Quick Sort

This example uses:

Conventional Java

Quicksort.java

Jedi Java

QuickSort.java

Get All Non-Empty Strings From a Collection

Conventional Java

This example uses:

SomeClass.java

Jedi Java

SomeClass.java

Options

(see Options)

Everyone has come across nasty code like this in many languages:

SomeClass.java

These situations occur during lazy initialisation, retrieving values from maps, retrieving data from a database. It's everywhere and it's nasty.

The reason it's nasty is that a null reference sits around waiting for some unfortunate code to dereference it. The even more unfortunate programmer has to then figure where it came from which is not always trivial.

This is where Option comes in. Let's rewrite the above code block:

SomeClass.java

The point is that there are no nulls in this code and the possibility that the value may not exist has to be dealt with.

If you don't care about the None case you can use a for loop:

SomeClass.java

If you just want a default value if the Option is a None then:

SomeClass.java

where aFunctor is a Jedi Functor0 that will generate the value you need.

This is JEDI though, so there is also another way to use Options:

SomeClass.java

Where someCommand and noneCommand are Commands to be executed depending on whether foo is a Some or None. (The commands could be generated by annotations against existing methods of course.) The someCommand will be given the actual value contained in the Some instance so it does not need to match on an Option. This can be much more concise with a static import:

SomeClass.java

Another use is performing some action if a value exists. Normally this requires a null check. In Jedi, with Commands and Option:

SomeClass.java

Another is in equal methods that need to check for nulls because the enclosing class hasn't modelled optional fields properly:

SomeClass.java

Project Euler Example

This example illustrates solutions to Project Euler problems using Jedi.

The code was compiled with the jediSuppressProxySuffix switch which is why none of the factory methods have the word 'proxy' in them - see the example build file included in the distribution's examples.

ProjectEuler.java