Freitag, 30. November 2012

Abolishing Java's final restriction

The keyword final can be use to indicate, that a variable should not be changed after it has been assign. It is a constant value. Java requires the assignment to be taken place in the initialization of a object. This means that a final can be initialized in the constructor or a instance initialization block. But it can not be initialized in a method. If this functionality is required it has to be coded by hand. The following code emulates final with the ability to set the value out of the initialization scope of an object.
// -*- compile-command: "javac fin.java && java fin"; -*-

class fin
{
    private class Final<Type>
    {
        private Type value;
        private boolean defined = false;

        public void set (Type value)
        {
            if (defined)
                throw new IllegalStateException ("Value is already defiend.");
            this.value = value;
            defined = true;
        }

        Type get ()
        {
            if (!defined)
                throw new IllegalStateException ("Value is undefiend.");
            return value;
        }

        boolean defined ()
        {
            return defined;
        }
    }

    void run()
    {
        Final<Integer> i = new Final<Integer>();

        i.set(42);                    // ok
        System.out.println (i.get()); // ok
        i.set(43);                    // failure
    }

    public static void main (String argv[])
    {
        new fin().run();
    }
}

Dienstag, 27. November 2012

Long Term Evolution Next Generation (LTENG)

Nowadays there is almost no web site without any Facebook, Twitter, Google, "Fuck my brain" or what else button. Those buttons are implemented by loading JavaScript APIs and images. After loading the APIs the code gets executed and does itself some TCP connections maybe even with TLS encryption to load additional data like the number of bone heads which already pawed the button. In the end you have about a dozen network connections for every single page significantly slowing down what you really want to see.

But there is a solution for the global network pollution: Adblock. Let us pray to the god of the Internet to thank him for giving us the Internet litter service.

After you have done this you should consider doing it just again.

Next we can customize the Adblock with the following URLs:

connect.facebook.net
platform.twitter.com
apis.google.com/js/plusone.js
Now we can lay back and enjoy the new Internet speed boost. I call it Long Term Evolution Next Generation™.

Feel free to like it. But do not tell anybody about. Nobody wants to know it.

Freitag, 23. November 2012

Instance initializations in abstract Java classes

Instance initializations are rarely seen in Java code but they allow funny things together with abstract classes. Class initializations are widely used and are written by enclosing the code block with single curly brackets. Instance initializations are written in the same way but with two curly brackets. When used in an abstract class the initialization code gets executed in the scope of the derived class but without the need to specify any constructor and without the need to call any parent constructor with super. Furthermore it is possible to call abstract methods in the initialization block. By this it is possible to force an initialization with some kind of default logic but without forcing the derived class to use this default logic. The following example illustrates this.
// -*- compile-command: "javac custom.java && java custom"; -*-

class custom
{
    abstract class A
    {
        final String name;
        {{ name = calc_name(); }}

        protected String calc_name() {
            return getClass().getName();
        }
    }
    
    class B extends A
    {
    }

    class C extends A
    {
        protected String calc_name() { return "C"; }
    }

    void run ()
    {
        B b = new B();
        C c = new C();
        System.out.println (b.name);  // -> custom$B
        System.out.println (c.name);  // -> C
    }

    public static void main (String argv[])
    {
        custom x = new custom();
        x.run();
    }
}
The same thing will not work in static contexts, because Java lacks any generic syntax like self in PHP to get the name of the current class other than writing an exact class name directly into the code. Calling A.class.getName() in the abstract class A will never return B in the class B derived from the abstract class A.

Freitag, 16. November 2012

Checking a column definition in Oracle

Sometimes it is useful to check, if a specific column of a specific table has the required definition. Oracle exports the structure of all tables in the table all_tab_columns. Here is an example to check if a table PERSON exists with a column FIRSTNAME being of the type VARCHAR2 holding 100 bytes:
select count(*) from all_tab_columns 
where 
  table_name = 'PERSON' and
  column_name = 'FIRSTNAME' and
  data_type = 'VARCHAR2' and
  data_length = 100;

Overloading with anonymous classes in Java

Most examples of anonymous classes in Java use interfaces but it is also possible to create an anonymous class by overloading a normal class. Here is an example:
class inner
{
    int x;
    inner (int x) { this.x = x; }
    int next () { return x + 1; }

    public static void main (String[] args)
    {
        inner i = new inner (42);
        inner j = new inner (42) {
                { this.x = x + 1; }
                int next () { return x - 1; }
            };

        System.out.println (i + ": " + i.x);  // -> 42
        System.out.println (j + ": " + j.x);  // -> 43

        System.out.println (i + " next: " + i.next());  // -> 43
        System.out.println (j + " next: " + j.next());  // -> 42
    }
}
And here is a bit more complicated example: an anonymous class derived from an abstract class implementing an interface based on another interface.
// outer class
class run
{
    // named inner interface
    interface Named
    {
        String name ();
    }

    // named inner interface extending another interface
    interface Column<T> extends Named
    {
        T get ();
        void set (T value);
    }

    // named abstract class implementing an interface
    abstract class StringColumn implements Column<String>
    {
        public abstract String name ();
        int length;
        StringColumn (int length) { this.length = length; }
        String value;
        public String get () { return value; }
        public void set (String value) { this.value = value; }
    }

    run () 
    {
        // anonymous class extending the abstract class
        Column<String> column = new StringColumn (100) {
                public String name () { return "FIRSTNAME"; }
            };

        column.set ("Sascha");

        System.out.println (column.name() + ": " + column.get());
    }

    public static void main (String[] args)
    {
        new run ();  // -> FIRSTNAME: Sascha
    }
}