001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.core;
020
021import java.util.HashMap;
022import java.util.Map;
023
024/**
025 *
026 * A simple sequencer intended for Debugging applications.<br>
027 * Production grade Sequencers are required to be persistent capables
028 *
029 * @author apr@cs.com.uy
030 * @version $Id$
031 * @since jPOS 1.1
032 *
033 */
034@SuppressWarnings("unchecked")
035public class VolatileSequencer implements Sequencer, VolatileSequencerMBean {
036    private Map map;
037    /** Default constructor. */
038    public VolatileSequencer () {
039        map = new HashMap();
040    }
041    /**
042     * @param counterName name of the counter
043     * @param add increment
044     * @return counterName's value + add
045     */
046    synchronized public int get (String counterName, int add) {
047        int i = 0;
048        Integer I = (Integer) map.get (counterName);
049        if (I != null)
050            i = I;
051        i += add;
052        map.put (counterName, i);
053        return i;
054    }
055    /**
056     * @param counterName name of the counter
057     * @return counterName's value + 1
058     */
059    public int get (String counterName) {
060        return get (counterName, 1);
061    }
062    /**
063     * @param counterName name of the counter
064     * @param newValue value to assign to the counter
065     * @return oldValue
066     */
067    synchronized public int set (String counterName, int newValue) {
068        int oldValue = 0;
069        Integer I = (Integer) map.get (counterName);
070        if (I != null)
071            oldValue = I;
072        map.put (counterName, newValue);
073        return oldValue;
074    }
075    public String[] getCounterNames () {
076        Object[] o = map.keySet().toArray();
077        String[] s = new String [o.length];
078        System.arraycopy (o, 0, s, 0, o.length);
079        return s;
080    }
081}