001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (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 Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.util.concurrent;
019
020import java.util.concurrent.ConcurrentHashMap;
021import java.util.concurrent.ConcurrentMap;
022
023/**
024 * @author Paul Ferraro
025 */
026public class MapRegistryStoreFactory<K> implements RegistryStoreFactory<K>
027{
028        /**
029         * {@inheritDoc}
030         * @see net.sf.hajdbc.util.concurrent.RegistryStoreFactory#createStore()
031         */
032        @Override
033        public <V> LifecycleRegistry.Store<K, V> createStore()
034        {
035                return new MapRegistryStore<K, V>();
036        }
037        
038        static class MapRegistryStore<K, V> implements LifecycleRegistry.Store<K, V>
039        {
040                private final ConcurrentMap<K, V> map = new ConcurrentHashMap<K, V>();
041                
042                /**
043                 * {@inheritDoc}
044                 * @see net.sf.hajdbc.util.concurrent.RegistryStore#setIfAbsent(java.lang.Object, java.lang.Object)
045                 */
046                @Override
047                public V setIfAbsent(K key, V value)
048                {
049                        return this.map.putIfAbsent(key, value);
050                }
051
052                /**
053                 * {@inheritDoc}
054                 * @see net.sf.hajdbc.util.concurrent.RegistryStore#get(java.lang.Object)
055                 */
056                @Override
057                public V get(K key)
058                {
059                        return this.map.get(key);
060                }
061
062                /**
063                 * {@inheritDoc}
064                 * @see net.sf.hajdbc.util.concurrent.RegistryStore#clear(java.lang.Object)
065                 */
066                @Override
067                public V clear(K key)
068                {
069                        return this.map.remove(key);
070                }
071        }
072}