From 08372956342d872c54f6138f2eb221b164997b5f Mon Sep 17 00:00:00 2001
From: Nikolaus Krismer <niko@krismer.de>
Date: Tue, 11 Feb 2014 10:02:28 +0100
Subject: [PATCH] refactored constructors

---
 .../it/unibz/inf/isochrone/util/Config.java   | 143 ++++++++++--------
 1 file changed, 82 insertions(+), 61 deletions(-)

diff --git a/src/main/java/it/unibz/inf/isochrone/util/Config.java b/src/main/java/it/unibz/inf/isochrone/util/Config.java
index 43b157ae..2b4bfc43 100644
--- a/src/main/java/it/unibz/inf/isochrone/util/Config.java
+++ b/src/main/java/it/unibz/inf/isochrone/util/Config.java
@@ -28,57 +28,43 @@ import java.util.Properties;
  * @author <a href="mailto:markus.innerebner@inf.unibz.it">Markus Innerebner</a>.
  * @version 2.2
  */
-//TODO: Shouldn't Config be a singleton class
 public class Config {
 	protected Properties internalProperties;
 	protected final ConnectionFactory factory;
 	protected short densityLimit = Short.MIN_VALUE;
 
+	// Constructors
+
 	public Config() {
-		this(null);
+		this("config.xml");
 	}
 
-	public Config(final Dataset dataset) {
-		internalProperties = new Properties();
-
-		final String fileName = "config.xml";
-		final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
-		appendPropertyFile(in);
-
-		if (dataset != null) {
-			final String fileName2 = "config_" + dataset.toString().toLowerCase() + ".xml";
-			final InputStream in2 = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName2);
-			appendPropertyFile(in2);
-		}
+	public Config(final String fileName) {
+		appendPropertiesFromFile(fileName);
 
 		factory = initConnectionFactory();
 	}
 
-	protected Properties getInternalProperties() {
-		return internalProperties;
-	}
+	public Config(final Dataset dataset) {
+		appendPropertiesFromFile("config.xml");
+		appendPropertiesFromFile("config_" + dataset.toString().toLowerCase() + ".xml");
 
-	protected ConnectionFactory initConnectionFactory() {
-		final String serverName = getDBServerName();
-		final String dbName = getDBName();
-		final int dbPort = getDBPort();
-		final String dbUser = getDBUser();
-		final String dbPassword = getDBPassword();
-		final boolean isPooling = getDBPooling();
+		factory = initConnectionFactory();
+	}
 
-		ConnectionFactory f = null;
-		try {
-			f = new ConnectionFactory(isPooling);
-			f.register(serverName, dbName, dbPort, dbUser, dbPassword);
-		} catch (final SQLException e) {
-			throw new RuntimeException("Unable to connect to the database: " + e);
-		} catch (final ClassNotFoundException e) {
-			throw new RuntimeException("Class not found: " + e);
+	public Config(final Config config) {
+		if (config == null) {
+			throw new NullPointerException("Invalid config in clone constructor given!");
 		}
 
-		return f;
+		internalProperties = new Properties();
+		appendProperties(config.internalProperties);
+
+		factory = config.factory;
 	}
 
+	// Public methods
+
 	public Connection getConnection() {
 		try {
 			return factory.getConnection();
@@ -127,34 +113,6 @@ public class Config {
 		return Integer.parseInt(getProperty("org.postgresql.port"));
 	}
 
-	/**
-	 * <p>
-	 * Method appendPropertyFile
-	 * </p>
-	 * appends the properties of that stream. An existing property is overwritten
-	 *
-	 * @param inputStream
-	 */
-	protected void appendPropertyFile(final InputStream inputStream) {
-		try {
-			internalProperties.loadFromXML(inputStream);
-			internalProperties.putAll(System.getProperties());
-
-			final String density = getProperty("limit.density");
-			if (densityLimit == Short.MIN_VALUE && density != null) {
-				densityLimit = Short.parseShort(density);
-			}
-		} catch (final InvalidPropertiesFormatException e) {
-			throw new RuntimeException(e);
-		} catch (final IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	public void appendProperties(final Properties properties) {
-		internalProperties.putAll(properties);
-	}
-
 	public String getEdgeTable() {
 		return getProperty("tbl.links");
 	}
@@ -183,4 +141,67 @@ public class Config {
 		return Integer.parseInt(getProperty("sql.spatial.srid"));
 	}
 
+	// Protected methods
+
+	/**
+	 * This appends properties from the given file to the configuration.
+	 * Old values will be kept, but overwritten by new values (if the same property has been specified)
+	 */
+	protected void appendPropertiesFromFile(final String fileName) {
+		internalProperties = new Properties();
+		final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
+		appendPropertiesFromStream(in);
+	}
+
+	/**
+	 * This appends properties from the given stream to the configuration.
+	 * Old values will be kept, but overwritten by new values (if the same property has been specified)
+	 */
+	protected void appendPropertiesFromStream(final InputStream inputStream) {
+		try {
+			internalProperties.loadFromXML(inputStream);
+			internalProperties.putAll(System.getProperties());
+
+			final String density = getProperty("limit.density");
+			if (densityLimit == Short.MIN_VALUE && density != null) {
+				densityLimit = Short.parseShort(density);
+			}
+		} catch (final InvalidPropertiesFormatException e) {
+			throw new RuntimeException(e);
+		} catch (final IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	/**
+	 * This merges properties to the configuration.
+	 * Old values will be kept, but overwritten by new values (if the same property has been specified)
+	 */
+	protected void appendProperties(final Properties properties) {
+		internalProperties.putAll(properties);
+	}
+
+	// Private methods
+
+	private ConnectionFactory initConnectionFactory() {
+		final String serverName = getDBServerName();
+		final String dbName = getDBName();
+		final int dbPort = getDBPort();
+		final String dbUser = getDBUser();
+		final String dbPassword = getDBPassword();
+		final boolean isPooling = getDBPooling();
+
+		ConnectionFactory f = null;
+		try {
+			f = new ConnectionFactory(isPooling);
+			f.register(serverName, dbName, dbPort, dbUser, dbPassword);
+		} catch (final SQLException e) {
+			throw new RuntimeException("Unable to connect to the database: " + e);
+		} catch (final ClassNotFoundException e) {
+			throw new RuntimeException("Class not found: " + e);
+		}
+
+		return f;
+	}
+
 }
-- 
GitLab