Changeset 1286

Show
Ignore:
Timestamp:
03/07/09 20:06:15 (3 years ago)
Author:
amandel
Message:

New utility methods.

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/java/org/jcoderz/commons/util/FileUtils.java

    r1011 r1286  
    424424      } 
    425425   } 
     426 
     427   /** 
     428    * Creates the given directories. 
     429    * 
     430    * @param dirs the directories to be created. 
     431    * @throws IOException the directories could not be created. 
     432    * @see File#mkdirs() 
     433    */ 
     434   public static void mkdirs (File dirs) 
     435         throws IOException 
     436   { 
     437      if (!dirs.exists() || !dirs.isDirectory()) 
     438      { 
     439         if (!dirs.mkdirs()) 
     440         { 
     441            throw new IOException("Failed to create directories " + dirs); 
     442         } 
     443      } 
     444   } 
     445 
     446   /** 
     447    * Creates the given directory. 
     448    * 
     449    * @param dir the directory to be created. 
     450    * @throws IOException the directory could not be created. 
     451    * @see File#mkdir() 
     452    */ 
     453   public static void mkdir (File dir) 
     454         throws IOException 
     455   { 
     456      if (!dir.exists() || !dir.isDirectory()) 
     457      { 
     458         if (!dir.mkdir()) 
     459         { 
     460            throw new IOException("Failed to create directory " + dir); 
     461         } 
     462      } 
     463   } 
    426464} 
  • trunk/src/java/org/jcoderz/commons/util/ObjectUtil.java

    r1011 r1286  
    136136      return obj == null ? null : obj.toString(); 
    137137   } 
     138 
     139   /** 
     140    * Returns the string representation of the object or 
     141    * an empty string if the object is null. 
     142    * @param obj the object to be converted or <code>null</code>. 
     143    * @return the string representation of the object or 
     144    * an empty string if the object is null. 
     145    */ 
     146   public static String toStringOrEmpty (Object obj) 
     147   { 
     148      return obj == null ? "" : obj.toString(); 
     149   } 
    138150} 
  • trunk/test/java/org/jcoderz/commons/util/ObjectUtilTest.java

    r1011 r1286  
    6969            "foo", ObjectUtil.toString("foo")); 
    7070   } 
     71 
     72   /** Tests the {@link ObjectUtil#toStringOrEmpty(Object)} method. */ 
     73   public void testToStringOrEmpty () 
     74   { 
     75      assertEquals("ObjectUtil.toStringOrEmpty(null) should return \"\".", 
     76            "", ObjectUtil.toStringOrEmpty(null)); 
     77      assertEquals("ObjectUtil.toStringOrEmpty(\"foo\") should return \"foo\".", 
     78            "foo", ObjectUtil.toStringOrEmpty("foo")); 
     79   } 
    7180}