view src/org/dancres/blitz/entry/InstanceSwitchListener.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.blitz.entry;

import org.dancres.blitz.stats.Stat;
import org.dancres.blitz.stats.SwitchListener;
import org.dancres.blitz.stats.Switch;
import org.dancres.blitz.stats.StatsBoard;
import org.dancres.blitz.stats.InstanceSwitch;
import org.dancres.blitz.stats.InstanceCount;
import org.dancres.blitz.stats.SwitchSettings;

class InstanceSwitchListener implements SwitchListener {
    private String theInstanceType;
    private InstanceCount theCount;

    InstanceSwitchListener(String aType, int anInitialCount) {
        theInstanceType = aType;
        theCount = new InstanceCount(aType, anInitialCount);

        SwitchSettings.get().add(this);
    }

    public synchronized void switchFlipped(Switch aSwitch) {
        if (aSwitch instanceof InstanceSwitch) {
            InstanceSwitch mySwitch = (InstanceSwitch) aSwitch;

            if ((mySwitch.getType().equals(theInstanceType)) ||
                (mySwitch.isWildcard())) {

                if (mySwitch.isOn()) {
                    StatsBoard.get().add(theCount);
                } else
                    StatsBoard.get().remove(theCount);
            }
        }
    }

    synchronized void wrote() {
        theCount.wrote();
    }

    synchronized void took() {
        theCount.took();
    }

    Stat getInstanceStat() {
        return theCount.generate();
    }

    synchronized int getTotal() {
        return theCount.getCount();
    }

    void destroy() {
        SwitchSettings.get().remove(this);
        StatsBoard.get().remove(theCount);
    }
}