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 * @author apr@cs.com.uy
026 * @version $Id$
027 * @since jPOS 1.1
028 *
029 * A simple sequencer intended for Debugging applications.<br>
030 * Production grade Sequencers are required to be persistent capables
031 */
032@SuppressWarnings("unchecked")
033public class VolatileSequencer implements Sequencer, VolatileSequencerMBean {
034    private Map map;
035    public VolatileSequencer () {
036        map = new HashMap();
037    }
038    /**
039     * @param counterName
040     * @param add increment
041     * @return counterName's value + add
042     */
043    synchronized public int get (String counterName, int add) {
044        int i = 0;
045        Integer I = (Integer) map.get (counterName);
046        if (I != null)
047            i = I;
048        i += add;
049        map.put (counterName, i);
050        return i;
051    }
052    /**
053     * @param counterName
054     * @return counterName's value + 1
055     */
056    public int get (String counterName) {
057        return get (counterName, 1);
058    }
059    /**
060     * @param counterName
061     * @param newValue
062     * @return oldValue
063     */
064    synchronized public int set (String counterName, int newValue) {
065        int oldValue = 0;
066        Integer I = (Integer) map.get (counterName);
067        if (I != null)
068            oldValue = I;
069        map.put (counterName, newValue);
070        return oldValue;
071    }
072    public String[] getCounterNames () {
073        Object[] o = map.keySet().toArray();
074        String[] s = new String [o.length];
075        System.arraycopy (o, 0, s, 0, o.length);
076        return s;
077    }
078}