IdGenerator.java :  » Game » galatichero » net » narusas » cafelibrary » Java Open Source

Java Open Source » Game » galatichero 
galatichero » net » narusas » cafelibrary » IdGenerator.java
package net.narusas.cafelibrary;

import java.util.prefs.Preferences;

public abstract class IdGenerator {
  static IdGenerator instance;

  public static long newId() {
    return getInstance().createId();
  }

  abstract protected long createId();

  private static IdGenerator getInstance() {
    if (instance == null) {
      instance = new PreferenceIdGenerator();
    }
    return instance;
  }

  public static void setLastId(long id) {
    getInstance().setLastIdReally(id);
  }

  abstract protected void setLastIdReally(long id);
}

class PreferenceIdGenerator extends IdGenerator {

  
  protected synchronized long createId() {
    long id = Preferences.userRoot().node("libcafe").getLong("lastId", 0);
    id++;
    setLastIdReally(id);
    return id;
  }

  
  protected void setLastIdReally(long id) {
    Preferences.userRoot().node("libcafe").putLong("lastId", id);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.