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