All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.hargrave.configuration.Configuration Maven / Gradle / Ivy

The newest version!
package info.hargrave.configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

/**
 * Manages object-types in the configuration -- that and queries
 * Date: 10/24/13
 * Time: 10:42 PM
 */
public class Configuration {

    private final Serializer serializer;
    private final ConfigurationMedium storage;

    private HashMap configurationData = new HashMap();

    public Configuration(Serializer sprovider, ConfigurationMedium cprovider){
        serializer = sprovider;
        storage = cprovider;
    }

    /**
     * Load the configuration from the ConfigurationMedium
     * @throws IOException if the configuration should fail to load
     */
    public void loadFromMedium() throws IOException {
        try{
            configurationData = storage.readConfiguration();
        } catch (ClassNotFoundException clsExcept) {
            throw new IOException("Unable to load the configuration from the medium");
        }
    }

    /**
     * Save the configuration to the ConfigurationMedium
     * @throws IOException if the configuration should fail to commit
     */
    public void commitToMedium() throws IOException {

        /*
         * Note: This map is copied for thread safety reasons
         */
        storage.commitConfiguration(new HashMap(configurationData));

    }

    /**
     * Retrieve a value from the configuration
     * @param key name of the value
     * @param objectType class belonging to the type of the value
     * @param  class-type of value
     * @return value
     * @throws KeyException if there is no key/value pair for the key
     */
    public  OT retrieve(String key, Class objectType) throws KeyException{

        if(!configurationData.containsKey(key))
            throw new KeyException("No such key exists", key);

        //TODO Logger; Error messages
        try{
            return serializer.deSerialize(configurationData.get(key), objectType);
        } catch(IOException io) {
            return null;
        } catch(ClassNotFoundException cls) {
            return null;
        }

    }

    /**
     * Set a value in the configuration
     * @param key name of the value
     * @param value value
     * @throws IOException when serialization should fail
     * @throws KeyException when the key already exists
     */
    public void set(String key, Object value) throws IOException, KeyException {

        if(configurationData.containsKey(key))
            throw new KeyException("Key already exists in configuration", key);

        configurationData.put(key, serializer.serialize(value));
    }


    /**
     * Remove a key/value pair from the configuration
     * @param key key of the pair
     * @throws KeyException when the key does not exist
     */
    public void delete(String key) throws KeyException {

        if(!configurationData.containsKey(key))
            throw new KeyException("No such key exists", key);

        configurationData.remove(key);

    }

    /**
     * Get a list of the keys in the configuration
     * @return keys
     */
    public Set getKeys() {
        return configurationData.keySet();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy