| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.thrift; |
| 20 |
|
| 21 |
import java.util.Vector; |
| 22 |
import java.util.Hashtable; |
| 23 |
import java.util.Enumeration; |
| 24 |
|
| 25 |
public class TBaseHelper { |
| 26 |
|
| 27 |
public static int compareTo(boolean a, boolean b) { |
| 28 |
return (a == b) ? 0 : (a ? 1 : -1); |
| 29 |
|
| 30 |
|
| 31 |
} |
| 32 |
public static int compareTo(Boolean a, Boolean b) { |
| 33 |
return (a.booleanValue() == b.booleanValue()) ? 0 : (a.booleanValue() ? 1 : -1); |
| 34 |
|
| 35 |
|
| 36 |
} |
| 37 |
public static int compareTo(Boolean a, boolean b) { |
| 38 |
return (a.booleanValue() == b) ? 0 : (a.booleanValue() ? 1 : -1); |
| 39 |
|
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
public static Boolean booleanValueOf(boolean b) { |
| 44 |
return (b ? Boolean.TRUE : Boolean.FALSE); |
| 45 |
} |
| 46 |
|
| 47 |
public static int compareTo(byte a, byte b) { |
| 48 |
if (a < b) { |
| 49 |
return -1; |
| 50 |
} else if (b < a) { |
| 51 |
return 1; |
| 52 |
} else { |
| 53 |
return 0; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public static int compareTo(short a, short b) { |
| 58 |
if (a < b) { |
| 59 |
return -1; |
| 60 |
} else if (b < a) { |
| 61 |
return 1; |
| 62 |
} else { |
| 63 |
return 0; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public static int compareTo(int a, int b) { |
| 68 |
if (a < b) { |
| 69 |
return -1; |
| 70 |
} else if (b < a) { |
| 71 |
return 1; |
| 72 |
} else { |
| 73 |
return 0; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public static int compareTo(long a, long b) { |
| 78 |
if (a < b) { |
| 79 |
return -1; |
| 80 |
} else if (b < a) { |
| 81 |
return 1; |
| 82 |
} else { |
| 83 |
return 0; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public static int compareTo(double a, double b) { |
| 88 |
if (a < b) { |
| 89 |
return -1; |
| 90 |
} else if (b < a) { |
| 91 |
return 1; |
| 92 |
} else { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public static int compareTo(String a, String b) { |
| 98 |
return a.compareTo(b); |
| 99 |
} |
| 100 |
|
| 101 |
public static int compareTo(byte[] a, byte[] b) { |
| 102 |
int sizeCompare = compareTo(a.length, b.length); |
| 103 |
if (sizeCompare != 0) { |
| 104 |
return sizeCompare; |
| 105 |
} |
| 106 |
for (int i = 0; i < a.length; i++) { |
| 107 |
int byteCompare = compareTo(a, b); |
| 108 |
if (byteCompare != 0) { |
| 109 |
return byteCompare; |
| 110 |
} |
| 111 |
} |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
public static int compareTo(Object a, Object b) { |
| 116 |
if (a instanceof Vector) { |
| 117 |
return compareTo((Vector)a, (Vector)b); |
| 118 |
} if (a instanceof Hashtable) { |
| 119 |
return compareTo((Hashtable)a, (Hashtable)b); |
| 120 |
} else { |
| 121 |
return ((TBase)a).compareTo(b); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
public static int compareTo(Vector a, Vector b) { |
| 126 |
int lastComparison = compareTo(a.size(), b.size()); |
| 127 |
if (lastComparison != 0) { |
| 128 |
return lastComparison; |
| 129 |
} |
| 130 |
for (int i = 0; i < a.size(); i++) { |
| 131 |
Object oA = a.elementAt(i); |
| 132 |
Object oB = b.elementAt(i); |
| 133 |
lastComparison = compareTo(oA, oB); |
| 134 |
if (lastComparison != 0) { |
| 135 |
return lastComparison; |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
public static int compareTo(Hashtable a, Hashtable b) { |
| 143 |
int lastComparison = compareTo(a.size(), b.size()); |
| 144 |
if (lastComparison != 0) { |
| 145 |
return lastComparison; |
| 146 |
} |
| 147 |
Enumeration enumA = a.keys(); |
| 148 |
Enumeration enumB = b.keys(); |
| 149 |
while (lastComparison == 0 && enumA.hasMoreElements()) { |
| 150 |
Object keyA = enumA.nextElement(); |
| 151 |
Object keyB = enumB.nextElement(); |
| 152 |
lastComparison = compareTo(keyA, keyB); |
| 153 |
if (lastComparison == 0) { |
| 154 |
lastComparison = compareTo(a.get(keyA), b.get(keyB)); |
| 155 |
} |
| 156 |
} |
| 157 |
return lastComparison; |
| 158 |
} |
| 159 |
|
| 160 |
public static int compareTo(TEnum a, TEnum b) { |
| 161 |
return compareTo(a.getValue(), b.getValue()); |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
public static int compareTo(List a, List b) { |
| 166 |
int lastComparison = compareTo(a.size(), b.size()); |
| 167 |
if (lastComparison != 0) { |
| 168 |
return lastComparison; |
| 169 |
} |
| 170 |
for (int i = 0; i < a.size(); i++) { |
| 171 |
Object oA = a.get(i); |
| 172 |
Object oB = b.get(i); |
| 173 |
if (oA instanceof List) { |
| 174 |
lastComparison = compareTo((List) oA, (List) oB); |
| 175 |
} else { |
| 176 |
lastComparison = compareTo((Comparable) oA, (Comparable) oB); |
| 177 |
} |
| 178 |
if (lastComparison != 0) { |
| 179 |
return lastComparison; |
| 180 |
} |
| 181 |
} |
| 182 |
return 0; |
| 183 |
} |
| 184 |
*/ |
| 185 |
|
| 186 |
public static void toString(byte[] bytes, StringBuffer sb) { |
| 187 |
toString(bytes, 0, bytes.length, sb); |
| 188 |
} |
| 189 |
|
| 190 |
public static void toString(byte[] buf, int arrayOffset, int origLimit, StringBuffer sb) { |
| 191 |
int limit = (origLimit - arrayOffset > 128) ? arrayOffset + 128 : origLimit; |
| 192 |
|
| 193 |
for (int i = arrayOffset; i < limit; i++) { |
| 194 |
if (i > arrayOffset) { |
| 195 |
sb.append(" "); |
| 196 |
} |
| 197 |
sb.append(paddedByteString(buf[i])); |
| 198 |
} |
| 199 |
if (origLimit != limit) { |
| 200 |
sb.append("..."); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
public static String paddedByteString(byte b) { |
| 205 |
int extended = (b | 0x100) & 0x1ff; |
| 206 |
return Integer.toHexString(extended).toUpperCase().substring(1); |
| 207 |
} |
| 208 |
|
| 209 |
} |