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

de.invation.code.toval.properties.AbstractProperties Maven / Gradle / Ivy

Go to download

TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.

The newest version!
package de.invation.code.toval.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;
import java.util.Set;

import de.invation.code.toval.validate.ParameterException;
import de.invation.code.toval.validate.ParameterException.ErrorCode;
import de.invation.code.toval.validate.Validate;

public class AbstractProperties {

        protected Properties props;

        public AbstractProperties(String fileName) throws IOException {
                load(fileName);
        }

        public AbstractProperties() {
                loadDefaultProperties();
        }

        protected AbstractProperties(Properties properties) {
                setProperties(properties);
        }

        public Properties getProperties() {
                return props;
        }

        public void store(String filename) throws IOException {
                try (FileOutputStream fos = new FileOutputStream(filename)) {
                        props.store(fos, "");
                } catch (Exception e) {
                        throw new IOException("Cannot store properties file to disk.", e);
                }
        }

        public final void load(String fileName) throws IOException {
                props = new Properties();
                try (FileInputStream fis = new FileInputStream(fileName)) {
                        props.load(fis);
                }
        }

        public final void setProperties(Properties properties) {
                this.props = properties;
        }

        protected final void loadDefaultProperties() {
                props = getDefaultProperties();
        }

        protected Properties getDefaultProperties() {
                return new Properties();
        }

        //------- Helper methods --------------------------------------------------------------
        protected  String[] encapsulateValues(Set values) {
                String[] result = new String[values.size()];
                int count = 0;
                for (Object value : values) {
                        result[count++] = "'" + value.toString() + "'";
                }
                return result;
        }

        //------- Validation -------------------------------------------------------------------
        public static void validateStringValue(String value) throws ParameterException {
                Validate.notNull(value);
                Validate.notEmpty(value);
        }

        public static void validatePath(String logPath) throws ParameterException {
                validateStringValue(logPath);
                File cPath = new File(logPath);
                if (!cPath.isDirectory()) {
                        throw new ParameterException(ErrorCode.INCOMPATIBILITY, logPath + " is not a valid path!");
                }
        }

        public static void validateStringCollection(Collection coll) throws ParameterException {
                Validate.notNull(coll);
                Validate.notEmpty(coll);
                for (String string : coll) {
                        validateStringValue(string);
                }
        }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy