Merge remote-tracking branch 'origin/svn'
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / ObjectUtils.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12
13 package org.simantics.databoard.util;
14
15 import java.lang.reflect.Array;\r
16 import java.lang.reflect.Field;\r
17 import java.util.Arrays;\r
18 import java.util.Collection;\r
19 import java.util.HashSet;\r
20 import java.util.Iterator;\r
21 import java.util.Set;\r
22
23 /**
24  * ObjectUtils
25  * 
26  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
27  */
28 public class ObjectUtils {
29
30     /**
31      * Equals test for two optional values. 
32      *  
33      * @param o1 obj1 or null
34      * @param o2 obj2 or null
35      * @return true if equal or both null
36      */
37     public static boolean objectEquals(Object o1, Object o2) {
38         if (o1==o2) return true;
39         if (o1==null && o2==null) return true;
40         if (o1==null || o2==null) return false;
41         return o1.equals(o2);
42     }
43     
44     public static int hashCode(Object obj) {
45         return (obj==null?0:obj.hashCode());
46     }
47
48         public static boolean equals(Collection<?> c1, Collection<?> c2)
49         {
50                 if (c1.size()!=c2.size()) return false;
51                 Iterator<?> i1 = c1.iterator();
52                 Iterator<?> i2 = c2.iterator();
53                 while (i1.hasNext())
54                 {
55                         if (!ObjectUtils.objectEquals(i1.next(), i2.next()))
56                                 return false;
57                 }
58                 return true;
59         }
60         
61         public static String toString(Collection<?> c)
62         {
63                 if (c.isEmpty()) return "[]";
64         StringBuilder sb = new StringBuilder();
65         sb.append("[");
66         int i=0;
67         for (Object o : c)
68         {
69                 if (i++>0) sb.append(", ");
70                 sb.append( o );
71         }
72         sb.append("]");
73         return sb.toString();
74                 
75         }
76         
77         
78         public static String printFieldsDeep(Object o)
79         {
80                 Set<Object> visitedObjects = new HashSet<Object>();
81                 StringBuilder sb = new StringBuilder();
82                 _printFieldsDeep(sb, o, "", visitedObjects);
83                 return sb.toString();
84         }
85         
86         private static void _printFieldsDeep(StringBuilder sb, Object o, String indent, Set<Object> visitedObjects)
87         {
88                 Class<?> c = o.getClass();
89                 sb.append(c.getSimpleName()+" (id="+System.identityHashCode(o)+")\n");
90                 if (!visitedObjects.add(o)) return;
91                 indent += "  ";
92                 while (!c.equals(Object.class)) {
93                         for (Field f : ReflectionUtils.getAllFields(c))
94                         {
95                                 boolean isStatic = (f.getModifiers() & 8) == 8;
96                                 boolean isArray = f.getType().isArray();
97
98                                 if (isStatic) continue;
99                                 f.setAccessible(true);
100                                 sb.append(indent+f.getName()+"=");
101                                 try {
102                                         Object dada = f.get(o);         
103                                         if (dada==null)
104                                                 sb.append("null\n");
105                                         else if (isArray) {
106                                         if (dada instanceof double[]) sb.append( Arrays.toString((double[])dada) +"\n"); 
107                                         else if (dada instanceof boolean[]) sb.append( Arrays.toString((boolean[])dada) +"\n"); 
108                                         else if (dada instanceof byte[]) sb.append( Arrays.toString((byte[])dada) +"\n"); 
109                                         else if (dada instanceof char[]) sb.append( Arrays.toString((char[])dada) +"\n"); 
110                                         else if (dada instanceof float[]) sb.append( Arrays.toString((float[])dada) +"\n"); 
111                                         else if (dada instanceof int[]) sb.append( Arrays.toString((int[])dada) +"\n"); 
112                                         else if (dada instanceof long[]) sb.append( Arrays.toString((long[])dada) +"\n"); 
113                                         else if (dada instanceof short[]) sb.append( Arrays.toString((short[])dada) +"\n");
114                                         else {
115                                                 sb.append(dada.getClass().getComponentType()+"["+Array.getLength(dada)+"]\n");
116                                                 for (int i=0; i<Array.getLength(dada); i++) {
117                                                         Object subDada = Array.get(dada, i);
118                                                         sb.append(indent+"  ["+i+"]=");
119                                                         try{
120                                                         if (subDada.getClass().getCanonicalName().startsWith("java")) {                                                          
121                                                                 sb.append(subDada.toString());
122                                                                 sb.append("\n");
123                                                         } else {
124                                                                 _printFieldsDeep(sb, subDada, indent, visitedObjects);
125                                                         }
126                                                         }
127                                                         catch (Exception e) {
128                                                                         System.out.println("ERROR From ObjectUtils / _printfieldsDeep");
129                                                                         if(subDada == null){
130                                                                                 System.out.println("Subdada null");
131                                                                                 System.out.println(sb.toString());
132                                                                         }
133                                                                 }
134                                                         
135                                                 }
136                                         }
137                                         } else {
138                                         if (dada.getClass().getCanonicalName().startsWith("java")) {
139                                                 sb.append(dada.toString());
140                                                 sb.append("\n");
141                                         } else {
142                                                 _printFieldsDeep(sb, dada, indent, visitedObjects);
143                                         }
144                                         }
145                                         //sb.append("\n");
146                                 } catch (IllegalArgumentException e) {
147                                         sb.append("?");
148                                         continue;
149                                 } catch (IllegalAccessException e) {
150                                         sb.append("?");
151                                         continue;
152                                 }
153                         }
154                         c = c.getSuperclass();          
155                 }
156         }
157         
158         public static String printFields(Object o)
159         {
160                 Class<?> c = o.getClass();
161                 StringBuilder sb = new StringBuilder();
162                 
163                 while (!c.equals(Object.class)) {
164                         for (Field f : c.getDeclaredFields())
165                         {
166                                 boolean isStatic = (f.getModifiers() & 8) == 8;
167                                 boolean isArray = f.getType().isArray();
168
169                                 if (isStatic) continue;
170                                 f.setAccessible(true);
171                                 if (sb.length()>0) sb.append(", ");
172                                 sb.append(f.getName()+"=");
173                                 try {
174                                         Object dada = f.get(o);         
175                                         if (dada==null)
176                                                 sb.append("null");
177                                         else if (isArray) 
178                                                 sb.append(toString(dada));
179                                         else                            
180                                                 sb.append(dada);
181                                 } catch (IllegalArgumentException e) {
182                                         sb.append("?");
183                                         continue;
184                                 } catch (IllegalAccessException e) {
185                                         sb.append("?");
186                                         continue;
187                                 }
188                         }
189                         c = c.getSuperclass();
190                 }
191                 
192                 return sb.toString();
193         }       
194
195     public static String toString(Object obj) {
196         if (obj instanceof Object[]) {            
197             return Arrays.toString((Object[])obj); 
198         }
199         if (obj instanceof double[]) return Arrays.toString((double[])obj); 
200         if (obj instanceof boolean[]) return Arrays.toString((boolean[])obj); 
201         if (obj instanceof byte[]) return Arrays.toString((byte[])obj); 
202         if (obj instanceof char[]) return Arrays.toString((char[])obj); 
203         if (obj instanceof float[]) return Arrays.toString((float[])obj); 
204         if (obj instanceof int[]) return Arrays.toString((int[])obj); 
205         if (obj instanceof long[]) return Arrays.toString((long[])obj); 
206         if (obj instanceof short[]) return Arrays.toString((short[])obj);
207         return obj.toString();
208     }           
209 }