01: /*
02: * Copyright 2005 Joe Walker
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: */
16: package org.directwebremoting.util;
17:
18: /**
19: * Some utilities for comparing objects that could be null
20: * @author Joe Walker [joe at getahead dot ltd dot uk]
21: */
22: public class CompareUtil {
23: /**
24: * Compare 2 objects taking account of the fact that either could be null
25: * @param <T> The type that we are comparing
26: * @param o1 The first object to compare
27: * @param o2 The second object to compare
28: * @return -1, 0, or 1
29: */
30: public static <T> int compare(Comparable<T> o1, T o2) {
31: if (o1 == null && o2 == null) {
32: return 0;
33: }
34:
35: if (o1 == null) {
36: return -1;
37: }
38:
39: if (o2 == null) {
40: return 1;
41: }
42:
43: return o1.compareTo(o2);
44: }
45:
46: /**
47: * Compare 2 objects taking account of the fact that either could be null
48: * @param o1 The first object to compare
49: * @param o2 The second object to compare
50: * @return true iff they are both null or if o1.equals(o2)
51: */
52: public static boolean equals(Object o1, Object o2) {
53: if (o1 == null && o2 == null) {
54: return true;
55: }
56:
57: if (o1 == null || o2 == null) {
58: return false;
59: }
60:
61: return o1.equals(o2);
62: }
63: }
|