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.space;
020
021import java.util.ArrayList;
022import java.util.List;
023
024/**
025 * Space related helper methods
026 *
027 * @author Alejandro Revilla
028 * @version $Revision$ $Date$
029 * @since 1.4.7
030 */
031
032@SuppressWarnings("unchecked")
033public class SpaceUtil {
034    /**
035     * return all entries under a given key
036     *
037     * @param sp the Space
038     * @param key Entry's key
039     * @return array containing all entries under key
040     */
041    public static Object[] inpAll (Space sp, Object key) {
042        List list = new ArrayList();
043        Object value;
044        do {
045            value = sp.inp (key);
046            if (value != null) {
047                list.add (value);
048            }
049        } while (value != null);
050        return list.toArray();
051    }
052
053    /**
054     * Remove all entries under key
055     *
056     * @param sp the Space
057     * @param key Entry's key
058     */
059    public static void wipe (Space sp, Object key) {
060        while (sp.inp (key) != null)
061            ; // NOPMD
062    }
063
064    /**
065     * @deprecated Use space.put instead
066     */
067    @Deprecated
068    public static void wipeAndOut  (Space sp, Object key, Object value) {
069        sp.put(key, value);
070    }
071
072    /**
073     * @deprecated use space.put instead
074     */
075    @Deprecated
076    public static void wipeAndOut  (Space sp, Object key, Object value, long timeout) {
077        sp.out(key, value, timeout);
078    }
079    public static long nextLong (Space sp, Object key) {
080        long l = 0L;
081        synchronized (sp) {
082            Object obj = sp.inp (key);
083            wipe (sp, key); // just in case
084            if (obj instanceof Long) 
085                l = (Long) obj;
086            sp.out (key, ++l);
087        }
088        return l;
089    }
090    public static boolean outIfEmpty (Space sp, Object key, Object value, long nrdTimeout, long outTimeout) {
091        synchronized (sp) {
092            if (sp.nrd(key, nrdTimeout) == null) {
093                sp.out(key, value, outTimeout);
094                return true;
095            }
096        }
097        return false;
098    }
099    public static void outWhenEmpty (Space sp, Object key, Object value, long timeout) {
100        synchronized (sp) {
101            sp.nrd(key);
102            sp.out(key, value, timeout);
103        }
104    }
105    public static void outWhenEmpty (Space sp, Object key, Object value) {
106        synchronized (sp) {
107            sp.nrd(key);
108            sp.out(key, value);
109        }
110    }
111}