KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > DomainUtils


1 /*
2  * $Id: DomainUtils.java,v 1.2 2006/03/06 18:49:15 mattias Exp $
3  *****************************************************************************/

4 package org.infoglue.cms.util;
5
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.SortedSet JavaDoc;
9
10 /**
11  * Utility class for the domain objects. This class cannot be instantiated.
12  *
13  * @author Frank Febbraro (frank@phase2technology.com)
14  */

15 public final class DomainUtils
16 {
17     private DomainUtils() {}
18
19     /**
20      * Compares two objects for equality, considering the possibility that one or both may be null.
21      * @param o1 an object
22      * @param o2 another object
23      * @return true if o1 is null and o1 == o2, or o1.equals(o2) returns true; otherwise returns false.
24      */

25     public static boolean equals(Object JavaDoc o1, Object JavaDoc o2)
26     {
27         return (o1 == null) ? o1 == o2 : o1.equals(o2);
28     }
29
30     /**
31      * Compares two collections for equality, considering the possibility that one or both may be null.
32      * @param c1 a collection
33      * @param c2 another collection
34      * @return true if c1.size() == c2.size(), c1.containsAll(c2), and c2.containsAll(c1). This keeps the semantics
35      * consistent across the various collection implementations.
36      */

37     public static boolean equals(Collection JavaDoc c1, Collection JavaDoc c2)
38     {
39         if (c1 == null || c2 == null)
40             return c1 == c2;
41
42         if (c1.size() != c2.size())
43             return false;
44
45         return (c1 instanceof SortedSet JavaDoc && c2 instanceof SortedSet JavaDoc)
46                 ? containsAll((SortedSet JavaDoc)c1, (SortedSet JavaDoc)c2)
47                 : c1.containsAll(c2) && c2.containsAll(c1);
48     }
49
50     /**
51      * Compares two comparable objects, considering the possibility that one or both may be null.
52      * @param c1 an object
53      * @param c2 another object
54      * @return 0 if o1 and 02 are null, -1 if c1 is not null and c2 is null,
55      * 1 if c1 is null and c2 is not null, or c1.compareTo(o2) otherwise.
56      */

57     public static int compare(Comparable JavaDoc c1, Comparable JavaDoc c2)
58     {
59         if(c1 == null && c2 == null)
60             return 0;
61
62         if(c1 != null && c2 == null)
63             return -1;
64
65         if(c1 == null && c2 != null)
66             return 1;
67
68         return c1.compareTo(c2);
69     }
70
71     /*
72      * Indicates whether the given sorted sets contains the same elements in the same order. We need this method because
73      * of the way sorted sets are implemented, TreeSet in particular: the objects added to the set are used as keys in the
74      * underlying map, while the values in the map is some internal constant of type Object (TreeSet.PRESENT, to be exact).
75      * AbstractCollection.contains() called equals() on the values in the map, which are NOT the objects that were added.
76      * To get around this, we check each item in the set in succession; since they are sorted, the same elements should
77      * appear in the same order. If we make it through the entire collection, we return true if both iterators have no
78      * more elements; otherwise we return false;
79      */

80     private static boolean containsAll(SortedSet JavaDoc s1, SortedSet JavaDoc s2)
81     {
82         Iterator JavaDoc i = s1.iterator(), j = s2.iterator();
83
84         while (i.hasNext() && j.hasNext())
85             if (!i.next().equals(j.next()))
86                 return false;
87
88         return !i.hasNext() && !j.hasNext();
89     }
90
91     /**
92      * Returns an appropriate hash code for the given object, considering the possibilty that the object may be null.
93      * @param s a string
94      * @return if s is null, returns 0, otherwise returns s.hashCode().
95      */

96     public static int hashCode(Object JavaDoc s)
97     {
98         return (s == null) ? 0 : s.hashCode();
99     }
100 }
101
Popular Tags