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

org.aludratest.cloud.config.SimplePreferences Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2010-2015 AludraTest.org and the contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aludratest.cloud.config;

import java.util.HashMap;
import java.util.Map;

/**
 * Default implementation of the {@link MutablePreferences} interface, which just holds all configuration in memory. Does not fire
 * events on any Setter calls.
 * 
 * @author falbrech
 * 
 */
public class SimplePreferences extends AbstractPreferences implements MutablePreferences {

    private Map values = new HashMap();

    // lazy initialization as most modules will not have children
    private Map children;

	/**
	 * Creaes a new SimplePreferences object with the given parent.
	 * 
	 * @param parent
	 *            Parent of the SimplePreferences object, may be null.
	 */
	public SimplePreferences(Preferences parent) {
		super(parent);
	}

    @Override
    public String internalGetStringValue(String key) {
        return values.get(key);
    }

    @Override
    public String[] getKeyNames() {
        return values.keySet().toArray(new String[0]);
    }

    @Override
    public Preferences getChildNode(String name) {
        if (children == null) {
            return null;
        }

        return children.get(name);
    }

    @Override
    public String[] getChildNodeNames() {
        if (children == null) {
            return new String[0];
        }

        return children.keySet().toArray(new String[0]);
    }

    @Override
    public void setValue(String key, String value) {
        if (key.contains("/")) {
            String childName = key.substring(0, key.indexOf('/'));
            MutablePreferences child = createChildNode(childName);
            child.setValue(key.substring(key.indexOf('/') + 1), value);
        }
        else {
            values.put(key, value);
        }
    }

    @Override
    public void setValue(String key, boolean value) {
        setValue(key, "" + value);
    }

    @Override
    public void setValue(String key, int value) {
        setValue(key, "" + value);
    }

    @Override
    public void setValue(String key, double value) {
        setValue(key, "" + value);
    }

    @Override
    public void setValue(String key, float value) {
        setValue(key, "" + value);
    }

    @Override
    public void setValue(String key, char value) {
        setValue(key, "" + value);
    }

    @Override
    public MutablePreferences createChildNode(String name) {
        if (name == null || "".equals(name)) {
            throw new IllegalArgumentException("Name must be a non-empty string");
        }

        if (children == null) {
            children = new HashMap();
        }

        if (children.containsKey(name)) {
            return children.get(name);
        }

		SimplePreferences prefs = new SimplePreferences(this);
        children.put(name, prefs);

        return prefs;
    }

    @Override
    public void removeChildNode(String name) {
        if (children != null) {
            children.remove(name);
        }
    }

    @Override
    public void removeKey(String key) {
        values.remove(key);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy