Monday, June 29, 2009

littleware on Google Code

I just setup my littleware code as a project on Google code: http://code.google.com/p/littleware/ .

What a great site! It was easy to setup the project, but here are a few tricks for interacting with the Mercurial repository.

  • To initialize the Google repository from an existing repository on your local machine:
    • hg clone googlerepo
    • hg bundle localrepo
    • cd googlerepo; hg unbundle localrepo.bundle
    Something like that.
  • Took me a while to realize that googlecode autogenerates a custom password for Mercurial push. You need to track down that custom password under the googlecode admin console to 'hg push' up to Google's repository.

Sunday, June 14, 2009

Guice, Read only POJO, and Builders

I often design java interfaces with methods that return POJOs with read-only properties like the following.


public interface Manager {
      public Info lookupInfo( String sKey );
}

public interface Info {
    public String getA();
    public int    getB();
}

I prefer to decouple Manager's implementation from Info's implementation via Guice injection like this:

public class SimpleManager implements Manager {
    ...
    @Inject
    public SimpleManager( Provider<Info> provideInfo ) { ...
}

The problem with injecting the Info provider is that provideInfo supplies read-only objects which SimpleManager cannot assign property values to. One fix to this problem is to introduce an Info builder interface, and inject that, and a nice way to implement the Builder is to nest the Builder interface within the Info interface, then nest the Info implementation within the Builder implementation. We wind up with something like this:

public interface Info {
    ...
    @ImplementedBy( SimpleInfoBuilder.class )
    public Interface Builder {
        public Builder putA( String val );
        public Builder putB( int val );
        public Info build();
    }
}

public class SimpleInfoBuilder implements Info.Builder {
    private static class SimpleInfo implements Info {
           ...
    }
    ...

    public Info build() { return new SimpleInfo( ... ); }
}


public class SimpleManager implements Manager {
    ...
    @Inject
    public SimpleManager( Provider<Info.Builder> provideInfo ) { ...
}