|
|||||||||||||||||||
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 | |||||||||||||||
HiveMind.java | 90.9% | 93.9% | 90.9% | 92.4% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind;
|
|
16 |
|
|
17 |
import java.util.Collection;
|
|
18 |
import java.util.List;
|
|
19 |
|
|
20 |
/**
|
|
21 |
* Static utility class for HiveMind.
|
|
22 |
*
|
|
23 |
* @author Howard Lewis Ship
|
|
24 |
*/
|
|
25 |
public final class HiveMind |
|
26 |
{ |
|
27 |
/**
|
|
28 |
* The full id of the {@link org.apache.hivemind.service.ThreadEventNotifier}
|
|
29 |
* service.
|
|
30 |
*/
|
|
31 |
public static final String THREAD_EVENT_NOTIFIER_SERVICE = "hivemind.ThreadEventNotifier"; |
|
32 |
|
|
33 | 0 |
private HiveMind()
|
34 |
{ |
|
35 |
// Prevent instantiation
|
|
36 |
} |
|
37 |
|
|
38 |
/**
|
|
39 |
* Checks that the number of parameters provided to a service implementation factory
|
|
40 |
* matches the expected number.
|
|
41 |
*
|
|
42 |
* @param extensionPointId the id of the service implementation factory (used in the error message)
|
|
43 |
* @param parameters the actual parameters
|
|
44 |
* @param expectedCount the expected number of parameters (usually, 1)
|
|
45 |
* @throws ApplicationRuntimeException if the actual and expected counts differ.
|
|
46 |
*/
|
|
47 | 234 |
public static void checkFactoryParameterCount( |
48 |
String extensionPointId, |
|
49 |
List parameters, |
|
50 |
int expectedCount)
|
|
51 |
{ |
|
52 | 234 |
if (parameters.size() != expectedCount)
|
53 | 1 |
throw new ApplicationRuntimeException( |
54 |
HiveMindMessages.wrongFactoryParameterCount( |
|
55 |
extensionPointId, |
|
56 |
expectedCount, |
|
57 |
parameters.size())); |
|
58 |
} |
|
59 |
|
|
60 | 4 |
public static ApplicationRuntimeException createRegistryShutdownException() |
61 |
{ |
|
62 | 4 |
return new ApplicationRuntimeException(HiveMindMessages.registryShutdown()); |
63 |
} |
|
64 |
|
|
65 |
/**
|
|
66 |
* Selects the first {@link Location} in an array of objects.
|
|
67 |
* Skips over nulls. The objects may be instances of
|
|
68 |
* Location or {@link Locatable}. May return null
|
|
69 |
* if no Location can be found.
|
|
70 |
*/
|
|
71 |
|
|
72 | 97 |
public static Location findLocation(Object[] locations) |
73 |
{ |
|
74 | 97 |
for (int i = 0; i < locations.length; i++) |
75 |
{ |
|
76 | 256 |
Object location = locations[i]; |
77 |
|
|
78 | 256 |
Location result = getLocation(location); |
79 |
|
|
80 | 256 |
if (result != null) |
81 | 20 |
return result;
|
82 |
|
|
83 |
} |
|
84 |
|
|
85 | 77 |
return null; |
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* Extracts a location from an object, checking to see if it
|
|
90 |
* implement {@link Location} or {@link Locatable}.
|
|
91 |
*
|
|
92 |
* @returns the Location, or null if it can't be found
|
|
93 |
*/
|
|
94 | 290 |
public static Location getLocation(Object object) |
95 |
{ |
|
96 | 290 |
if (object == null) |
97 | 197 |
return null; |
98 |
|
|
99 | 93 |
if (object instanceof Location) |
100 | 12 |
return (Location) object;
|
101 |
|
|
102 | 81 |
if (object instanceof Locatable) |
103 |
{ |
|
104 | 36 |
Locatable locatable = (Locatable) object; |
105 |
|
|
106 | 36 |
return locatable.getLocation();
|
107 |
} |
|
108 |
|
|
109 | 45 |
return null; |
110 |
} |
|
111 |
|
|
112 |
/**
|
|
113 |
* Invokes {@link #getLocation(Object)}, then translate the result
|
|
114 |
* to a string value, or "unknown location" if null.
|
|
115 |
*
|
|
116 |
*/
|
|
117 | 4 |
public static String getLocationString(Object object) |
118 |
{ |
|
119 | 4 |
Location l = getLocation(object); |
120 |
|
|
121 | 4 |
if (l != null) |
122 | 0 |
return l.toString();
|
123 |
|
|
124 | 4 |
return HiveMindMessages.unknownLocation();
|
125 |
} |
|
126 |
|
|
127 |
/**
|
|
128 |
* Returns true if the string is null, empty, or contains only whitespace.
|
|
129 |
*
|
|
130 |
* <p>
|
|
131 |
* The commons-lang library provides a version of this, but the naming and behavior
|
|
132 |
* changed between 1.0 and 2.0, which causes some dependency issues.
|
|
133 |
*/
|
|
134 | 40299 |
public static boolean isBlank(String string) |
135 |
{ |
|
136 | 40299 |
if (string == null || string.length() == 0) |
137 | 2154 |
return true; |
138 |
|
|
139 | 38145 |
if (string.trim().length() == 0)
|
140 | 0 |
return true; |
141 |
|
|
142 | 38145 |
return false; |
143 |
} |
|
144 |
|
|
145 |
/**
|
|
146 |
* As with {@link #isBlank(String)}, but inverts the response.
|
|
147 |
*/
|
|
148 | 12 |
public static boolean isNonBlank(String string) |
149 |
{ |
|
150 | 12 |
return !isBlank(string);
|
151 |
} |
|
152 |
|
|
153 |
/**
|
|
154 |
* Updates the location of an object, if the object implements
|
|
155 |
* {@link LocationHolder}.
|
|
156 |
*
|
|
157 |
* @param holder the object to be updated
|
|
158 |
* @param location the location to assign to the holder object
|
|
159 |
*/
|
|
160 | 29015 |
public static void setLocation(Object holder, Location location) |
161 |
{ |
|
162 | 29015 |
if (holder != null && holder instanceof LocationHolder) |
163 |
{ |
|
164 | 28386 |
LocationHolder lh = (LocationHolder) holder; |
165 |
|
|
166 | 28386 |
lh.setLocation(location); |
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
/**
|
|
171 |
* Checks if the value (a constructor or method parameter) is null,
|
|
172 |
* and throws an InvalidArgumentException if so.
|
|
173 |
*/
|
|
174 |
|
|
175 | 91 |
public static void checkNullParameter(String parameterName, Object value) |
176 |
{ |
|
177 | 91 |
if (value == null) |
178 | 1 |
throw new IllegalArgumentException( |
179 |
HiveMindMessages.nullParameterInvalid(parameterName)); |
|
180 |
} |
|
181 |
|
|
182 |
/**
|
|
183 |
* Returns true if the Collection is null or empty.
|
|
184 |
*/
|
|
185 | 420 |
public static boolean isEmpty(Collection c) |
186 |
{ |
|
187 | 420 |
return c == null || c.isEmpty(); |
188 |
} |
|
189 |
} |
|
190 |
|
|