Thursday 17 September 2015

isNumeric in Java.

Once in while in java we might have to check If a passed string to method is a valid numerical value. Below are some utility methods to accomplish this:

    Using java.text.NumberFormat.


    public static boolean isNumeric(final String str) {
        try {
            Double.parseDouble(str);
        } catch (final NumberFormatException | NullPointerException e) {
            return false;
        }

        return true;
    }

    JUnit Test Case:


        Assert.assertEquals(false, yourClass.isNumeric(null));
        Assert.assertEquals(false, yourClass.isNumeric(""));
        Assert.assertEquals(true, yourClass.isNumeric("2.74"));
        Assert.assertEquals(true, yourClass.isNumeric("2.745435E3"));
        Assert.assertEquals(true, yourClass.isNumeric("0"));
        Assert.assertEquals(true, yourClass.isNumeric("0.0f"));
        Assert.assertEquals(false, yourClass.isNumeric("NaN"));       


    Using Double.parseDouble.


     public static boolean isNumeric(final String string) {
        try {
            NumberFormat.getInstance().parse(string);
        } catch (ParseException | NullPointerException e) {
            return false;
        }

        return true;
    }

    JUnit Test Case:


        Assert.assertEquals(false, yourClass.isNumeric(null));
        Assert.assertEquals(false, yourClass.isNumeric(""));
        Assert.assertEquals(true, yourClass.isNumeric("2.74"));
        Assert.assertEquals(true, yourClass.isNumeric("2.745435E3"));
        Assert.assertEquals(true, yourClass.isNumeric("0"));
        Assert.assertEquals(true, yourClass.isNumeric("0.0f"));
        Assert.assertEquals(true, yourClass.isNumeric("NaN"));


As you guys might have noticed that I have an assert statement for both the methods which checks the method against string "NaN". The thing which makes this more interesting is that the method which uses NumberFormat returns false for this string while the method which uses Double.parseDouble(string) returns true.

You might say that the method which uses NumberFormat is correct because "NaN" is technically just a mere string value. Below is the description of how java treats "NaN" while doing floating-point operations :

"NaN" stands for "not a number". "Nan" is produced if a  floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided
by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.


That's all for now folks stay tuned for new posts !!!!!!!!

 
 
 



Saturday 5 September 2015

Jetty - Enabling request logs

Whenever you start jetty using jetty.sh shell script located inside jetty's bin folder, the jetty will start with the below logs enabled defined inside /opt/jetty/start.ini file (defined using xml files) by default:

etc/jetty.xml
etc/jetty-annotations.xml
etc/jetty-deploy.xml
etc/jetty-webapps.xml
etc/jetty-contexts.xml
etc/jetty-testrealm.xml

Below is the snapshot of the /opt/jetty/start.ini file to show complete set of configuration files:

#===========================================================
# Configuration files.
# For a full list of available configuration files do
#   java -jar start.jar --help
#-----------------------------------------------------------
#etc/jetty-jmx.xml
etc/jetty.xml
etc/jetty-annotations.xml
# etc/jetty-ssl.xml
# etc/jetty-requestlog.xml
etc/jetty-deploy.xml
#etc/jetty-overlay.xml
etc/jetty-webapps.xml
etc/jetty-contexts.xml
etc/jetty-testrealm.xml
#===========================================================

As highlighted in the above configuration the request log xml file is commented by default. We just have to uncomment the same and restart jetty to enable request logs and you should be able to see the request logs generated in the below directory:

/opt/jetty/logs

$ ls -lt
total 48
-rw-rw-r-- 1 jetty jetty  165 Aug 21 13:54 2015_08_21.request.log