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

com.ovea.tajin.props.PropertySettings Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2011 Ovea 
 *
 * 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 com.ovea.tajin.props;

import com.ovea.tajin.io.Resource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public final class PropertySettings {

    private final PropertyPlaceholderResolver resolver = new PropertyPlaceholderResolver();
    private final Properties properties;

    public PropertySettings() {
        this(new Properties());
    }

    public PropertySettings(Properties properties) {
        this.properties = properties;
        this.resolver.setSystemPropertiesMode(SystemPropertiesMode.OVERRIDE);
    }

    public PropertySettings(Resource resource) {
        if (!resource.isExist())
            throw new IllegalArgumentException("Inexisting resource: " + resource);
        InputStream is = resource.getInput();
        this.properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            throw new IllegalArgumentException("Unreadable resource: " + resource);
        } finally {
            try {
                is.close();
            } catch (IOException ignored) {
            }
        }
        this.resolver.setSystemPropertiesMode(SystemPropertiesMode.OVERRIDE);
    }

    public File getPath(String key) {
        return new File(getRequired(key));
    }

    public String getString(String key) {
        return getRequired(key);
    }

    public String getString(String key, String def) {
        String v = resolve(key);
        return v == null ? def : v;
    }

    public List getStrings(String key) {
        return Arrays.asList(getRequired(key).split(",|;"));
    }

    public List getStrings(String key, String... def) {
        String v = resolve(key);
        return Arrays.asList((v == null ? def : v.split(",|;")));
    }

    public Resource getResource(String key) {
        return Resource.resource(getRequired(key));
    }

    private String getRequired(String key) throws MissingPropertySettingException {
        String v = resolve(key);
        if (v == null)
            throw new MissingPropertySettingException(key);
        return v;
    }

    public Properties getProperties() {
        return properties;
    }

    @Override
    public String toString() {
        return properties.toString();
    }

    private String resolve(String key) {
        return resolver.resolve(key, properties);
    }

    public long getLong(String key) {
        return Long.parseLong(getRequired(key));
    }

    public long getLong(String key, long def) {
        String v = resolve(key);
        return v == null ? def : Long.parseLong(v);
    }

    public int getInt(String key) {
        return Integer.parseInt(getRequired(key));
    }

    public int getInt(String key, int def) {
        String v = resolve(key);
        return v == null ? def : Integer.parseInt(v);
    }

    public boolean getBoolean(String key) {
        return Boolean.valueOf(getRequired(key));
    }

    public boolean getBoolean(String key, boolean def) {
        String v = resolve(key);
        return v == null ? def : Boolean.valueOf(v);
    }

    public > E getEnum(Class type, String key) {
        return Enum.valueOf(type, getRequired(key));
    }

    public > E getEnum(Class type, String key, E def) {
        String v = resolve(key);
        return v == null ? def : Enum.valueOf(type, v);
    }

    public boolean has(String key) {
        return resolve(key) != null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy