Code example for AtomicLong

Methods: getincrementAndGetset

0
        AtomicLong serialId = getSerialId(pid);
 
        // Asign a serialid to the source if we don't already have one 
        String sourceid = source.get(TRANSIENT_PROPERTY_SERIAL_ID);
        if (sourceid == null) {
            sourceid = new Long(serialId.incrementAndGet()).toString();
            source.put(TRANSIENT_PROPERTY_SERIAL_ID, sourceid);
        } 
 
        // Ignore the serialid on rollback 
        if (rollback) {
            LOGGER.debugf("Config rollback: %s => %s", pid, source);
            serialId.set(Long.parseLong(sourceid));
            configurations.put(pid, source);
            return true; 
        } 
 
        // Update if there is no previous config for this pid 
        Dictionary<String, String> previous = configurations.get(pid);
        if (previous == null) {
            if (Long.parseLong(sourceid) == serialId.get()) {
                LOGGER.debugf("Config put: %s => %s", pid, source);
                configurations.put(pid, new UnmodifiableDictionary<String, String>(source));
                return true; 
            } else { 
                LOGGER.debugf("Config skip put: %s => %s", pid, source);
                return false; 
            } 
        } 
 
        // Do not allow an older configuration to update a newer one 
        String previd = previous.get(TRANSIENT_PROPERTY_SERIAL_ID);
        if (Long.parseLong(previd) <= Long.parseLong(sourceid)) {
            LOGGER.debugf("Config put: %s => %s", pid, source);
            configurations.put(pid, new UnmodifiableDictionary<String, String>(source));
            return true; 
        } else { 
            LOGGER.debugf("Config skip put: %s => %s", pid, source);
            return false; 
        } 
    } 
 
    private AtomicLong getSerialId(String pid) {
        AtomicLong serialid = serialids.get(pid);
        if (serialid == null) {
            serialids.put(pid, serialid = new AtomicLong());
        } 
        return serialid;
    } 
}