1 /*
2 * $Id: CompareUtil.java 787720 2009-06-23 15:39:21Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.tiles;
23
24 /**
25 * Utilities to work with comparation between objects.
26 *
27 * @version $Rev: 787720 $ $Date: 2009-06-24 01:39:21 +1000 (Wed, 24 Jun 2009) $
28 * @since 2.2.0
29 */
30 public final class CompareUtil {
31
32 /**
33 * Private constructor to avoid instantiation.
34 */
35 private CompareUtil() { }
36
37 /**
38 * Checks if two objects (eventually null) are the same. They are considered the same
39 * even if they are both null.
40 *
41 * @param obj1 The first object to check.
42 * @param obj2 The second object to check.
43 * @return <code>true</code> if the objects are the same.
44 * @since 2.2.0
45 */
46 public static boolean nullSafeEquals(Object obj1, Object obj2) {
47 if (obj1 != null) {
48 return obj1.equals(obj2);
49 }
50 return obj2 == null;
51 }
52
53 /**
54 * Returns <code>0</code> if the object is null, the hash code of the object
55 * otherwise.
56 *
57 * @param obj The object from which the hash code must be calculated..
58 * @return The hash code.
59 * @since 2.2.0
60 */
61 public static int nullSafeHashCode(Object obj) {
62 if (obj != null) {
63 return obj.hashCode();
64 }
65 return 0;
66 }
67 }