Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / mojo / public / java / bindings / src / org / chromium / mojo / bindings / BindingsHelper.java
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.mojo.bindings;
6
7 import org.chromium.mojo.bindings.Struct.DataHeader;
8 import org.chromium.mojo.system.AsyncWaiter;
9 import org.chromium.mojo.system.Handle;
10
11 /**
12  * Helper functions.
13  */
14 public class BindingsHelper {
15     /**
16      * Alignment in bytes for mojo serialization.
17      */
18     public static final int ALIGNMENT = 8;
19
20     /**
21      * The size, in bytes, of a serialized handle. A handle is serialized as an int representing the
22      * offset of the handle in the list of handles.
23      */
24     public static final int SERIALIZED_HANDLE_SIZE = 4;
25
26     /**
27      * The size, in bytes, of a serialized pointer. A pointer is serializaed as an unsigned long
28      * representing the offset from its position to the pointed elemnt.
29      */
30     public static final int POINTER_SIZE = 8;
31
32     /**
33      * The header for a serialized map element.
34      */
35     public static final DataHeader MAP_STRUCT_HEADER = new DataHeader(24, 2);
36
37     /**
38      * The value used for the expected length of a non-fixed size array.
39      */
40     public static final int UNSPECIFIED_ARRAY_LENGTH = -1;
41
42     /**
43      * Passed as |arrayNullability| when neither the array nor its elements are nullable.
44      */
45     public static final int NOTHING_NULLABLE = 0;
46
47     /**
48      * "Array bit" of |arrayNullability| is set iff the array itself is nullable.
49      */
50     public static final int ARRAY_NULLABLE = (1 << 0);
51
52     /**
53      * "Element bit" of |arrayNullability| is set iff the array elements are nullable.
54      */
55     public static final int ELEMENT_NULLABLE = (1 << 1);
56
57     public static boolean isArrayNullable(int arrayNullability) {
58         return (arrayNullability & ARRAY_NULLABLE) > 0;
59     }
60
61     public static boolean isElementNullable(int arrayNullability) {
62         return (arrayNullability & ELEMENT_NULLABLE) > 0;
63     }
64
65     /**
66      * Align |size| on {@link BindingsHelper#ALIGNMENT}.
67      */
68     public static int align(int size) {
69         return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
70     }
71
72     /**
73      * Align |size| on {@link BindingsHelper#ALIGNMENT}.
74      */
75     public static long align(long size) {
76         return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
77     }
78
79     /**
80      * Compute the size in bytes of the given string encoded as utf8.
81      */
82     public static int utf8StringSizeInBytes(String s) {
83         int res = 0;
84         for (int i = 0; i < s.length(); ++i) {
85             char c = s.charAt(i);
86             int codepoint = c;
87             if (isSurrogate(c)) {
88                 i++;
89                 char c2 = s.charAt(i);
90                 codepoint = Character.toCodePoint(c, c2);
91             }
92             res += 1;
93             if (codepoint > 0x7f) {
94                 res += 1;
95                 if (codepoint > 0x7ff) {
96                     res += 1;
97                     if (codepoint > 0xffff) {
98                         res += 1;
99                         if (codepoint > 0x1fffff) {
100                             res += 1;
101                             if (codepoint > 0x3ffffff) {
102                                 res += 1;
103                             }
104                         }
105                     }
106                 }
107             }
108         }
109         return res;
110     }
111
112     /**
113      * Returns |true| if and only if the two objects are equals, handling |null|.
114      */
115     public static boolean equals(Object o1, Object o2) {
116         if (o1 == o2) {
117             return true;
118         }
119         if (o1 == null) {
120             return false;
121         }
122         return o1.equals(o2);
123     }
124
125     /**
126      * Returns the hash code of the object, handling |null|.
127      */
128     public static int hashCode(Object o) {
129         if (o == null) {
130             return 0;
131         }
132         return o.hashCode();
133     }
134
135     /**
136      * Returns the hash code of the value.
137      */
138     public static int hashCode(boolean o) {
139         return o ? 1231 : 1237;
140     }
141
142     /**
143      * Returns the hash code of the value.
144      */
145     public static int hashCode(long o) {
146         return (int) (o ^ (o >>> 32));
147     }
148
149     /**
150      * Returns the hash code of the value.
151      */
152     public static int hashCode(float o) {
153         return Float.floatToIntBits(o);
154     }
155
156     /**
157      * Returns the hash code of the value.
158      */
159     public static int hashCode(double o) {
160         return hashCode(Double.doubleToLongBits(o));
161     }
162
163     /**
164      * Returns the hash code of the value.
165      */
166     public static int hashCode(int o) {
167         return o;
168     }
169
170     /**
171      * Determines if the given {@code char} value is a Unicode <i>surrogate code unit</i>. See
172      * {@link Character#isSurrogate}. Extracting here because the method only exists at API level
173      * 19.
174      */
175     private static boolean isSurrogate(char c) {
176         return c >= Character.MIN_SURROGATE && c < (Character.MAX_SURROGATE + 1);
177     }
178
179     /**
180      * Returns an {@link AsyncWaiter} to use with the given handle, or |null| if none if available.
181      */
182     static AsyncWaiter getDefaultAsyncWaiterForHandle(Handle handle) {
183         if (handle.getCore() != null) {
184             return handle.getCore().getDefaultAsyncWaiter();
185         } else {
186             return null;
187         }
188     }
189 }