1 19 package gcc.util; 20 21 import java.util.*; 22 23 public class ListUtil 24 { 25 29 public static void addIfNotPresent(List list, Object item) 30 { 31 if (! list.contains(item)) 32 { 33 list.add(item); 34 } 35 } 36 37 public static ArrayList getCommaSeparatedList(String arg) 38 { 39 return getListWithSeparator(arg, ","); 40 } 41 42 public static ArrayList getPathList(String arg) 43 { 44 return getListWithSeparator(arg, java.io.File.pathSeparator); 45 } 46 47 public static ArrayList getSpaceSeparatedList(String arg) 48 { 49 return getListWithSeparator(arg.replace('\t', ' ').replace('\r', ' ').replace('\n', ' '), " "); 50 } 51 52 public static ArrayList getListWithSeparator(String text, String separator) 53 { 54 ArrayList list = new ArrayList(); 55 int n = text.length(); 56 StringBuffer item = new StringBuffer(); 57 char endQuote = 0; 58 for (int i = 0; i < n; i++) 59 { 60 if (endQuote == 0 && text.startsWith(separator, i)) 61 { 62 add(list, item); 63 i += separator.length() - 1; 64 } 65 else 66 { 67 char c = text.charAt(i); 68 item.append(c); 69 if (endQuote != 0) 70 { 71 if (c == endQuote) 72 { 73 endQuote = 0; 74 } 75 } 76 else if (c == '\'' || c == '\"') 77 { 78 endQuote = c; 79 } 80 } 81 } 82 add(list, item); 83 return list; 84 } 85 86 public static String formatCommaSeparatedList(Collection list) 87 { 88 return formatListWithSeparator(list, ","); 89 } 90 91 public static String formatSpaceSeparatedList(Collection list) 92 { 93 return formatListWithSeparator(list, " "); 94 } 95 96 public static String formatListWithSeparator(Collection list, String separator) 97 { 98 StringBuffer buffer = new StringBuffer(); 99 for (Iterator i = list.iterator(); i.hasNext();) 100 { 101 Object item = i.next(); 102 if (buffer.length() != 0) 103 { 104 buffer.append(separator); 105 } 106 buffer.append(item.toString()); 107 } 108 return buffer.toString(); 109 } 110 111 public static ArrayList getArrayList(Object[] array) 112 { 113 int n = array.length; 114 ArrayList list = new ArrayList(n); 115 for (int i = 0; i < n; i++) 116 { 117 list.add(array[i]); 118 } 119 return list; 120 } 121 122 public static String[] getStringArray(List list) 123 { 124 int n = list.size(); 125 String[] array = new String[n]; 126 int i = 0; 127 for (Iterator j = list.iterator(); j.hasNext(); i++) 128 { 129 String s = (String)j.next(); 130 array[i] = s; 131 } 132 return array; 133 } 134 135 public static void printAll(java.io.PrintStream out, String rowPrefix, Collection values) 136 { 137 for (Iterator i = values.iterator(); i.hasNext();) 138 { 139 Object value = i.next(); 140 if (rowPrefix != null) 141 { 142 out.print(rowPrefix); 143 } 144 out.println(value); 145 } 146 } 147 148 152 private static void add(List list, StringBuffer itemBuffer) 153 { 154 String item = itemBuffer.toString().trim(); 155 if (item.length() != 0) 156 { 157 list.add(item); 158 } 159 itemBuffer.setLength(0); 160 } 161 } 162 | Popular Tags |