Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using GetterUtils and ParamUtils

For instance, when to use

GetterUtil.getBoolean()

and when

ParamUtil.getBoolean()?

Are both same, or is it expected to be used differently according to a parameter, a variable, etc? Can you give some examples for both?

like image 563
Whimusical Avatar asked Aug 16 '12 16:08

Whimusical


2 Answers

Both are util methods to avoid Null-Pointer Exceptions.

GetterUtil internally returns the default type and does the casting too. So in case where someone has passed a null value, it will return default value of the type.

Example:
Assume you have a String value "true", and you are expecting it will always be of type boolean. So you use GetterUtil.getBoolean("true") which will internally do the casting to boolen and return the value as boolean-true. Incase someone passes rubbish characters like "tr", it will be converted to boolean-false.

As mentioned ParamUtil does the same treatment with request parameters. ParamUtil internally uses the GetterUtil to have the above behaviour. It first retrieves the parameter (which always would be a string) and then passes it to GetterUtil.getType() method and in turn returns the proper type.

like image 110
Sandeep Nair Avatar answered Oct 05 '22 03:10

Sandeep Nair


GetterUtil and ParmUtil both are different classes.

GetterUtil is to get the default values for basic Java data types.

ParamUtil is to retrive the values(of primitive data types) from the HttpReqeust.

Check the source code here for these two classes here

For GetterUtil http://docs.liferay.com/portal/6.0/javadocs/src-html/com/liferay/portal/kernel/util/GetterUtil.html

For ParamUtil http://docs.liferay.com/portal/5.1/javadocs/portal-kernel/com/liferay/portal/kernel/util/ParamUtil.java.html

like image 21
ravikuwi Avatar answered Oct 05 '22 04:10

ravikuwi