Tuesday, 21 June 2011

Cretan Opinel

On our holiday to Paleochora, Crete we spent a day on the East Beach. The sun was pretty hot, so we spent a lot of time under a tamarisk tree. I built a little wall.
On the way back I found an Opinel knife
which has cleaned up quite well. Note the change in the branding: no France and the Main Coroné has moved. This would indicate that it is quite a few years old. (I could not discover when this change occurred).

[Thanks to Laurent in the comments: it was made between 1986 and 1990.]

A button from Tom's Farm

Really quite a long time ago, maybe sixteen years ago, I went to Tom's Farm, on Black Alder Road and walking up the Green Lane found this button. I rediscovered it recently and put it in some silvo, which seemed to show up a bit of silver, now black.
Will give it back to Tom, can't think why I ever took it away, except perhaps I was younger.

Thursday, 9 June 2011

My first functional java class

I have known about this particular hammer for ten years. It is a technique widely used in Melati, as William was mostly 'translating on the fly from OCAML' at the time, however I have never recognised a situation where it was the correct approach.

To a commercial programmer on the long journey from BASIC to lisp, via perl and java, even when you know of the functional hammer the world does not appear to have any functional nails in it.

Last night I did recognise such a situation. I wanted to reuse a bit of code:
if (lock()) {
  try {
    body();
  } catch (Exception e) {
    throw new RuntimeException(e); 
  } finally {
    unlock();
  }
} else {
  throw new RuntimeException("Lock file " + LOCK_FILE_NAME + " exists.");
}

The only way to reuse this, for a body2() function, would have been to cut'n'paste, which I will go to some lengths to avoid.

I extracted this to its own abstract class ExclusiveFunction
import java.io.File;
import java.io.IOException;

/**
 * @author timp
 * @since 2011/06/07 22:00:01
 *
 */
public abstract class ExclusiveFunction {
  final static String LOCK_FILE_NAME = "/tmp/EXCLUSIVE_LOCK";

  abstract void body() throws Exception;
  
  public void invoke() { 
    if (lock()) {
      try {
        body();
      } catch (Exception e) {
        throw new RuntimeException(e); 
      } finally {
        unlock();
      }
    } else {
      throw new RuntimeException("Lock file " + LOCK_FILE_NAME + " exists.");
    }
  }
  
  private boolean lock() {
    File lockFile = new File(LOCK_FILE_NAME);
    if (!lockFile.exists()) {
      try {
        lockFile.createNewFile();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
      return true;
    }
    return false;
  }

  private void unlock() {
    new File(LOCK_FILE_NAME).delete();
  }

}


which is then invoked with:

new ExclusiveFunction() {

      @Override
      void body() throws IOException {
        body2();
      }
    }.invoke();


I think the world is soon going to appear to have a lot more nails in it.