summaryrefslogtreecommitdiff
path: root/lib/java/src/org/apache/thrift/TBaseHelper.java
blob: 18471b4ba59a54157827858462b8370a94414d36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.thrift;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public final class TBaseHelper {

  private TBaseHelper(){}

  private static final Comparator comparator = new NestedStructureComparator();

  public static int compareTo(Object o1, Object o2) {
    if (o1 instanceof Comparable) {
      return compareTo((Comparable)o1, (Comparable)o2);
    } else if (o1 instanceof List) {
      return compareTo((List)o1, (List)o2);
    } else if (o1 instanceof Set) {
      return compareTo((Set)o1, (Set)o2);
    } else if (o1 instanceof Map) {
      return compareTo((Map)o1, (Map)o2);
    } else if (o1 instanceof byte[]) {
      return compareTo((byte[])o1, (byte[])o2);
    } else {
      throw new IllegalArgumentException("Cannot compare objects of type " + o1.getClass());
    }
  }

  public static int compareTo(boolean a, boolean b) {
    return Boolean.compare(a, b);
  }

  public static int compareTo(byte a, byte b) {
    return Byte.compare(a, b);
  }

  public static int compareTo(short a, short b) {
    return Short.compare(a,b);
  }

  public static int compareTo(int a, int b) {
    return Integer.compare(a, b);
  }

  public static int compareTo(long a, long b) {
    return Long.compare(a, b);
  }

  public static int compareTo(double a, double b) {
    return Double.compare(a, b);
  }

  public static int compareTo(String a, String b) {
    return a.compareTo(b);
  }

  public static int compareTo(byte[] a, byte[] b) {
    int compare = compareTo(a.length, b.length);
    if (compare == 0) {
      for (int i = 0; i < a.length; i++) {
        compare = compareTo(a[i], b[i]);
        if (compare != 0) {
          break;
        }
      }
    }
    return compare;
  }

  public static int compareTo(Comparable a, Comparable b) {
    return a.compareTo(b);
  }

  public static int compareTo(List a, List b) {
    int compare = compareTo(a.size(), b.size());
    if (compare == 0) {
      for (int i = 0; i < a.size(); i++) {
        compare = comparator.compare(a.get(i), b.get(i));
        if (compare != 0) {
          break;
        }
      }
    }
    return compare;
  }

  public static int compareTo(Set a, Set b) {
    int compare = compareTo(a.size(), b.size());
    if (compare == 0) {
      ArrayList sortedA = new ArrayList(a);
      ArrayList sortedB = new ArrayList(b);

      Collections.sort(sortedA, comparator);
      Collections.sort(sortedB, comparator);

      Iterator iterA = sortedA.iterator();
      Iterator iterB = sortedB.iterator();

      // Compare each item.
      while (iterA.hasNext() && iterB.hasNext()) {
        compare = comparator.compare(iterA.next(), iterB.next());
        if (compare != 0) {
          break;
        }
      }
    }
    return compare;
  }

  public static int compareTo(Map a, Map b) {
    int lastComparison = compareTo(a.size(), b.size());
    if (lastComparison != 0) {
      return lastComparison;
    }

    // Sort a and b so we can compare them.
    SortedMap sortedA = new TreeMap(comparator);
    sortedA.putAll(a);
    Iterator<Map.Entry> iterA = sortedA.entrySet().iterator();
    SortedMap sortedB = new TreeMap(comparator);
    sortedB.putAll(b);
    Iterator<Map.Entry> iterB = sortedB.entrySet().iterator();

    // Compare each item.
    while (iterA.hasNext() && iterB.hasNext()) {
      Map.Entry entryA = iterA.next();
      Map.Entry entryB = iterB.next();
      lastComparison = comparator.compare(entryA.getKey(), entryB.getKey());
      if (lastComparison != 0) {
        return lastComparison;
      }
      lastComparison = comparator.compare(entryA.getValue(), entryB.getValue());
      if (lastComparison != 0) {
        return lastComparison;
      }
    }

    return 0;
  }

  /**
   * Comparator to compare items inside a structure (e.g. a list, set, or map).
   */
  private static class NestedStructureComparator implements Comparator, Serializable {
    public int compare(Object oA, Object oB) {
      if (oA == null && oB == null) {
        return 0;
      } else if (oA == null) {
        return -1;
      } else if (oB == null) {
        return 1;
      } else if (oA instanceof List) {
        return compareTo((List)oA, (List)oB);
      } else if (oA instanceof Set) {
        return compareTo((Set)oA, (Set)oB);
      } else if (oA instanceof Map) {
        return compareTo((Map)oA, (Map)oB);
      } else if (oA instanceof byte[]) {
        return compareTo((byte[])oA, (byte[])oB);
      } else {
        return compareTo((Comparable)oA, (Comparable)oB);
      }
    }
  }

  public static void toString(Collection<ByteBuffer> bbs, StringBuilder sb) {
    if (bbs == null) {
      sb.append(bbs);
      return;
    }

    Iterator<ByteBuffer> it = bbs.iterator();
    if (!it.hasNext()) {
      sb.append("[]");
    } else {
      sb.append("[");
      while (true) {
        ByteBuffer bb = it.next();
        org.apache.thrift.TBaseHelper.toString(bb, sb);
        if (!it.hasNext()) {
          sb.append("]");
          return;
        } else {
          sb.append(", ");
        }
      }
    }
  }

  public static void toString(ByteBuffer bb, StringBuilder sb) {
    if (bb == null) {
      sb.append(bb);
      return;
    }

    byte[] buf = bb.array();

    int arrayOffset = bb.arrayOffset();
    int offset = arrayOffset + bb.position();
    int origLimit = arrayOffset + bb.limit();
    int limit = (origLimit - offset > 128) ? offset + 128 : origLimit;

    for (int i = offset; i < limit; i++) {
      if (i > offset) {
        sb.append(" ");
      }
      sb.append(paddedByteString(buf[i]));
    }
    if (origLimit != limit) {
      sb.append("...");
    }
  }

  public static String paddedByteString(byte b) {
    int extended = (b | 0x100) & 0x1ff;
    return Integer.toHexString(extended).toUpperCase().substring(1);
  }

  public static byte[] byteBufferToByteArray(ByteBuffer byteBuffer) {
    if (wrapsFullArray(byteBuffer)) {
      return byteBuffer.array();
    }
    byte[] target = new byte[byteBuffer.remaining()];
    byteBufferToByteArray(byteBuffer, target, 0);
    return target;
  }

  public static boolean wrapsFullArray(ByteBuffer byteBuffer) {
    return byteBuffer.hasArray()
      && byteBuffer.position() == 0
      && byteBuffer.arrayOffset() == 0
      && byteBuffer.remaining() == byteBuffer.capacity();
  }

  public static int byteBufferToByteArray(ByteBuffer byteBuffer, byte[] target, int offset) {
    int remaining = byteBuffer.remaining();
    System.arraycopy(byteBuffer.array(),
        byteBuffer.arrayOffset() + byteBuffer.position(),
        target,
        offset,
        remaining);
    return remaining;
  }

  public static ByteBuffer rightSize(ByteBuffer in) {
    if (in == null) {
      return null;
    }
    if (wrapsFullArray(in)) {
      return in;
    }
    return ByteBuffer.wrap(byteBufferToByteArray(in));
  }

  public static ByteBuffer copyBinary(final ByteBuffer orig) {
    if (orig == null) {
      return null;
    }
    ByteBuffer copy = ByteBuffer.wrap(new byte[orig.remaining()]);
    if (orig.hasArray()) {
      System.arraycopy(orig.array(), orig.arrayOffset() + orig.position(), copy.array(), 0, orig.remaining());
    } else {
      orig.slice().get(copy.array());
    }

    return copy;
  }

  public static byte[] copyBinary(final byte[] orig) {
    return (orig == null) ? null : Arrays.copyOf(orig, orig.length);
  }

  public static int hashCode(long value) {
    return Long.hashCode(value);
  }

  public static int hashCode(double value) {
    return Double.hashCode(value);
  }
}