Skip to content
Snippets Groups Projects
Commit 41d0f6b4 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Added getDouble(Object) as utility method

parent 6bd9f5e1
No related branches found
No related tags found
No related merge requests found
......@@ -24,11 +24,14 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URLConnection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import no.nibio.vips.entity.WeatherObservation;
import no.nibio.vips.model.ConfigValidationException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
......@@ -145,4 +148,54 @@ public class ModelUtil {
return mapper.convertValue(unknownClassList, new TypeReference<List<WeatherObservation>>(){});
}
}
public Double getDouble(Object possibleNumber) throws ConfigValidationException
{
if(possibleNumber == null)
{
return null;
}
if(possibleNumber instanceof String)
{
try
{
return Double.valueOf((String) possibleNumber);
}
catch(NumberFormatException ex)
{
throw new ConfigValidationException(ex.getMessage());
}
}
if(possibleNumber instanceof Integer)
{
return ((Integer) possibleNumber).doubleValue();
}
if(possibleNumber instanceof Float)
{
return ((Float) possibleNumber).doubleValue();
}
if(possibleNumber instanceof Double)
{
return (Double) possibleNumber;
}
// Clutching at straws
if(possibleNumber instanceof BigInteger)
{
return ((BigInteger) possibleNumber).doubleValue();
}
if(possibleNumber instanceof BigDecimal)
{
return ((BigDecimal) possibleNumber).doubleValue();
}
// Out of options. Throw Exception
try
{
return (Double) possibleNumber;
}
catch(ClassCastException ex)
{
throw new ConfigValidationException(ex.getMessage());
}
}
}
......@@ -20,7 +20,10 @@ package no.nibio.vips.util;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import junit.framework.TestCase;
import no.nibio.vips.model.ConfigValidationException;
/**
*
......@@ -80,6 +83,6 @@ public class ModelUtilTest extends TestCase {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment