Apache Thrift, Java: Object Data Types

I am stuck with Thrift about data types.

Now that I have a map and the integer value is generated using a bean, I use the i32 type in the idl definition.

class MyBean {
  Integer i = null;
}

struct TMyBean {
  1: i32 i;
}

The problem is that in the TMyBean generated by the bean, I var is a primitive int type, than it sets 0 as the default value, and for me 0 is a valid value.

I tried to put an optional keyword in the idl file, but everything does not change, it is always int.

How should I deal with this situation? I need me to take a null value in TMyBean i var.

Thank you Phaedra ..

+3
source share
3 answers

The keyword optionalwas the right choice.

, optional , isset:

struct MyBean {
  1: i32 IntValue
}

public class MyBean implements org.apache.thrift.TBase<MyBean, MyBean._Fields>, java.io.Serializable, Cloneable, Comparable<MyBean> {

  // ... lots of other code omitted ...

  // isset id assignments
  private static final int __INTVALUE_ISSET_ID = 0;
  private byte __isset_bitfield = 0;

  // ... lots of other code omitted ...

  /** Returns true if field IntValue is set (has been assigned a value) and false otherwise */
  public boolean isSetIntValue() {
    return EncodingUtils.testBit(__isset_bitfield, __INTVALUE_ISSET_ID);
  }

  public void setIntValueIsSet(boolean value) {
    __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __INTVALUE_ISSET_ID, value);
  }

  // ... even more code omitted ...

}
+2

java - , . Thrift i16, i32, i64, double . i32 i int i; Java; a int 0 Java, . , Thrift, .

+1

java ( ) , . , autoboxing, .

: struct TMyBean, myValue, Java , null: isSetMyValue() , : setMyValueIsSet(false).

* I don’t understand why Thrift decided not to use optional primitives as objects in Java and let autoboxing do this magic. Perhaps because of the large collections? In any case, it sounds like another priority issue, preferring performance over simplicity.

+1
source

All Articles