Skip to: Site menu | Main content


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

Jedi 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, Filter).

Conventional Java

Conventional as in previous example.

Jedi Java

Jedi as in previous example.

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

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

Conventional Java

Conventional as in previous example.

Jedi Java

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

Composeable Functors

Functors can be composed as follows:

Quick Sort

This example uses:

Conventional Java

Jedi Java

Get All Non-Empty Strings From a Collection

Conventional Java

This example uses:

Jedi Java


(see Options)

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

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:

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:

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

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:

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:

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

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

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.