• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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 
17 package libcore.util;
18 
19 import java.lang.reflect.Field;
20 import java.lang.reflect.Modifier;
21 import java.util.Arrays;
22 
23 public final class Objects {
Objects()24     private Objects() {}
25 
26     /**
27      * Returns true if two possibly-null objects are equal.
28      */
equal(Object a, Object b)29     public static boolean equal(Object a, Object b) {
30         return a == b || (a != null && a.equals(b));
31     }
32 
hashCode(Object o)33     public static int hashCode(Object o) {
34         return (o == null) ? 0 : o.hashCode();
35     }
36 
37     /**
38      * Returns a string reporting the value of each declared field, via reflection.
39      * Static and transient fields are automatically skipped. Produces output like
40      * "SimpleClassName[integer=1234,string="hello",character='c',intArray=[1,2,3]]".
41      */
toString(Object o)42     public static String toString(Object o) {
43         Class<?> c = o.getClass();
44         StringBuilder sb = new StringBuilder();
45         sb.append(c.getSimpleName()).append('[');
46         int i = 0;
47         for (Field f : c.getDeclaredFields()) {
48             if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0) {
49                 continue;
50             }
51             f.setAccessible(true);
52             try {
53                 Object value = f.get(o);
54 
55                 if (i++ > 0) {
56                     sb.append(',');
57                 }
58 
59                 sb.append(f.getName());
60                 sb.append('=');
61 
62                 if (value.getClass().isArray()) {
63                     if (value.getClass() == boolean[].class) {
64                         sb.append(Arrays.toString((boolean[]) value));
65                     } else if (value.getClass() == byte[].class) {
66                         sb.append(Arrays.toString((byte[]) value));
67                     } else if (value.getClass() == char[].class) {
68                         sb.append(Arrays.toString((char[]) value));
69                     } else if (value.getClass() == double[].class) {
70                         sb.append(Arrays.toString((double[]) value));
71                     } else if (value.getClass() == float[].class) {
72                         sb.append(Arrays.toString((float[]) value));
73                     } else if (value.getClass() == int[].class) {
74                         sb.append(Arrays.toString((int[]) value));
75                     } else if (value.getClass() == long[].class) {
76                         sb.append(Arrays.toString((long[]) value));
77                     } else if (value.getClass() == short[].class) {
78                         sb.append(Arrays.toString((short[]) value));
79                     } else {
80                         sb.append(Arrays.toString((Object[]) value));
81                     }
82                 } else if (value.getClass() == Character.class) {
83                     sb.append('\'').append(value).append('\'');
84                 } else if (value.getClass() == String.class) {
85                     sb.append('"').append(value).append('"');
86                 } else {
87                     sb.append(value);
88                 }
89             } catch (IllegalAccessException unexpected) {
90                 throw new AssertionError(unexpected);
91             }
92         }
93         sb.append("]");
94         return sb.toString();
95     }
96 }
97