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

quickfix.Dictionary Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
/****************************************************************************
 ** Copyright (c) quickfixengine.org  All rights reserved.
 **
 ** This file is part of the QuickFIX FIX Engine
 **
 ** This file may be distributed under the terms of the quickfixengine.org
 ** license as defined by quickfixengine.org and appearing in the file
 ** LICENSE included in the packaging of this file.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ** See http://www.quickfixengine.org/LICENSE for licensing information.
 **
 ** Contact [email protected] if any conditions of this licensing are
 ** not clear to you.
 **
 ****************************************************************************/

package quickfix;

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

/**
 * Name/value pairs used for specifying groups of settings.
 *
 * @see SessionSettings
 */
public class Dictionary {
    private String name;
    private final HashMap data = new HashMap<>();

    public Dictionary() {
    }

    public Dictionary(String name) {
        this(name, new HashMap<>());
    }

    public Dictionary(Dictionary dictionary) {
        this(dictionary.name, dictionary.data);
    }

    public Dictionary(String name, Map data) {
        this.name = name;
        this.data.putAll(data);
    }

    public String getName() {
        return name;
    }

    public int size() {
        return data.size();
    }

    public String getString(String key) throws ConfigError, FieldConvertError {
        try {
            return data.get(key).toString();
        } catch (NullPointerException e) {
            throw new ConfigError("No value for key: " + key);
        }
    }

    public String getString(String key, boolean capitalize) throws ConfigError, FieldConvertError {
        String string = getString(key);
        return capitalize ? string.toUpperCase() : string;
    }

    public long getLong(String key) throws ConfigError, FieldConvertError {
        try {
            return (Long) data.get(key);
        } catch (ClassCastException e) {
            throw new FieldConvertError("Incorrect data type");
        } catch (NullPointerException e) {
            throw new ConfigError("No value for key: " + key);
        }
    }

    public double getDouble(String key) throws ConfigError, FieldConvertError {
        try {
            return (Double) data.get(key);
        } catch (ClassCastException e) {
            throw new FieldConvertError("Incorrect data type");
        } catch (NullPointerException e) {
            throw new ConfigError("No value for key: " + key);
        }
    }

    public boolean getBool(String key) throws ConfigError, FieldConvertError {
        try {
            return (Boolean) data.get(key);
        } catch (ClassCastException e) {
            throw new FieldConvertError("Incorrect data type");
        } catch (NullPointerException e) {
            throw new ConfigError("No value for key: " + key);
        }
    }

    public int getDay(String key) throws ConfigError, FieldConvertError {
        Object datum = data.get(key);
        if (datum == null) {
            throw new ConfigError("No value for key.");
        }
        if (datum instanceof String) {
            return DayConverter.toInteger((String) datum);
        }
        throw new ConfigError("Invalid data type for day value: " + datum.getClass().getName());
    }

    public void setString(String key, String value) {
        data.put(key, value);
    }

    public void setLong(String key, long value) {
        data.put(key, value);
    }

    public void setDouble(String key, double value) {
        data.put(key, value);
    }

    public void setBool(String key, boolean value) {
        data.put(key, value);
    }

    public void setDay(String key, int value) {
        try {
            data.put(key, DayConverter.toString(value));
        } catch (ConfigError e) {
            // JNI API doesn't allow a ConfigError to be thrown
            throw new RuntimeError(e);
        }
    }

    public void setDay(String key, String dayName) {
        data.put(key, dayName);
    }

    public boolean has(String key) {
        return data.containsKey(key);
    }

    public void merge(Dictionary toMerge) {
        data.putAll(toMerge.data);
    }

    public Map toMap() {
        return data;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy