Tag: Denove

join(collection).with(element)

Posted by – November 29, 2009

While playing around with the Denove Core library I often wrote something like this:

public boolean addElement(final T element) {
  if (!this.collection.contains(element)) {
    return this.collection.add(checkNotNull(element, "element")); //$NON-NLS-1$
  }

  throw new IllegalArgumentException("May not add duplicate elements"); //$NON-NLS-1$
}

So I’m not directly exposing the underlying collection but rather offer addElement, addAllElements, removeElement, etc. methods to work with the collection from the outside. This is done so that no one can insert or remove anything without the objects (that is the collection holder) knowledge. In the above example I’m restricting new elements to be non-null and any new element must not be in the collection already. Since this code looks more or less the same for every collection I’m using I thought about how to apply the DRY principle and move that piece of code somewhere else (that is away from the addElement, etc. methods).

At first I thought aspect oriented programming (always wanted to try AspectJ) but ditched it later on for something simpler. My final solution introduces a single static factory method which creates a so called CollectionJoiner which offers its users a DSL-like language to combine/merge/join collections with single elements are other collections. The code below was the result after my first iteration:

public boolean addElement(final T element) {
  join(this.collection).with(element);
}

This was is fairly simple because all it takes is to move the common code from all those addElement-methods to a central place (I’ve used static imports here to make the line even shorter). However I applied the same restrictions to every collection I was going to join so others may not be able to work with this code (e.g. all those who want to allow null-able objects). For that I came up with a new method called restrictedOn() which takes a so-called Restriction as parameter and applies all given restrictions to the element (or all elements of the given collection) to join. It looks like the following:

public boolean addElement(final T element) {
  join(this.collection)
     .restrictedOn(Restrictions.NO_DUPLICATES)
     .restrictedOn(Restrictions.NON_NULL_VALUES)
     .with(element);
}

That way I can apply different restrictions to different collections but I’m still able to define those restrictions only once and use them as often as I want to. The code for the final with() method looks quite simple as well:

public boolean with(final T element) {
  for (final Restriction<T> restriction : this.restrictions) {
    restriction.apply(element);
  }

  return this.collection.add(element);
}

Currently I’m looking into google-collections (and possible google-guava) to see whether the Restriction interface can be replaced with Predicates. I’m already using them with those checkNotNull-methods (see first example for an example..) and would be happy to integrate them into the CollectionJoiner class. Maybe I’ll add a method which sets the default restrictions (or predicates) too, so when you never ever want to work with null-objects you don’t have to specify that every time.

Any comments/suggestions on this?

Denove 9.01 (Malabo)

Posted by – January 20, 2009

Today is not only the first day of the first black american president but also the release date of Denove 9.01 (a.k.a ‘Malabo‘). The release is by far not feature complete and we still have lots to do. So think of this release as some sort of “show-off”..

As always everything can be found at http://denove.shoss.de/. And as you may see there the DenoveSE project didn’t quite make it so we are postponing that release to somewhere in june/july.

Denove 8.10

Posted by – October 23, 2008

Endlich da und noch immer nicht fertig: Mein Vokabel-Trainer. Zu finden ist das Ganze unter hier.