|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
CollectionUtil.java | 30% | 47.6% | 77.8% | 50% |
|
1 |
package com.diasparsoftware.java.util;
|
|
2 |
|
|
3 |
import java.util.*;
|
|
4 |
|
|
5 |
import org.apache.commons.collections.*;
|
|
6 |
|
|
7 |
public class CollectionUtil { |
|
8 |
|
|
9 |
/**
|
|
10 |
* Allows you to detect whether any item in a collection satisfies
|
|
11 |
* some acceptance criterion.
|
|
12 |
*
|
|
13 |
* @param collection
|
|
14 |
* The collection to search in
|
|
15 |
* @param predicate
|
|
16 |
* A predicate that should evaluate to <code>true</code>
|
|
17 |
* for the matching object.
|
|
18 |
* @return
|
|
19 |
*/
|
|
20 | 44 |
public static boolean detect( |
21 |
Collection collection, |
|
22 |
Predicate predicate) { |
|
23 |
|
|
24 | 44 |
return (CollectionUtils.find(collection, predicate) != null); |
25 |
} |
|
26 |
|
|
27 |
/**
|
|
28 |
* Searches a collection of <code>String</code> s for the
|
|
29 |
* specified search string, but ignoring case.
|
|
30 |
*
|
|
31 |
* @param stringCollection
|
|
32 |
* A collection of <code>String</code> s
|
|
33 |
* @param searchString
|
|
34 |
* The string to forEachDoIgnoreExceptionsearchString
|
|
35 |
* </code> in here if we ignore case?"
|
|
36 |
*/
|
|
37 | 44 |
public static boolean stringCollectionContainsIgnoreCase( |
38 |
Collection stringCollection, |
|
39 |
final String searchString) { |
|
40 |
|
|
41 | 44 |
return CollectionUtil
|
42 |
.detect(stringCollection, new Predicate() {
|
|
43 | 55 |
public boolean evaluate(Object parameters) { |
44 | 55 |
String eachString = (String) parameters; |
45 | 44 |
return eachString.equalsIgnoreCase(searchString);
|
46 |
} |
|
47 |
}); |
|
48 |
} |
|
49 |
|
|
50 |
/**
|
|
51 |
* Executes <code>closure</code> for each element in the
|
|
52 |
* specified collection, ignoring any thrown exceptions. This
|
|
53 |
* method is designed to be used by the <code>JdbcResourceRegistry</code>:
|
|
54 |
* at cleanup time, you can't recover from exceptions anyway.
|
|
55 |
*
|
|
56 |
* @param collection
|
|
57 |
* @param closure
|
|
58 |
*/
|
|
59 | 0 |
public static void forEachDoIgnoreException( |
60 |
Collection collection, |
|
61 |
ExceptionalClosure closure) { |
|
62 |
|
|
63 | 0 |
for (Iterator i = collection.iterator(); i.hasNext();) {
|
64 | 0 |
Object each = (Object) i.next(); |
65 | 0 |
try {
|
66 | 0 |
closure.execute(each); |
67 |
} |
|
68 |
catch (Exception ignored) {
|
|
69 |
} |
|
70 |
} |
|
71 |
} |
|
72 |
|
|
73 |
/**
|
|
74 |
* The first exception stops the iteration.
|
|
75 |
*
|
|
76 |
* @param collection
|
|
77 |
* @param closure
|
|
78 |
* @throws Exception
|
|
79 |
*/
|
|
80 | 220 |
public static void forEachDo( |
81 |
Collection collection, |
|
82 |
ExceptionalClosure closure) |
|
83 |
throws Exception {
|
|
84 |
|
|
85 | 220 |
for (Iterator i = collection.iterator(); i.hasNext();) {
|
86 | 627 |
Object each = (Object) i.next(); |
87 | 627 |
closure.execute(each); |
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
/**
|
|
92 |
* Execute the specified closure for each item in the specified
|
|
93 |
* collection.
|
|
94 |
*
|
|
95 |
* @param collection
|
|
96 |
* @param closure
|
|
97 |
* @throws Exception
|
|
98 |
*/
|
|
99 | 11 |
public static void forEachDo( |
100 |
Collection collection, |
|
101 |
Closure closure) { |
|
102 |
|
|
103 | 11 |
for (Iterator i = collection.iterator(); i.hasNext();) {
|
104 | 0 |
Object each = (Object) i.next(); |
105 | 0 |
closure.execute(each); |
106 |
} |
|
107 |
} |
|
108 |
|
|
109 |
/**
|
|
110 |
* Execute the specified closure for each item in the specified
|
|
111 |
* collection.
|
|
112 |
*
|
|
113 |
* @param collection
|
|
114 |
* @param closure
|
|
115 |
* @throws Exception
|
|
116 |
*/
|
|
117 | 11 |
public static void forEachDo(Map map, MapEntryClosure closure) { |
118 | 11 |
forEachDo(map.entrySet(), closure); |
119 |
} |
|
120 |
|
|
121 |
/**
|
|
122 |
* Execute the specified closure for each item in the specified
|
|
123 |
* collection.
|
|
124 |
*
|
|
125 |
* @param collection
|
|
126 |
* @param closure
|
|
127 |
* @throws Exception
|
|
128 |
*/
|
|
129 | 44 |
public static void forEachDo( |
130 |
Map map, |
|
131 |
ExceptionalMapEntryClosure closure) |
|
132 |
throws Exception {
|
|
133 |
|
|
134 | 44 |
forEachDo(map.entrySet(), closure); |
135 |
} |
|
136 |
|
|
137 |
/**
|
|
138 |
* Selects the first object that the <code>selector</code>
|
|
139 |
* accepts, or <code>null</code> if the selector rejects
|
|
140 |
* them all.
|
|
141 |
*
|
|
142 |
* @param set
|
|
143 |
* @param selector
|
|
144 |
* @return
|
|
145 |
*/
|
|
146 | 0 |
public static Object select(Set set, Selector selector) { |
147 | 0 |
for (Iterator i = set.iterator(); i.hasNext();) {
|
148 | 0 |
Object element = (Object) i.next(); |
149 | 0 |
if (selector.accept(element))
|
150 | 0 |
return element;
|
151 |
} |
|
152 |
|
|
153 | 0 |
return null; |
154 |
} |
|
155 |
} |
|
156 |
|
|