view src/org/dancres/util/Timestamp.java @ 35:6f68e94c1fb8 default tip

Add CondensedStats monitoring utility, equivalent to vmstat
author Dominic Cleal <dominic-cleal@cdo2.com>
date Thu, 05 Aug 2010 11:07:25 +0100
parents 3dc0c5604566
children
line wrap: on
line source

package org.dancres.util;

import java.util.HashMap;

/**
   Generates timestamps values which can be used to uniquely order events.
 */
public class Timestamp {
    private static HashMap theTimestamps = new HashMap();

    public static Timestamp newTimestamp(String aName) {
        synchronized(theTimestamps) {
            if (theTimestamps.get(aName) != null)
                throw new IllegalArgumentException("Timestamp already exists");

            Timestamp myTimestamp = new Timestamp();
            theTimestamps.put(aName, myTimestamp);

            return myTimestamp;
        }
    }

    public static Timestamp getTimestamp(String aName) {
        synchronized(theTimestamps) {
            return (Timestamp) theTimestamps.get(aName);
        }
    }

    long theStamp = 0;

    private Timestamp() {
    }

    /**
       Returns the last timestamp allocated or zero if none have been
       allocated yet.
     */
    public long get() {
        synchronized(this) {
            return theStamp;
        }
    }

    /**
       Return the next timestamp
     */
    public long next() {
        synchronized(this) {
            return ++theStamp;
        }
    }
}