|
|||||||||||||||||||
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 | 88.9% | 93.1% | 88.9% | 91.1% |
|
1 |
// Copyright 2004, 2005 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}service.
|
|
28 |
*/
|
|
29 |
public static final String THREAD_EVENT_NOTIFIER_SERVICE = "hivemind.ThreadEventNotifier"; |
|
30 |
|
|
31 |
/**
|
|
32 |
* An object used to synchronize access to {@link java.beans.Introspector}(which is not fully
|
|
33 |
* threadsafe).
|
|
34 |
*
|
|
35 |
* @since 3.1
|
|
36 |
*/
|
|
37 |
|
|
38 |
public static final Object INTROSPECTOR_MUTEX = new Object(); |
|
39 |
|
|
40 | 0 |
private HiveMind()
|
41 |
{ |
|
42 |
// Prevent instantiation
|
|
43 |
} |
|
44 |
|
|
45 | 4 |
public static ApplicationRuntimeException createRegistryShutdownException() |
46 |
{ |
|
47 | 4 |
return new ApplicationRuntimeException(HiveMindMessages.registryShutdown()); |
48 |
} |
|
49 |
|
|
50 |
/**
|
|
51 |
* Selects the first {@link Location}in an array of objects. Skips over nulls. The objects may
|
|
52 |
* be instances of Location or {@link Locatable}. May return null if no Location can be found.
|
|
53 |
*/
|
|
54 |
|
|
55 | 122 |
public static Location findLocation(Object[] locations) |
56 |
{ |
|
57 | 122 |
for (int i = 0; i < locations.length; i++) |
58 |
{ |
|
59 | 318 |
Object location = locations[i]; |
60 |
|
|
61 | 318 |
Location result = getLocation(location); |
62 |
|
|
63 | 318 |
if (result != null) |
64 | 26 |
return result;
|
65 |
|
|
66 |
} |
|
67 |
|
|
68 | 96 |
return null; |
69 |
} |
|
70 |
|
|
71 |
/**
|
|
72 |
* Extracts a location from an object, checking to see if it implement {@link Location}or
|
|
73 |
* {@link Locatable}.
|
|
74 |
*
|
|
75 |
* @returns the Location, or null if it can't be found
|
|
76 |
*/
|
|
77 | 331 |
public static Location getLocation(Object object) |
78 |
{ |
|
79 | 331 |
if (object == null) |
80 | 250 |
return null; |
81 |
|
|
82 | 81 |
if (object instanceof Location) |
83 | 19 |
return (Location) object;
|
84 |
|
|
85 | 62 |
if (object instanceof Locatable) |
86 |
{ |
|
87 | 14 |
Locatable locatable = (Locatable) object; |
88 |
|
|
89 | 14 |
return locatable.getLocation();
|
90 |
} |
|
91 |
|
|
92 | 48 |
return null; |
93 |
} |
|
94 |
|
|
95 |
/**
|
|
96 |
* Invokes {@link #getLocation(Object)}, then translate the result to a string value, or
|
|
97 |
* "unknown location" if null.
|
|
98 |
*/
|
|
99 | 4 |
public static String getLocationString(Object object) |
100 |
{ |
|
101 | 4 |
Location l = getLocation(object); |
102 |
|
|
103 | 4 |
if (l != null) |
104 | 0 |
return l.toString();
|
105 |
|
|
106 | 4 |
return HiveMindMessages.unknownLocation();
|
107 |
} |
|
108 |
|
|
109 |
/**
|
|
110 |
* Returns true if the string is null, empty, or contains only whitespace.
|
|
111 |
* <p>
|
|
112 |
* The commons-lang library provides a version of this, but the naming and behavior changed
|
|
113 |
* between 1.0 and 2.0, which causes some dependency issues.
|
|
114 |
*/
|
|
115 | 52117 |
public static boolean isBlank(String string) |
116 |
{ |
|
117 | 52117 |
if (string == null || string.length() == 0) |
118 | 49432 |
return true; |
119 |
|
|
120 | 2685 |
if (string.trim().length() == 0)
|
121 | 0 |
return true; |
122 |
|
|
123 | 2685 |
return false; |
124 |
} |
|
125 |
|
|
126 |
/**
|
|
127 |
* As with {@link #isBlank(String)}, but inverts the response.
|
|
128 |
*/
|
|
129 | 12 |
public static boolean isNonBlank(String string) |
130 |
{ |
|
131 | 12 |
return !isBlank(string);
|
132 |
} |
|
133 |
|
|
134 |
/**
|
|
135 |
* Updates the location of an object, if the object implements {@link LocationHolder}.
|
|
136 |
*
|
|
137 |
* @param holder
|
|
138 |
* the object to be updated
|
|
139 |
* @param location
|
|
140 |
* the location to assign to the holder object
|
|
141 |
*/
|
|
142 | 40336 |
public static void setLocation(Object holder, Location location) |
143 |
{ |
|
144 | 40336 |
if (holder != null && holder instanceof LocationHolder) |
145 |
{ |
|
146 | 38932 |
LocationHolder lh = (LocationHolder) holder; |
147 |
|
|
148 | 38932 |
lh.setLocation(location); |
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
/**
|
|
153 |
* Returns true if the Collection is null or empty.
|
|
154 |
*/
|
|
155 | 724 |
public static boolean isEmpty(Collection c) |
156 |
{ |
|
157 | 724 |
return c == null || c.isEmpty(); |
158 |
} |
|
159 |
} |
|