Original Source Code.
- http://taapps-sourcecode-libraries.googlegroups.com/web/Obj.java
- Description
We sometimes need to check whether the java object is null before accessing the object to avoid occuring the NullPointerException, and then we check whether the object is empty if the object is not null as follows.String test="test";
This is a long and mistakable, and Obj.isNull method returns the value of true if the first argument object is null (not initialized),Obj.isNotNull(obj,true) method returns the value of !Obj.isNull(obj,true) like this.
....
if(test!=null && !test.equals("")){
....
}String test="test";
The second argument is used for checking the object value. The second argument is set to true, System checks whether the first argument object is empty or not if the first argument object is defined as String. If the first argument is defined as Object[], Map, or Collection, System checks the Object has any elements more than 1. If Object doesn't have any elements, Obj.isNull(obj,true) returns true.
....
if(Obj.isNull(test,true)){
....
} - Normal Object
Obj.isNull(normalObject,false) means if(normalObject==null) return true - Check String object
Obj.isNull(stringObject, true) means if(stringObject==null || stringObject.equals("")) return true. - Check Array
Obj.isNull(objects[], true) means if(objects==null || objects.length<1)> - Check Map Object
Obj.isNull(mapObject,true) means if(mapObject==null || mapObject.size()<1)> - Check Collection Object
Obj.isNull(listObject,true) means if(listObject==null || listObject.size()<1)>
- Description
For example, we have to close the opened stream or connection after we finished to use java.io.FileInputStream or java.sql.Connection like this.FileInputStream fis=null;
Before closing the FileInputStream, InputStreamReader, BufferedReader in finally block, we usually check whether the object is null or not, and then we close the opened object in try-catch exception handler to catch the Exception when closing the opened object.
InputStreamReader isr=null;
BufferedReader br =null;
try{
fis=new FileInputStream("/tmp/test.txt");
....
}catch(Exception e){
......
}finally{
if(br!=null){
try{
br.close();
}catch(Exception ignore){}
}
if(isr!=null){
try{
isr.close();
}catch(Exception ignore){}
}
if(fis!=null){
try{
br.close();
}catch(Exception e){}
}
}
These statements are troublesome and mistakable. We can change the statements described above into following. We don't need to check whether the object is null or not and catch the Exception occuring while closing the object.FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br =null;
try{
fis=new FileInputStream("/tmp/test.txt");
....
}catch(Exception e){
......
}finally{
Obj.close(fis);
Obj.close(isr);
Obj.close(br);
} We can use this Obj.close function for these class.
- java.io.Reader object
- java.io.InputStream
- java.io.OutputStream
- javax.imageio.ImageOutputStream
- javax.imageio.ImageInputStream
- javax.imageio.ImageWriter
- java.awt.Graphics
- java.io.Writer
- java.sql.Connection
- java.sql.Statement
- java.sql.ResultSet
- java.net.HttpURLConnection
- java.net.URLConnection
0 件のコメント:
コメントを投稿