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

com.appslandia.common.base.PropertiesMap Maven / Gradle / Ivy

The newest version!
// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.common.base;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
/**
*
* @author Loc Ha
*
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.appslandia.common.formatters.Formatter;
import com.appslandia.common.utils.StringUtils;

/**
 *
 * @author Loc Ha
 *
 */
public class PropertiesMap implements Map, PropertiesConfig, Serializable {
	private static final long serialVersionUID = 1L;

	protected final Map entries;

	public PropertiesMap() {
		this.entries = new CaseInsensitiveMap<>();
	}

	public PropertiesMap(Map newMap) {
		this.entries = newMap;
	}

	public void load(InputStream inStream) throws IOException {
		Properties props = new LinkedProperties();
		props.load(inStream);
		fromProperties(props);
	}

	public void load(Reader reader) throws IOException {
		Properties props = new LinkedProperties();
		props.load(reader);
		fromProperties(props);
	}

	public void load(File file) throws IOException {
		try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
			load(r);
		}
	}

	public void load(String file) throws IOException {
		load(new File(file));
	}

	protected void fromProperties(Properties props) {
		for (Object k : props.keySet()) {
			String key = (String) k;
			String value = (String) props.get(key);

			if (key.isEmpty() == false) {
				this.entries.put(key, (value.isEmpty() == false) ? value : null);
			}
		}
	}

	public void store(OutputStream os, String comments) throws IOException {
		toProperties().store(os, comments);
	}

	public void store(Writer w, String comments) throws IOException {
		toProperties().store(w, comments);
	}

	protected Properties toProperties() {
		Properties props = new LinkedProperties();
		for (Map.Entry prop : this.entries.entrySet()) {
			props.put(prop.getKey(), prop.getValue() != null ? prop.getValue() : StringUtils.EMPTY_STRING);
		}
		return props;
	}

	@Override
	public String getString(String key) {
		return this.entries.get(key);
	}

	@Override
	public int size() {
		return this.entries.size();
	}

	@Override
	public boolean isEmpty() {
		return this.entries.isEmpty();
	}

	@Override
	public boolean containsKey(Object key) {
		return this.entries.containsKey((String) key);
	}

	@Override
	public boolean containsValue(Object value) {
		throw new UnsupportedOperationException();
	}

	@Override
	public String get(Object key) {
		return this.entries.get(key);
	}

	@Override
	public String put(String key, String value) {
		return this.entries.put(key, StringUtils.trimToNull(value));
	}

	@Override
	public String remove(Object key) {
		return this.entries.remove((String) key);
	}

	@Override
	public void putAll(Map m) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void clear() {
		this.entries.clear();
	}

	@Override
	public Set keySet() {
		return this.entries.keySet();
	}

	@Override
	public Collection values() {
		throw new UnsupportedOperationException();
	}

	@Override
	public Set> entrySet() {
		return this.entries.entrySet();
	}

	@Override
	public String toString() {
		try {
			StringWriter out = new StringWriter();
			store(out, null);
			return out.toString();
		} catch (IOException ex) {
			throw new UncheckedIOException(ex);
		}
	}

	public void put(String key, boolean value) {
		this.entries.put(key, Boolean.toString(value));
	}

	public void put(String key, int value) {
		this.entries.put(key, Integer.toString(value));
	}

	public void put(String key, long value) {
		this.entries.put(key, Long.toString(value));
	}

	public void put(String key, float value) {
		this.entries.put(key, PropertiesConfig.format(value, Formatter.FLOAT));
	}

	public void put(String key, double value) {
		this.entries.put(key, PropertiesConfig.format(value, Formatter.DOUBLE));
	}

	public void put(String key, java.sql.Date value) {
		this.entries.put(key, PropertiesConfig.format(value, Formatter.SQL_DATE));
	}

	public void put(String key, java.sql.Timestamp value) {
		this.entries.put(key, PropertiesConfig.format(value, Formatter.SQL_DATETIME_24H));
	}

	private static class LinkedProperties extends Properties {
		private static final long serialVersionUID = 1;

		final Set keys = new LinkedHashSet<>();

		@Override
		public Set keySet() {
			return this.keys;
		}

		@Override
		public synchronized Enumeration keys() {
			return Collections.enumeration(this.keys);
		}

		@Override
		public synchronized Object put(Object key, Object value) {
			this.keys.add(key);
			return super.put(key, value);
		}
	}
}