1 /*
2 * File: EqualsUtil.java
3 * Created: 13.10.2006 8:52:33
4 *
5 * Copyright 2006 Michal Burda.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 package net.sf.webmancer.util;
22
23 import java.util.Arrays;
24
25 /**
26 * @see Arrays
27 *
28 * @author Michal Burda
29 */
30 public class EqualsUtil {
31 /**
32 * Prevent instantiation
33 */
34 private EqualsUtil() {
35 }
36
37 /**
38 * @param first
39 * @param second
40 * @return
41 */
42 static public boolean areEqual(boolean first, boolean second) {
43 return first == second;
44 }
45
46 /**
47 * @param first
48 * @param second
49 * @return
50 */
51 static public boolean areEqual(char first, char second) {
52 return first == second;
53 }
54
55 /**
56 * @param first
57 * @param second
58 * @return
59 */
60 static public boolean areEqual(long first, long second) {
61 return first == second;
62 }
63
64 /**
65 * @param first
66 * @param second
67 * @return
68 */
69 static public boolean areEqual(float first, float second) {
70 return Float.floatToIntBits(first) == Float.floatToIntBits(second);
71 }
72
73 /**
74 * @param first
75 * @param second
76 * @return
77 */
78 static public boolean areEqual(double first, double second) {
79 return Double.doubleToLongBits(first) == Double.doubleToLongBits(second);
80 }
81
82 /**
83 * This method implements a <code>null</code> value safe comparison of two objects. If the two given parameters
84 * are both <code>null</code>, the method returns <code>true</code>. If either parameter is <code>null</code>
85 * while the other is not <code>null</code>, the method returns <code>false</code>. In the other case, a first
86 * parameter's {@link Object#equals(Object)} method is called and the result is returned.
87 *
88 * @param o1 First object to be compared or <code>null</code>
89 * @param o2 Second object to be compared or <code>null</code>
90 * @return <code>true</code> iff both parameters are <code>null</code> or they equal accordingly to
91 * {@link Object#equals(Object)}
92 */
93 public static boolean areEqual(final Object o1, final Object o2) {
94 if (o1 != null) {
95 return o1.equals(o2);
96 } else {
97 return (o2 == null);
98 }
99 }
100
101 }