Tuesday 19 October 2010

Access properties as if they are constants

Goal
Sometimes it is nice to be able to access properties from a properties file in the same way as enums are accessed within Java. By this I mean that you can simply use the '.' (dot) notation and you get exactly what you expect.

for example with this enum

enum Colours { 
    BLUE(1), GREEN(2), RED(3);
    private int num;
    Colours( final int i ) {
        num = i;
    }
    int getValue() {
        return num; 
    }
}

you can expect that
int iCol = Colours.GREEN.getValue();
will not only compile but also give the correct number to iCol.
Now imagine that the colour blue is no longer to be represented by the number 1 but the number 7 instead, you would need to re-compile you enum in order to get the correct number.

Very Simple Solution
now take this code
enum KFType {
    type( "type", ConversionType.INT ),
    words( "words", ConversionType.STRING ),
    //example in overriding the getter
    special( "special", ConversionType.INT ) {
        @SuppressWarnings( "unchecked" )
        @Override
        T getValue() {
            Integer t = super.getValue();
            return (T) Integer.valueOf( t.intValue() + 10 );
        }
    };

    private ConversionType c;
    private String tag;
    private static final Properties keyFile = new Properties();
    static {
        try {
            keyFile.load( new FileInputStream( "path to props" ) );
        } catch ( IOException ioe ) {
            ioe.printStackTrace();
        }
    }
    KFType( final String t, final ConversionType con ) {
        c = con;
        tag = t;
    }
    T getValue() {
        return c.convert( keyFile.get( tag ) );
    }
}

@SuppressWarnings( "unchecked" )
enum ConversionType {
    INT {
        Integer convert( Object o ) {
            return Integer.valueOf( o.toString() );
        }
    },
    STRING {
        String convert( Object o ) {
            return o.toString();
        }
    };
    abstract T convert( Object o );
}

The disadvantage here is that in order to get any changed values from the properties file the jvm will need to reload the enumerated type which normally means a restart (I think). The big advantage here is that you can now use properties from properties file, or an xml file as if they were code.
I have also shown that you can perform some sort of transformation to the values read from the properties file, here I simply add 10 to the number read but there is no reason why we couldn't wrap text in an http url or something similar.
Look at the output of this
@Test
public void testEnum() {
System.out.println( ((Integer)KFType.type.getValue()) + 10 );
System.out.println( ((Integer)KFType.special.getValue()) + 10 );
System.out.println( KFType.words.getValue() );
System.out.println( KFType.special.getValue().getClass() );
System.out.println( KFType.words.getValue().getClass() );
}


12355
120
these are words
class java.lang.Integer
class java.lang.String

this is when I use the following properties
type=12345
words=these are words
special=100




Monday 11 October 2010

Kindle

Kindle I am probably the last person to provide his verdict about the Kindle, but I don't care here it is anyway. I have for sometime been looking for a good way to read books when not in my house. Being a developer I find that in order to read about a subject area I do normally tend to read several books side by side which is great when at a desk with plenty of space to spread out but not so great when on the move. I was quite surprised by the cost of the Kindle the only other device to come close in my opinion was the sony eReader and that was at least four hundred quid when I last checked. My kindle was £149 and it came with free 3G access to the kindle store plus I could actually browse the internet. It's not great at browsing the internet, but that's not what it's for. One thing it does do very well is that it has an 'Article Mode' that is great when reading wikipedia entries. The most important thing is that I could read it all day everyday when I was on holiday without getting eye strain or needing to recharge it. Whilst I was sat on the beach in the sun reading happily away it gave me great satisfaction to see people trying to read iPads and failing because of the glare from the sun and running out of power at inopportune moments like as soon as they get to the airport tee hee. All in all brilliant bit of kit, if it was any bigger then I wouldn't be able to put it in my suit pocket so I hope this never changes in future.
 
Stack Overflow profile for Richard Johnson at Stack Overflow, Q&A for professional and enthusiast programmers