View Javadoc

1   package edu.drexel.cms.commons;
2   
3   import java.security.NoSuchAlgorithmException;
4   import java.text.ParseException;
5   import java.text.SimpleDateFormat;
6   import java.util.Collection;
7   import java.util.Date;
8   
9   import org.apache.commons.collections.CollectionUtils;
10  import org.apache.commons.lang.StringUtils;
11  
12  public class CommonsUtils{
13    private CommonsUtils(){};
14    public static String encrypt(String x) throws NoSuchAlgorithmException 
15    {
16       java.security.MessageDigest d =null;
17       d = java.security.MessageDigest.getInstance("SHA-1");
18       d.reset();
19       d.update(x.getBytes());
20       return  new String(d.digest());
21    }
22    /***
23     * Recursive is empty
24     * @param <T>
25     * @param collection
26     * @return
27     */
28    @SuppressWarnings("unchecked")
29    public static <T extends Collection>  boolean IsDeepEmpty(T collection){
30      if(CollectionUtils.isEmpty(collection)){
31        return true;
32      }
33      for(Object o : collection){
34        if (o instanceof Collection) {
35          Collection c = (Collection) o;
36          if(!IsDeepEmpty(c)){
37            return false;
38          }
39        }else{
40          return false; //found a non empty object, dont care anymore
41        }
42      }
43      return true;
44    }
45    public static Date getDate(String dateString, String patternString) {
46      if (StringUtils.isEmpty(dateString)) {
47        return null;
48      }
49      try {
50        return new SimpleDateFormat(patternString).parse(dateString);
51      } catch (ParseException e) {}
52      return null;
53    }
54  }