9 Articles


3 years Ago

Java 8

Published by Marco on

 This article discusses and compares the initial version of Java 8 and C# 4.5.1. I have not used Java 8 and I have not tested that any of the examples—Java or C#—even compile, but they should be pretty close to valid.

Java 8 has finally been released and—drum roll, please—it has closures/lambdas, as promised! I would be greeting this as champagne-cork–popping news if I were still a Java programmer.[1] As an ex-Java developer, I greet this news more with an ambivalent shrug than with any... [More]

11 years Ago

Versioned Objects with Hibernate

Published by Marco on

Hibernate is a persistence framework for Java. Among the many perks it purports to bring to the table is automatic versioning for objects in the database. That is, when saving an object to the database, it increments a version number. Any process that attempts to store a different version of the same object is rejected. This is all extremely flexible and can be added to a POJO using an annotation:

  private int version;

Nice … a single annotation takes care of people... [More]

Sorting Generic Collections

Published by Marco on

One of the features we expect from a collections library is sorting. You should be able to use generic library mechanisms to sort a list of any kind of element. Most libraries include a generic sort function, to which a comparison functor (object or function pointer) is passed. This functor is called repeatedly on pairs of elements until the list is sorted.

Let’s define the simple class we’ll use in the ensuing examples.

class A {
  String fileName;
  function getFileName() {
    return... [More]

Wildcard Generics

Published by Marco on

As of version 1.5, Java has blessed its developers with generics, which increase expressiveness through improved static typing. With generics, Java programmers should be able to get away from the “casting orgy” to which Java programming heretofore typically devolved. The implementation in 1.5 does not affect the JVm at all and is restricted to a syntactic sugar wherein the compiler simply performs the casts for you.

Let’s build a class hierarchy and see how much casting Java saves us. Assume... [More]

Inherited Method Annotations

Published by Marco on

See Finding Conforming Methods for part one of this two-part article.

The problem we’re working on is as follows:

  1. Given an object, a method name and a list of parameters, execute the matching method on the given object.
  2. Determine from the object’s class whether the given method can be executed from the given context (web, command-line, etc.)

We will use annotations to mark up methods as callable or not. Given the Method we obtained in part one, it shouldn’t be too hard to find its... [More]

Finding Conforming Methods

Published by Marco on

This is a two part post illustrating some tricks for working with the Java reflection API. Part two is available here.

Java reflection provides a wealth of information about your code. One interesting use of this information is to layer scriptability on top of an application, calling code dynamically. Suppose we wanted to do the following:

  1. Given an object, a method name and a list of parameters, execute the matching method on the given object.
  2. Determine from the object’s class whether the... [More]

Immutable Collections

Published by Marco on

Java supports immutable collections of all kinds, but not in the way you would expect. A naive implementation would declare the immutable (unmodifiable in Java parlance) interface as follows[1]:

interface UnmodifiableList<T> {
  function T get();
  function int size();

There is no way to modify this list—the API is simply not available. That done, we can now create the modifiable version of the list as follows:

interface List<T> extends UnmodifiableList<T> {
  function void add(T);
  function... [More]

Investigating Cayenne

Published by Marco on

Cayenne has nice-looking modeling tools and a decent API, but has other interesting limitations in their prefetching

Here are a few things I noticed about the framework:

  1. Their explanation of how many queries it takes to get a list of objects, each with 1-n sublink is confusing, at best. I expect it to take exactly one (1) query … they say two? … oh, Lord, check out the lovely syntax they have in the “advanced” section below:

                ... [More]

Drawbacks to Hibernate

Published by Marco on

The comment, Re: iBATIS vs Hibernate, offers some good advice for when to use iBATIS and when to use Hibernate. The damning sentence for Hibernate follows:

“If you try to shoehorn hibernate into a relational model created by a DBA who could care less about objects and thinks in terms of tables, columns, relationships and record sets, then you will get along better with your DBA if you use iBATIS, especially if the model is complex and may entail queries with outer joins and nested subqueries.”

... [More]