KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > ListUtil


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25
26 public class ListUtil {
27     // -----------------------------------------------------------------------
28
// public methods
29
// -----------------------------------------------------------------------
30

31     public static void addIfNotPresent(List JavaDoc list, Object JavaDoc item) {
32         if (!list.contains(item)) {
33             list.add(item);
34         }
35     }
36
37     public static ArrayList JavaDoc getCommaSeparatedList(String JavaDoc arg) {
38         return getListWithSeparator(arg, ",");
39     }
40
41     public static ArrayList JavaDoc getPathList(String JavaDoc arg) {
42         return getListWithSeparator(arg, java.io.File.pathSeparator);
43     }
44
45     public static ArrayList JavaDoc getSpaceSeparatedList(String JavaDoc arg) {
46         return getListWithSeparator(arg.replace('\t', ' ').replace('\r', ' ').replace('\n', ' '), " ");
47     }
48
49     public static ArrayList JavaDoc getListWithSeparator(String JavaDoc text, String JavaDoc separator) {
50         ArrayList JavaDoc list = new ArrayList JavaDoc();
51         int n = text.length();
52         StringBuffer JavaDoc item = new StringBuffer JavaDoc();
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 JavaDoc formatCommaSeparatedList(Collection JavaDoc list) {
75         return formatListWithSeparator(list, ",");
76     }
77
78     public static String JavaDoc formatSpaceSeparatedList(Collection JavaDoc list) {
79         return formatListWithSeparator(list, " ");
80     }
81
82     public static String JavaDoc formatListWithSeparator(Collection JavaDoc list, String JavaDoc separator) {
83         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
84         for (Iterator JavaDoc i = list.iterator(); i.hasNext();) {
85             Object JavaDoc 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 JavaDoc getArrayList(Object JavaDoc[] array) {
95         int n = array.length;
96         ArrayList JavaDoc list = new ArrayList JavaDoc(n);
97         for (int i = 0; i < n; i++) {
98             list.add(array[i]);
99         }
100         return list;
101     }
102
103     public static String JavaDoc[] getStringArray(List JavaDoc list) {
104         int n = list.size();
105         String JavaDoc[] array = new String JavaDoc[n];
106         int i = 0;
107         for (Iterator JavaDoc j = list.iterator(); j.hasNext(); i++) {
108             String JavaDoc s = (String JavaDoc) j.next();
109             array[i] = s;
110         }
111         return array;
112     }
113
114     public static void printAll(java.io.PrintStream JavaDoc out, String JavaDoc rowPrefix, Collection JavaDoc values) {
115         for (Iterator JavaDoc i = values.iterator(); i.hasNext();) {
116             Object JavaDoc value = i.next();
117             if (rowPrefix != null) {
118                 out.print(rowPrefix);
119             }
120             out.println(value);
121         }
122     }
123
124     // -----------------------------------------------------------------------
125
// private methods
126
// -----------------------------------------------------------------------
127

128     private static void add(List JavaDoc list, StringBuffer JavaDoc itemBuffer) {
129         String JavaDoc item = itemBuffer.toString().trim();
130         if (item.length() != 0) {
131             list.add(item);
132         }
133         itemBuffer.setLength(0);
134     }
135 }
136
Popular Tags