Posts Tagged ‘ckkl-core’

ToString, circular references and Hibernate

Monday, November 19th, 2007

I have been writing some Hibernate code and in order to check what is going on with the mappings and the data they actually retrieve, all my domain objects implement org.ckkloverdos.string.IToStringAware of ckkl-core.

The actual domain model does not matter for this post, but let’s say that two of my classes are Person and Address and the association is such that a Person has one or more Addresses (under the hoods there are 3 tables, 2 for the entities and one join table). Also, an Address object has a reference to the Person it belongs to.

Here is a printout an Addresses list:

ArrayList[
  0=Address(
    name="Some address 1"
    person=null
    addressId=4)
  1=Address(
    name="Some address 2"
    person=null
    addressId=3)
  2=<REF:1>Address(
    name="The Master Address"
    person=Person$$EnhancerByCGLIB$$e8f89ce1(
      addresses=PersistentSet[0=<@REF:1>]
      name="The Master Person"
      personId=1)
    addressId=1)
...
]

So, actually, I can verify that I have one Person (”The Master Person”) to which one Address (”The Master Address”) belongs to. Notice how this Address lists its Person, which in turn has a circular reference to the particular Address.

Do not get confused by the domain object model. It actually does not matter. The idea here is that ToString can correctly report a circular reference in my object graph. It can also index those circular references in order of appearance, so that you will see <REF:1>, <REF:2> and so on, and of course the respective back-references <@REF:1>, <@REF:2> and so on.

<REF:n> marks a reference and <@REF:n> designates a circular (back) reference to the point where <REF:n> appeared. So, at the example above, we mark the particular Address having index 2 in the ArrayList because its object is also used in one of its properties, the Person.

One little bonus, as a side-effect of using the ToString mini-framework, is that I can see what Hibernate does under the hood, by noticing the name of the class

Person$$EnhancerByCGLIB$$e8f89ce1

Well, that’s nice!

Declarative programming with L

Monday, November 12th, 2007

One of the reasons I created org.ckkloverdos.collection.L, is to be able to adopt a more declarative style of programming, since this collection/array wrapper class makes it easy to do some functional-oriented programming.

The other day, I was playing around with the API of Package. At one point, I just wanted to see the loaded packages from my own ckkl-core library. The implementation, using L, is both simple and I believe elegant:

new L(Package.getPackages())
  .selectProperty("name")
  .filterStartsWith("org.ckkloverdos")
  .print();

What would be the alternative in plain-old-Java?

Package[] packages = Package.getPackages();
for(int i = 0; i < packages.length; i++)
{
  String name = packages[i].getName();
  if(name.startsWith("org.ckkloverdos"))
  {
    System.out.println(name);
  }
}

Moving ckkl-core to google

Sunday, November 11th, 2007

I have decided to host ckkl-core to google now. Probably I will keep posting the releases to sourceforge as well but I have not definitely made my mind on that yet. Anyway, the new page is here. The latest release is 0.3.1.

On Open Source Licenses [ckkl-core]

Thursday, October 18th, 2007

The other day, I started releasing my core Java development library, under the name ckkl-core, as an open source project. For that I had to decide on a license and so I asked for the opinions of friends and the open source community in Greece. Combining these with my own findings, here is a list with helpful URLs:

David Wheeler has prepared a nice slide depicting the relationship between several open source licenses. His opinion is that one should go for a GPL-compatible license.

The Software Freedom Law Center, suggest the way to Maintaining Permissive-Licensed Files in a GPL-Licensed Project.

This article (with a second part) explains how to pick an open source license.

You can also try the License Wizard. The first page user interface is a little awkward. Choose Yes, or go directly to the second page :-)

Also, take a look at this quick reference. I find of particular importance their comment:

The most common misunderstanding about software licences is that giving someone else a copy of your source code under a licence restricts what you are allowed to do with your source code. The truth is, if you write some code, and give it to someone else under the terms of Licence X, or publish it so that anyone may use it under the terms of Licence X, that this does not subject you to the terms of Licence X! You are the author of the code, and you hold the copyright, and giving someone permission to use the code does not restrict you to using the code in only the way that they are allowed to use it!

And, of course, you should read what GNU has to say.

By the way, I have chosen the Apache License Version 2.0 for ckkl-core which is:

a collection of Java classes developed for personal projects and research. Its implemented features include but are not limited to Java type handling with aliases and equality testing, easy discovery and accessors for JavaBeans properties, functional-oriented collections (list with map(), filter(), and others), and an API to support an easy toString() implementation even for the most complex cases.

Over the years I have developed a library of, let’s say, personal taste; a library which is my swiss army knife when looking into some new (research) idea. Something I use in order not to re-invent the wheel every time. The source code is currently heavily undocumented and I will be releasing new versions as soon as I can put it into some “eye-friendly” shape (javadocs included). I will do my best on that, time permitting. So, stay tuned !