|
|||||||||||||||||||
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% | 93.5% | 90% | 91.8% |
|
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 |
|
|
19 |
/**
|
|
20 |
* Static utility class for HiveMind.
|
|
21 |
*
|
|
22 |
* @author Howard Lewis Ship
|
|
23 |
*/
|
|
24 |
public final class HiveMind |
|
25 |
{ |
|
26 |
/**
|
|
27 |
* The full id of the {@link org.apache.hivemind.service.ThreadEventNotifier}
|
|
28 |
* service.
|
|
29 |
*/
|
|
30 |
public static final String THREAD_EVENT_NOTIFIER_SERVICE = "hivemind.ThreadEventNotifier"; |
|
31 |
|
|
32 | 0 |
private HiveMind()
|
33 |
{ |
|
34 |
// Prevent instantiation
|
|
35 |
} |
|
36 |
|
|
37 | 4 |
public static ApplicationRuntimeException createRegistryShutdownException() |
38 |
{ |
|
39 | 4 |
return new ApplicationRuntimeException(HiveMindMessages.registryShutdown()); |
40 |
} |
|
41 |
|
|
42 |
/**
|
|
43 |
* Selects the first {@link Location} in an array of objects.
|
|
44 |
* Skips over nulls. The objects may be instances of
|
|
45 |
* Location or {@link Locatable}. May return null
|
|
46 |
* if no Location can be found.
|
|
47 |
*/
|
|
48 |
|
|
49 | 99 |
public static Location findLocation(Object[] locations) |
50 |
{ |
|
51 | 99 |
for (int i = 0; i < locations.length; i++) |
52 |
{ |
|
53 | 255 |
Object location = locations[i]; |
54 |
|
|
55 | 255 |
Location result = getLocation(location); |
56 |
|
|
57 | 255 |
if (result != null) |
58 | 23 |
return result;
|
59 |
|
|
60 |
} |
|
61 |
|
|
62 | 76 |
return null; |
63 |
} |
|
64 |
|
|
65 |
/**
|
|
66 |
* Extracts a location from an object, checking to see if it
|
|
67 |
* implement {@link Location} or {@link Locatable}.
|
|
68 |
*
|
|
69 |
* @returns the Location, or null if it can't be found
|
|
70 |
*/
|
|
71 | 295 |
public static Location getLocation(Object object) |
72 |
{ |
|
73 | 295 |
if (object == null) |
74 | 195 |
return null; |
75 |
|
|
76 | 100 |
if (object instanceof Location) |
77 | 16 |
return (Location) object;
|
78 |
|
|
79 | 84 |
if (object instanceof Locatable) |
80 |
{ |
|
81 | 40 |
Locatable locatable = (Locatable) object; |
82 |
|
|
83 | 40 |
return locatable.getLocation();
|
84 |
} |
|
85 |
|
|
86 | 44 |
return null; |
87 |
} |
|
88 |
|
|
89 |
/**
|
|
90 |
* Invokes {@link #getLocation(Object)}, then translate the result
|
|
91 |
* to a string value, or "unknown location" if null.
|
|
92 |
*
|
|
93 |
*/
|
|
94 | 4 |
public static String getLocationString(Object object) |
95 |
{ |
|
96 | 4 |
Location l = getLocation(object); |
97 |
|
|
98 | 4 |
if (l != null) |
99 | 0 |
return l.toString();
|
100 |
|
|
101 | 4 |
return HiveMindMessages.unknownLocation();
|
102 |
} |
|
103 |
|
|
104 |
/**
|
|
105 |
* Returns true if the string is null, empty, or contains only whitespace.
|
|
106 |
*
|
|
107 |
* <p>
|
|
108 |
* The commons-lang library provides a version of this, but the naming and behavior
|
|
109 |
* changed between 1.0 and 2.0, which causes some dependency issues.
|
|
110 |
*/
|
|
111 | 44070 |
public static boolean isBlank(String string) |
112 |
{ |
|
113 | 44070 |
if (string == null || string.length() == 0) |
114 | 41469 |
return true; |
115 |
|
|
116 | 2601 |
if (string.trim().length() == 0)
|
117 | 0 |
return true; |
118 |
|
|
119 | 2601 |
return false; |
120 |
} |
|
121 |
|
|
122 |
/**
|
|
123 |
* As with {@link #isBlank(String)}, but inverts the response.
|
|
124 |
*/
|
|
125 | 12 |
public static boolean isNonBlank(String string) |
126 |
{ |
|
127 | 12 |
return !isBlank(string);
|
128 |
} |
|
129 |
|
|
130 |
/**
|
|
131 |
* Updates the location of an object, if the object implements
|
|
132 |
* {@link LocationHolder}.
|
|
133 |
*
|
|
134 |
* @param holder the object to be updated
|
|
135 |
* @param location the location to assign to the holder object
|
|
136 |
*/
|
|
137 | 34507 |
public static void setLocation(Object holder, Location location) |
138 |
{ |
|
139 | 34507 |
if (holder != null && holder instanceof LocationHolder) |
140 |
{ |
|
141 | 33541 |
LocationHolder lh = (LocationHolder) holder; |
142 |
|
|
143 | 33541 |
lh.setLocation(location); |
144 |
} |
|
145 |
} |
|
146 |
|
|
147 |
/**
|
|
148 |
* Checks if the value (a constructor or method parameter) is null,
|
|
149 |
* and throws an InvalidArgumentException if so.
|
|
150 |
*/
|
|
151 |
|
|
152 | 100 |
public static void checkNullParameter(String parameterName, Object value) |
153 |
{ |
|
154 | 100 |
if (value == null) |
155 | 1 |
throw new IllegalArgumentException( |
156 |
HiveMindMessages.nullParameterInvalid(parameterName)); |
|
157 |
} |
|
158 |
|
|
159 |
/**
|
|
160 |
* Returns true if the Collection is null or empty.
|
|
161 |
*/
|
|
162 | 682 |
public static boolean isEmpty(Collection c) |
163 |
{ |
|
164 | 682 |
return c == null || c.isEmpty(); |
165 |
} |
|
166 |
} |
|
167 |
|
|