A Programmer's Blog

HBase Split Visualization – Introducing Hannibal!

Nov
07

As Christian wrote a few days ago at the Sentric Blog, we have created a new opensource tool, called Hannibal which is a tool to help monitor and maintain HBase-Clusters that are configured for manual splitting.

I recently created a video tutorial the tool:

Let me hear what you think.

neo4j + scala = fun

Jul
10

I had a great time at the LCGTUG Hackaton yesterday.

We played around with Neo4J and Scala. The result is pretty useless: a shortest-path calculator using the Dijsktra algorithm. But I think everyone learned something new about Neo4j, Scala or Dijsktra 🙂

Thank you LCGTUG for organizing the event.

Scala and the Observer Pattern

Jun
17

I just started coding my first scala snippets. I played a bit around with lwjgl library to create some random graphic stuff.

The first problem I faced was to find a generic implementation to dispatch events such as render- or input-events from one place and handle them in another place. In OO-Languages this problem is usually solved using the observer pattern.

After studying some existing scala solutions,  I did not find the one that fits my coding style. Since I come from the Flash-world, I would really appreciate a solution where observers are just callback-functions instead of implementations of traits. I also don’t like to have my observables cluttered with code for dispatching the events to the observers.

My Solution consists of a single generic class, the “Event”:

class Event[T] {
 private var listeners: List[T] = List();

 def apply(listener: T): Unit = register(listener);
 def register(listener : T) : Unit = listeners ::= listener;
 def unregisterAll() : Unit = listeners = List();
 def fire(f : (T) => Unit) = {
   for(l <- listeners) {
     f(l)
   }
 }
}

All an observable has to do now is to create one field for each event-type they offer and to call fire() on them. The following example defines one event that gets fired as soon as the position of a DisplayObject changes. The callback-method takes the coordinates as paramters containing the new values.

class DisplayObject {

 val onPositionChangeEvent = new Event[(Int, Int) => Unit];

 private var x:Int = 0;
 private var y:Int = 0;

 def changePosition(x:Int, y:Int) = {
   this.x = x;
   this.y = y;
   onPositionChangeEvent.fire(_(x, y));
 }
}

Now we can register a callback-function directly at the onPositionChangeEvent-field. In the following example we print the new value to the console.

var observable = new DisplayObject();
observable.onPositionChangeEvent((x, y) => println(x + ":" + y));
observable.changePosition(10, 15);

One problem I faced is that I could not find any possibility to reliably unregister from an event. So I decided to implement an unsubscribeAll method.

observable.onPositionChangeEvent.unregisterAll();

If someone has an idea how this could be improved, please leave me a comment.