01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */package com.bm.ejb3guice.internal;
16:
17: /**
18: * Object utilities.
19: *
20: * @author crazybob@google.com (Bob Lee)
21: */
22: public class Objects {
23:
24: /**
25: * Detects null values.
26: *
27: * @param t value
28: * @param message to display in the event of a null
29: * @return t
30: */
31: public static <T> T nonNull(T t, String message) {
32: if (t == null) {
33: throw new NullPointerException(message);
34: }
35: return t;
36: }
37:
38: /**
39: * {@code null}-aware equals.
40: */
41: public static boolean equal(Object a, Object b) {
42: if (a == b) {
43: return true;
44: }
45:
46: if (a == null || b == null) {
47: return false;
48: }
49:
50: return a.equals(b);
51: }
52:
53: /**
54: * We use this as a sanity check immediately before injecting into a method
55: * or constructor, to make sure we aren't supplying a null. This should never
56: * happen because we should have caught the problem earlier. Perhaps this
57: * should be used with Java asserts...
58: */
59: public static void assertNoNulls(Object[] objects) {
60: if (allowNullsBadBadBad) {
61: return;
62: }
63: if (objects != null) { // hmm. weird.
64: for (Object object : objects) {
65: if (object == null) {
66: throw new AssertionError();
67: }
68: }
69: }
70: }
71:
72: // TODO(kevinb): gee, ya think we might want to remove this?
73: private static final boolean allowNullsBadBadBad = true;
74: }
|