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

com.generallycloud.baseio.common.FixedProperties Maven / Gradle / Ivy

/*
 * Copyright 2015-2017 GenerallyCloud.com
 *  
 * 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.generallycloud.baseio.common;

import java.util.Properties;

public class FixedProperties extends Properties{
	
	private static final long	serialVersionUID	= 1L;

	public FixedProperties loadString(String content){
		
		if (StringUtil.isNullOrBlank(content)) {
			return this;
		}
		
		String [] lines = content.split("\n");
		
		for(String line :lines){
			insertOneRow(line);
		}
		
		return this;
	}
	
	private void insertOneRow(String line){
		
		if (StringUtil.isNullOrBlank(line)) {
			return;
		}
		
		int index = line.indexOf("=");
		
		if (index == -1) {
			return;
		}
		
		String key = line.substring(0,index);
		String value = line.substring(index+1,line.length());
		
		key = trim(key);
		value = trim(value);
		
		put(key, value);
	}
	
	private String trim(String value){
		return value.trim().replace("\r", "").replace("\t", "");
	}
	
	public boolean getBooleanProperty(String key) {
		return getBooleanProperty(key, false);
	}

	public boolean getBooleanProperty(String key, boolean defaultValue) {
		String temp = getProperty(key);
		if (StringUtil.isNullOrBlank(temp)) {
			return defaultValue;
		}
		return Boolean.valueOf(temp);
	}

	public double getDoubleProperty(String key) {
		return getDoubleProperty(key, 0);
	}

	public double getDoubleProperty(String key, double defaultValue) {
		String temp = getProperty(key);
		if (StringUtil.isNullOrBlank(temp)) {
			return defaultValue;
		}
		return Double.valueOf(temp);
	}

	public int getIntegerProperty(String key) {
		return getIntegerProperty(key, 0);
	}

	public int getIntegerProperty(String key, int defaultValue) {
		String temp = getProperty(key);
		if (StringUtil.isNullOrBlank(temp)) {
			return defaultValue;
		}
		return Integer.valueOf(temp);
	}

	public long getLongProperty(String key) {
		return getLongProperty(key, 0);
	}

	public long getLongProperty(String key, long defaultValue) {
		String temp = getProperty(key);
		if (StringUtil.isNullOrBlank(temp)) {
			return defaultValue;
		}
		return Long.valueOf(temp);
	}

	public String getPropertyNoBlank(String key) throws PropertiesException {
		String value = getProperty(key);
		if (StringUtil.isNullOrBlank(value)) {
			throw new PropertiesException("property " + key + " is empty");
		}
		return value;
	}
	
	class PropertiesException extends Exception {

		private static final long serialVersionUID = 1L;

		public PropertiesException() {
		}

		public PropertiesException(String message) {
			super(message);
		}

		public PropertiesException(String message, Throwable cause) {
			super(message, cause);
		}
	}
	
	
	public static void main(String[] args) {
		
		FixedProperties p = new FixedProperties();
		
		p.insertOneRow("aaa=bbb");
		
		System.out.println(p.get("aaa"));
		
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy