01: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
02: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
03: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
04: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
05: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
06: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
07: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
08: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
09: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
11: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
12: // POSSIBILITY OF SUCH DAMAGE.
13: //
14: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
15: package com.metaboss.util;
16:
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.Iterator;
20: import java.util.List;
21:
22: /** Set of useful utilites to do with collections */
23: public class CollectionUtils {
24: /** The interface used to filter elements of the collections */
25: public static interface Filter {
26: /** Tests given object and returns true if this objec t can pass through the filter */
27: public boolean canPassThrough(Object pObject);
28: }
29:
30: // Returns new collection which contains only objects which passed the filter
31: public static Collection filter(Collection pSourceCollection,
32: Filter pFilter) {
33: if (pSourceCollection == null || pSourceCollection.isEmpty())
34: return pSourceCollection;
35: ArrayList lFilteredList = new ArrayList();
36: for (Iterator lIter = pSourceCollection.iterator(); lIter
37: .hasNext();) {
38: Object lNextObject = lIter.next();
39: if (pFilter.canPassThrough(lNextObject))
40: lFilteredList.add(lNextObject);
41: }
42: return lFilteredList;
43: }
44:
45: // Returns new list which contains only objects which passed the filter
46: public static List filter(List pSourceList, Filter pFilter) {
47: if (pSourceList == null || pSourceList.isEmpty())
48: return pSourceList;
49: ArrayList lFilteredList = new ArrayList();
50: for (Iterator lIter = pSourceList.iterator(); lIter.hasNext();) {
51: Object lNextObject = lIter.next();
52: if (pFilter.canPassThrough(lNextObject))
53: lFilteredList.add(lNextObject);
54: }
55: return lFilteredList;
56: }
57:
58: /** Converts elements of the collection into string which can be used in debug */
59: public static String toDebugString(Collection pCollection) {
60: StringBuffer lBuffer = new StringBuffer("<");
61: if (pCollection == null)
62: lBuffer.append("null");
63: else if (pCollection.isEmpty())
64: lBuffer.append("empty");
65: else {
66: for (Iterator lIter = pCollection.iterator();;) {
67: lBuffer.append("[");
68: lBuffer.append(lIter.next().toString());
69: lBuffer.append("]");
70: if (!lIter.hasNext())
71: break;
72: lBuffer.append(",");
73: }
74: }
75: lBuffer.append(">");
76: return lBuffer.toString();
77: }
78:
79: /** Checks if container contains any of the specified objects
80: *
81: * @param pContainer the collection of objects to be used as a container
82: * @param pObjectsToTest the collection of objects to test the container for
83: * @return true if container contains at least one of the objects to test
84: */
85: public static boolean containsAny(Collection pContainer,
86: Collection pObjectsToTest) {
87: if (pContainer != null && pContainer.isEmpty() == false
88: && pObjectsToTest != null
89: && pObjectsToTest.isEmpty() == false) {
90: for (Iterator lObjectsToTestIter = pObjectsToTest
91: .iterator(); lObjectsToTestIter.hasNext();) {
92: Object lObjectToTest = lObjectsToTestIter.next();
93: if (pContainer.contains(lObjectToTest))
94: return true;
95: }
96: }
97: return false;
98: }
99: }
|