From: Mateusz Warowny Date: Mon, 8 May 2023 14:24:27 +0000 (+0200) Subject: JAL-3954 Add value parsers for double, int and bool arguments X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=e14d2d9318abc1a96e4ddb6b2564a05a61879d9b;p=jalview.git JAL-3954 Add value parsers for double, int and bool arguments --- diff --git a/src/jalview/ws/params/simple/BooleanOption.java b/src/jalview/ws/params/simple/BooleanOption.java index 87e4ad1..8609ff0 100644 --- a/src/jalview/ws/params/simple/BooleanOption.java +++ b/src/jalview/ws/params/simple/BooleanOption.java @@ -24,6 +24,8 @@ import java.net.URL; import java.util.Arrays; import java.util.List; +import jalview.ws.params.ArgumentI; + public class BooleanOption extends Option { public static class Builder extends Option.Builder @@ -98,4 +100,10 @@ public class BooleanOption extends Option { this(name, description, label, isrequired, defValue, String.valueOf(true), link); } + + public static Boolean parseBoolean(ArgumentI argument) + { + return argument.getValue() != null && !argument.getValue().isEmpty() ? + true : false; + } } diff --git a/src/jalview/ws/params/simple/DoubleParameter.java b/src/jalview/ws/params/simple/DoubleParameter.java index 97c5fe1..f08f5fd 100644 --- a/src/jalview/ws/params/simple/DoubleParameter.java +++ b/src/jalview/ws/params/simple/DoubleParameter.java @@ -1,5 +1,6 @@ package jalview.ws.params.simple; +import jalview.ws.params.ArgumentI; import jalview.ws.params.ParameterI; import jalview.ws.params.ValueConstrainI; @@ -152,4 +153,28 @@ public class DoubleParameter extends Option implements ParameterI { return new DoubleParameter(this); } + + /** + * Return argument value as double or null if string value is null or empty. + * + * @param arg argument to extract value form + * @return argument value as double + */ + public static Double parseDouble(ArgumentI arg) + { + return arg.getValue() != null && !arg.getValue().isEmpty() ? + Double.parseDouble(arg.getValue()) : null; + } + + /** + * Return argument value as float or null if string value is null or empty. + * + * @param arg argument to extract value from + * @return value as float + */ + public static Float parseFloat(ArgumentI arg) + { + return arg.getValue() != null && !arg.getValue().isEmpty() ? + Float.parseFloat(arg.getValue()) : null; + } } diff --git a/src/jalview/ws/params/simple/IntegerParameter.java b/src/jalview/ws/params/simple/IntegerParameter.java index e154194..774c21f 100644 --- a/src/jalview/ws/params/simple/IntegerParameter.java +++ b/src/jalview/ws/params/simple/IntegerParameter.java @@ -20,6 +20,7 @@ */ package jalview.ws.params.simple; +import jalview.ws.params.ArgumentI; import jalview.ws.params.ParameterI; import jalview.ws.params.ValueConstrainI; @@ -165,4 +166,16 @@ public class IntegerParameter extends Option implements ParameterI return new IntegerParameter(this); } + /** + * Return argument value as int or null if string value is null or empty. + * + * @param arg argument to extract value from + * @return value as int + */ + public static Integer parseInt(ArgumentI arg) + { + return arg.getValue() != null && !arg.getValue().isEmpty() ? + Integer.parseInt(arg.getValue()) : null; + } + }