Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
V
VIPSCommon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VIPS
VIPSCommon
Commits
41d0f6b4
Commit
41d0f6b4
authored
3 years ago
by
Tor-Einar Skog
Browse files
Options
Downloads
Patches
Plain Diff
Added getDouble(Object) as utility method
parent
6bd9f5e1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/no/nibio/vips/util/ModelUtil.java
+53
-0
53 additions, 0 deletions
src/main/java/no/nibio/vips/util/ModelUtil.java
src/test/java/no/nibio/vips/util/ModelUtilTest.java
+4
-1
4 additions, 1 deletion
src/test/java/no/nibio/vips/util/ModelUtilTest.java
with
57 additions
and
1 deletion
src/main/java/no/nibio/vips/util/ModelUtil.java
+
53
−
0
View file @
41d0f6b4
...
...
@@ -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
());
}
}
}
This diff is collapsed.
Click to expand it.
src/test/java/no/nibio/vips/util/ModelUtilTest.java
+
4
−
1
View file @
41d0f6b4
...
...
@@ -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 {
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment