public long inc() { synchronized(lock) { long x = (value++) ; set(value) ; return x ; } }
private void writeLocation(long value) { if ( filename != null ) { write(filename, value) ; } }
public void set(long x) { synchronized(lock) { value = x ; writeLocation(); } }
public PersistentCounter(String filename) { this.filename = filename ; String x = FileOps.basename(filename) ; if ( x == null || x.isEmpty() ) x = "." ; this.dir = x ; if ( Files.exists(Paths.get(filename)) ) { value = read(filename) ; return ; } value = 0 ; writeLocation(); }
private static void write(String filename, long value) { try { writeStringAsUTF8(filename, Long.toString(value)) ; } catch (IOException ex) {} catch (NumberFormatException ex) {} }
/** Write a string to a file as UTF-8. The file is closed after the operation. * @param filename * @param content String to be writtem * @throws IOException */ private /*public*/ static void writeStringAsUTF8(String filename, String content) throws IOException { try ( OutputStream out = IO.openOutputFileEx(filename) ) { writeStringAsUTF8(out, content) ; out.flush() ; } }
private void writeLocation() { writeLocation(value) ; }