indent(out) << "public int compareTo(" << type_name(tstruct) << " other) {" << endl;
indent(out) << " int lastComparison = TBaseHelper.compareTo(getSetField(), other.getSetField());" << endl;
indent(out) << " if (lastComparison == 0) {" << endl;
- indent(out) << " return TBaseHelper.compareTo((Comparable)getFieldValue(), (Comparable)other.getFieldValue());" << endl;
+ indent(out) << " Object myValue = getFieldValue();" << endl;
+ indent(out) << " if (myValue instanceof byte[]) {" << endl;
+ indent(out) << " return TBaseHelper.compareTo((byte[])myValue, (byte[])other.getFieldValue());" << endl;
+ indent(out) << " } else {" << endl;
+ indent(out) << " return TBaseHelper.compareTo((Comparable)myValue, (Comparable)other.getFieldValue());" << endl;
+ indent(out) << " }" << endl;
indent(out) << " }" << endl;
indent(out) << " return lastComparison;" << endl;
indent(out) << "}" << endl;
import thrift.test.Empty;
import thrift.test.StructWithAUnion;
import thrift.test.TestUnion;
+import thrift.test.ComparableUnion;
public class UnionTest {
*/
public static void main(String[] args) throws Exception {
testBasic();
- testEquality();
+ testEquality();
testSerialization();
+ testCompareTo();
}
public static void testBasic() throws Exception {
swau.write(proto);
new Empty().read(proto);
+ }
+
+ public static void testCompareTo() throws Exception {
+ ComparableUnion cu = ComparableUnion.string_field("a");
+ ComparableUnion cu2 = ComparableUnion.string_field("b");
+
+ if (cu.compareTo(cu2) != -1) {
+ throw new RuntimeException("a was supposed to be < b, but was " + cu.compareTo(cu2));
+ }
+
+ if (cu2.compareTo(cu) != 1) {
+ throw new RuntimeException("b was supposed to be > a, but was " + cu2.compareTo(cu));
+ }
+
+ cu2 = ComparableUnion.binary_field(new byte[]{2});
+
+ if (cu.compareTo(cu2) != -1) {
+ throw new RuntimeException("a was supposed to be < b, but was " + cu.compareTo(cu2));
+ }
+
+ if (cu2.compareTo(cu) != 1) {
+ throw new RuntimeException("b was supposed to be > a, but was " + cu2.compareTo(cu));
+ }
+
+ cu = ComparableUnion.binary_field(new byte[]{1});
+ if (cu.compareTo(cu2) != -1) {
+ throw new RuntimeException("a was supposed to be < b, but was " + cu.compareTo(cu2));
+ }
+
+ if (cu2.compareTo(cu) != 1) {
+ throw new RuntimeException("b was supposed to be > a, but was " + cu2.compareTo(cu));
+ }
}
}