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

com.elephantdrummer.parser.ParserNumber Maven / Gradle / Ivy

There is a newer version: 1.2.5
Show newest version
package com.elephantdrummer.parser;

import java.math.BigDecimal;
import java.math.BigInteger;

import com.elephantdrummer.exception.ParserException;
import com.elephantdrummer.validator.ValidatorBigDecimal;



/**
 * Copyright 2018 Elephant Software Klaudiusz Wojtkowiak e-mail: [email protected]
 * 
 * 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. 
 */
public interface ParserNumber {
	
	public static Double toDouble(Number from) {
		if (from == null)return null;
		return from.doubleValue();
	}
	
	public static double toSmallDouble(Number from) {
		if (from == null)return 0;
		return from.doubleValue();
	}
	 
	public static Integer toInteger(Number from)  {
		if (from == null)return null;
		return from.intValue();
	}
	
	public static int toSmallInteger(Number from)  {
		if (from == null)return 0;
		return from.intValue();
	}
	
	public static Long toLong(Number from)  {
		if (from == null)return null;
		return from.longValue();
	}
	
	public static long toSmallLong(Number from)  {
		if (from == null)return 0;
		return from.longValue();
	}
	
	public static Short toShort(Number from)  {
		if (from == null)return null;
		return from.shortValue();
	}
	
	public static short toSmallShort(Number from)  {
		if (from == null)return 0;
		return from.shortValue();
	}
	
	public static Float toFloat(Number from) {
		if (from == null)return null;
		return from.floatValue();
	}
	
	public static float toSmallFloat(Number from) {
		if (from == null)return 0;
		return from.floatValue();
	}
	
	public static String toString(Number from) {
		if (from == null)return null;
		return from.toString();
	}
	
	public static BigDecimal toBigDecimal(Number from) {
		if (from == null)return null;
		BigDecimal retVal= new BigDecimal(from.toString());
		return ValidatorBigDecimal.validateScale(retVal);
	}

	
	public static  BigInteger toBigInteger(Number from) {
		if (from == null) return null;
		return BigInteger.valueOf(from.intValue());
	}
	
	
	public static Boolean toBooleanBig(Number from)  {
		if (from==null) return null;
		return toBooleanSmall(from,false);
	}
	
	public static Boolean toBoolean(Number from,Boolean inCaseOfNull)  {
		if (from==null) return inCaseOfNull;
		return toBooleanSmall(from,false);
	}
	
	public static boolean toBooleanSmall(Number data,boolean inCaseOfNull) {
		if (data==null) return inCaseOfNull;
		if (data.intValue()==1){
			return true;
		}else if (data.intValue()==0){
			return false;
		}else{
			throw new ParserException("Value '"+data+"' can not be converted to boolean");
		}
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy