001 package org.kuali.maven.plugins.graph.tree; 002 003 import java.util.ArrayList; 004 import java.util.Arrays; 005 import java.util.Collection; 006 import java.util.Collections; 007 import java.util.List; 008 import java.util.Map; 009 010 import org.apache.commons.beanutils.BeanUtils; 011 import org.apache.commons.beanutils.PropertyUtils; 012 import org.apache.commons.collections15.list.SetUniqueList; 013 import org.apache.commons.collections15.map.ListOrderedMap; 014 import org.kuali.maven.plugins.graph.dot.GraphException; 015 016 /** 017 * General purpose utility methods 018 */ 019 public class Helper { 020 public static final String COMMA = ","; 021 public static final String EMPTY_STRING = ""; 022 023 public static final <T> Object getProperty(T bean, String property) { 024 try { 025 return PropertyUtils.getProperty(bean, property); 026 } catch (Exception e) { 027 throw new GraphException(e); 028 } 029 } 030 031 public static final <T> String toCSV(List<T> list) { 032 StringBuilder sb = new StringBuilder(); 033 for (int i = 0; i < list.size(); i++) { 034 if (i != 0) { 035 sb.append(COMMA); 036 } 037 sb.append(list.get(i)); 038 } 039 return sb.toString(); 040 } 041 042 public static final <T> ListOrderedMap<String, String> describe(T element, String... properties) { 043 return describe(element, Arrays.asList(properties)); 044 } 045 046 public static final <T> ListOrderedMap<String, String> describe(T element, List<String> properties) { 047 try { 048 @SuppressWarnings("unchecked") 049 Map<String, Object> map = BeanUtils.describe(element); 050 ListOrderedMap<String, String> orderedMap = new ListOrderedMap<String, String>(); 051 for (String property : properties) { 052 Object value = map.get(property); 053 if (value == null) { 054 continue; 055 } 056 orderedMap.put(property, value.toString()); 057 } 058 return orderedMap; 059 } catch (Exception e) { 060 throw new GraphException(e); 061 } 062 } 063 064 public static final <T> SetUniqueList<T> decorate() { 065 return SetUniqueList.decorate(new ArrayList<T>()); 066 } 067 068 public static final String[] toArray(List<String> strings) { 069 return strings.toArray(new String[strings.size()]); 070 } 071 072 public static final String toEmpty(String s) { 073 if (isBlank(s)) { 074 return EMPTY_STRING; 075 } else { 076 return s; 077 } 078 } 079 080 /** 081 * Null safe method for adding all the elements in "from" to "to" 082 */ 083 public static final <T> void addAll(Collection<T> to, Collection<T> from) { 084 if (!isEmpty(from)) { 085 to.addAll(from); 086 } 087 } 088 089 /** 090 * True if the collection is null or has no elements, false otherwise 091 */ 092 public static final boolean isEmpty(Collection<?> c) { 093 return c == null || c.size() == 0; 094 } 095 096 /** 097 * True if the string is null or whitespace only, false otherwise 098 */ 099 public static final boolean isBlank(String s) { 100 return s == null || s.trim().equals(EMPTY_STRING); 101 } 102 103 /** 104 * Return a List (never null) created from the csv string. Each csv token is trimmed before being added to the List. 105 * If the csv string is null or contains only whitespace an empty list is returned. 106 */ 107 public static final List<String> splitAndTrimCSVToList(String csv) { 108 if (isBlank(csv)) { 109 return Collections.emptyList(); 110 } 111 String[] tokens = csv.split(COMMA); 112 List<String> list = new ArrayList<String>(); 113 for (String token : tokens) { 114 list.add(token.trim()); 115 } 116 return list; 117 } 118 119 }