/*
 * SynchronizedStore.java
 *
 * Created on January 15, 2002, 10:09 AM
 * Last modified  on  $Date$ by $Author$
 */
package org.apache.commons.simplestore;

import java.io.IOException;
/**
 *
 * @author  Juozas Baliuka
 * @version $Revision$
 */
final class SynchronizedStore implements Store {
    
    private Store store;
    
    
    /** Creates new SynchronizedStore */
    public SynchronizedStore(Store store) {
        if(store == null)
            throw new NullPointerException();
        this.store = store;
    }
    
    
    /**
     * Returns the list of used keys as an Enumeration of Objects.
     */
    public java.util.Enumeration keys() {
        synchronized(store){ // not very meaningful here
           return store.keys();
        }
    }
    
    /**
     * Remove the object associated to the given key.
     *
     * @param key the Key Object
     */
    public void remove(Object key) {
        synchronized(store){
            store.remove(key);
        }    
    }
    
    /**
     * Indicates if the given key is associated to a contained object.
     *
     * @param key the Key Object
     */
    public boolean containsKey(Object key) {
        synchronized(store){
          return store.containsKey(key);
        }
    }
    
    /**
     * Frees some object out of the Store.
     */
    public void free() {
        synchronized(store){
           store.free();
        }
    }
    
    /**
     * Store the given object in a persistent state. It is up to the
     * caller to ensure that the key has a persistent state across
     * different JVM executions.
     *
     * @param key the Key Object
     * @param value the Value Object
     */
    public void store(Object key, Object value) throws IOException {
        synchronized(store){
          store.store(key,value);
        }
    }
    
    /**
     * Holds the given object in a volatile state. This means
     * the object store will discard held objects if the
     * virtual machine is restarted or some error happens.
     *
     * @param key the Key Object
     * @param value the Value Object
     */
    public void hold(Object key, Object value) throws IOException {
        synchronized(store){
          store.hold(key,value);
        }
    }
    
    /**
     * Get the object associated to the given unique key.
     *
     * @param key the Key Object
     */
    public Object get(Object key) {
        synchronized(store){
          return store.get(key);
        }
    }
    
}
