/*
 * Copyright (c) 2017, Dmitriy Shchekotin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package ru.silverhammer.common;

public final class Commons {

	private Commons() {}

	public static Number convertNumber(Number value, Class<?> target) {
		if (value != null) {
			if (target == Byte.class || target == byte.class) {
				return value.byteValue();
			} else if (target == Short.class || target == short.class) {
				return value.shortValue();
			} else if (target == Integer.class || target == int.class) {
				return value.intValue();
			} else if (target == Long.class || target == long.class) {
				return value.longValue();
			} else if (target == Float.class || target == float.class) {
				return value.floatValue();
			} else if (target == Double.class || target == double.class) {
				return value.doubleValue();
			}
		} else if (target.isPrimitive()) {
			if (target == byte.class) {
				return (byte) 0;
			} else if (target == short.class) {
				return (short) 0;
			} else if (target == int.class) {
				return (int) 0;
			} else if (target == long.class) {
				return (long) 0;
			} else if (target == float.class) {
				return (float) 0;
			} else if (target == double.class) {
				return (double) 0;
			}
		}
		return value;
	}
}

@@ -1404,16 +1404,75 @@
 ommon;%0A%0A
+import java.math.BigDecimal;%0Aimport java.math.BigInteger;%0A%0A
 public f
@@ -1604,24 +1604,62 @@
  != null) %7B%0A
+%09%09%09if (value.getClass() != target) %7B%0A%09
 %09%09%09if (targe
@@ -1693,32 +1693,33 @@
 = byte.class) %7B%0A
+%09
 %09%09%09%09return value
@@ -1727,32 +1727,33 @@
 byteValue();%0A%09%09%09
+%09
 %7D else if (targe
@@ -1789,32 +1789,33 @@
  short.class) %7B%0A
+%09
 %09%09%09%09return value
@@ -1824,32 +1824,33 @@
 hortValue();%0A%09%09%09
+%09
 %7D else if (targe
@@ -1890,32 +1890,33 @@
 nt.class) %7B%0A%09%09%09%09
+%09
 return value.int
@@ -1916,32 +1916,33 @@
 lue.intValue();%0A
+%09
 %09%09%09%7D else if (ta
@@ -1983,32 +1983,33 @@
 ng.class) %7B%0A%09%09%09%09
+%09
 return value.lon
@@ -2010,32 +2010,33 @@
 ue.longValue();%0A
+%09
 %09%09%09%7D else if (ta
@@ -2079,32 +2079,33 @@
 at.class) %7B%0A%09%09%09%09
+%09
 return value.flo
@@ -2111,24 +2111,25 @@
 oatValue();%0A
+%09
 %09%09%09%7D else if
@@ -2182,24 +2182,25 @@
 lass) %7B%0A%09%09%09%09
+%09
 return value
@@ -2215,16 +2215,290 @@
 alue();%0A
+%09%09%09%09%7D else if (target == BigInteger.class) %7B%0A%09%09%09%09%09return new BigInteger(Long.toString(value.longValue()));%0A%09%09%09%09%7D else if (target == BigDecimal.class) %7B%0A%09%09%09%09%09return value instanceof BigInteger ? new BigDecimal((BigInteger) value) : new BigDecimal(value.doubleValue());%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Add BigDecimal and BigInteger support