]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/ObjectUtils.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / ObjectUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * 27.12.2005
14  * @Author Toni Kalajainen
15  */
16 package org.simantics.utils;
17
18 import java.util.Collection;
19 import java.util.Iterator;
20
21 /**
22  * Basic utilities for any objects.
23  */
24 public class ObjectUtils {
25
26     /**
27      * Compares two object for equality.
28      * <p>
29      * Some times it is annoying to compare two objects if their value may be
30      * null.
31      * 
32      * @param o1 obj1
33      * @param o2 obj2
34      * @return true if equal or both null
35      */
36     public static boolean objectEquals(Object o1, Object o2) {
37         if (o1 == o2)
38             return true;
39         if (o1 == null && o2 == null)
40             return true;
41         if (o1 == null || o2 == null)
42             return false;
43         return o1.equals(o2);
44     }
45
46     public static int hashCode(Object obj) {
47         return (obj == null ? 0 : obj.hashCode());
48     }
49
50     public static boolean equals(Collection<?> c1, Collection<?> c2) {
51         if (c1.size() != c2.size())
52             return false;
53         Iterator<?> i1 = c1.iterator();
54         Iterator<?> i2 = c2.iterator();
55         while (i1.hasNext()) {
56             if (!ObjectUtils.objectEquals(i1.next(), i2.next()))
57                 return false;
58         }
59         return true;
60     }
61
62 }