KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > util > Objects


1 /**
2  * Copyright (C) 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  */

16
17 package com.google.inject.util;
18
19 /**
20  * Object utilities.
21  *
22  * @author crazybob@google.com (Bob Lee)
23  */

24 public class Objects {
25
26   /**
27    * Detects null values.
28    *
29    * @param t value
30    * @param message to display in the event of a null
31    * @return t
32    */

33   public static <T> T nonNull(T t, String JavaDoc message) {
34     if (t == null) {
35       throw new NullPointerException JavaDoc(message);
36     }
37     return t;
38   }
39
40   /**
41    * {@code null}-aware equals.
42    */

43   public static boolean equal(Object JavaDoc a, Object JavaDoc b) {
44     if (a == b) {
45       return true;
46     }
47
48     if (a == null || b == null) {
49       return false;
50     }
51
52     return a.equals(b);
53   }
54
55   /**
56    * We use this as a sanity check immediately before injecting into a method
57    * or constructor, to make sure we aren't supplying a null. This should never
58    * happen because we should have caught the problem earlier. Perhaps this
59    * should be used with Java asserts...
60    */

61   public static void assertNoNulls(Object JavaDoc[] objects) {
62     // TODO(kevinb): gee, ya think we might want to remove this?
63
if (("I'm a bad hack".equals(
64         System.getProperty("guice.allow.nulls.bad.bad.bad")))) {
65       return;
66     }
67     if (objects != null) { // hmm. weird.
68
for (Object JavaDoc object : objects) {
69         if (object == null) {
70           throw new AssertionError JavaDoc();
71         }
72       }
73     }
74   }
75 }
76
Popular Tags