001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026:
027: import java.io.BufferedReader;
028: import java.io.File;
029: import java.io.FileReader;
030: import java.io.IOException;
031:
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Comparator;
035: import java.util.Enumeration;
036: import java.util.Iterator;
037: import java.util.List;
038: import java.util.Set;
039: import java.util.TreeSet;
040:
041: /**
042: * <a href="ListUtil.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Brian Wing Shun Chan
045: *
046: */
047: public class ListUtil {
048:
049: public static List copy(List master) {
050: if (master == null) {
051: return null;
052: }
053:
054: List copy = new ArrayList(master.size());
055:
056: copy(master, copy);
057:
058: return copy;
059: }
060:
061: public static void copy(List master, List copy) {
062: if ((master == null) || (copy == null)) {
063: return;
064: }
065:
066: copy.clear();
067:
068: Iterator itr = master.iterator();
069:
070: while (itr.hasNext()) {
071: Object obj = itr.next();
072:
073: copy.add(obj);
074: }
075: }
076:
077: public static void distinct(List list) {
078: distinct(list, null);
079: }
080:
081: public static void distinct(List list, Comparator comparator) {
082: if ((list == null) || (list.size() == 0)) {
083: return;
084: }
085:
086: Set set = null;
087:
088: if (comparator == null) {
089: set = new TreeSet();
090: } else {
091: set = new TreeSet(comparator);
092: }
093:
094: Iterator itr = list.iterator();
095:
096: while (itr.hasNext()) {
097: Object obj = itr.next();
098:
099: if (set.contains(obj)) {
100: itr.remove();
101: } else {
102: set.add(obj);
103: }
104: }
105: }
106:
107: public static List fromArray(Object[] array) {
108: if ((array == null) || (array.length == 0)) {
109: return new ArrayList();
110: }
111:
112: List list = new ArrayList(array.length);
113:
114: for (int i = 0; i < array.length; i++) {
115: list.add(array[i]);
116: }
117:
118: return list;
119: }
120:
121: public static List fromCollection(Collection c) {
122: if ((c != null) && (c instanceof List)) {
123: return (List) c;
124: }
125:
126: if ((c == null) || (c.size() == 0)) {
127: return new ArrayList();
128: }
129:
130: List list = new ArrayList(c.size());
131:
132: Iterator itr = c.iterator();
133:
134: while (itr.hasNext()) {
135: list.add(itr.next());
136: }
137:
138: return list;
139: }
140:
141: public static List fromEnumeration(Enumeration enu) {
142: List list = new ArrayList();
143:
144: while (enu.hasMoreElements()) {
145: Object obj = enu.nextElement();
146:
147: list.add(obj);
148: }
149:
150: return list;
151: }
152:
153: public static List fromFile(String fileName) throws IOException {
154: return fromFile(new File(fileName));
155: }
156:
157: public static List fromFile(File file) throws IOException {
158: List list = new ArrayList();
159:
160: BufferedReader br = new BufferedReader(new FileReader(file));
161:
162: String s = StringPool.BLANK;
163:
164: while ((s = br.readLine()) != null) {
165: list.add(s);
166: }
167:
168: br.close();
169:
170: return list;
171: }
172:
173: public static List fromString(String s) {
174: return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
175: }
176:
177: public static List subList(List list, int begin, int end) {
178: List newList = new ArrayList();
179:
180: int normalizedSize = list.size() - 1;
181:
182: if ((begin < 0) || (begin > normalizedSize) || (end < 0)
183: || (begin > end)) {
184:
185: return newList;
186: }
187:
188: for (int i = begin; i < end && i <= normalizedSize; i++) {
189: newList.add(list.get(i));
190: }
191:
192: return newList;
193: }
194:
195: public static String toString(List list, String param) {
196: return toString(list, param, StringPool.COMMA);
197: }
198:
199: public static String toString(List list, String param,
200: String delimiter) {
201: StringMaker sm = new StringMaker();
202:
203: for (int i = 0; i < list.size(); i++) {
204: Object bean = list.get(i);
205:
206: Object value = BeanUtil.getObject(bean, param);
207:
208: if (value == null) {
209: value = StringPool.BLANK;
210: }
211:
212: sm.append(value.toString());
213:
214: if ((i + 1) != list.size()) {
215: sm.append(delimiter);
216: }
217: }
218:
219: return sm.toString();
220: }
221:
222: }
|