2007年12月29日土曜日

[Java>Original]Useful Object Utility(resource close function to avoid resource leak, and null and empty checker)

I created useful Object Utility including some functions, resource close function to avoid resource leak, and null and empty checker.

Original Source Code.
  • http://taapps-sourcecode-libraries.googlegroups.com/web/Obj.java
Object null and empty checker
  • 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";
    ....
    if(test!=null && !test.equals("")){
    ....
    }
    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.
    String test="test";
    ....
    if(Obj.isNull(test,true)){
    ....
    }
    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.

    • 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)>
Resource close function to avoid resource leak
  • 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;
    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){}
    }
    }
    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.
    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.
    1. java.io.Reader object
    2. java.io.InputStream
    3. java.io.OutputStream
    4. javax.imageio.ImageOutputStream
    5. javax.imageio.ImageInputStream
    6. javax.imageio.ImageWriter
    7. java.awt.Graphics
    8. java.io.Writer
    9. java.sql.Connection
    10. java.sql.Statement
    11. java.sql.ResultSet
    12. java.net.HttpURLConnection
    13. java.net.URLConnection

0 件のコメント: