thrift java 序列化_JAVA RPC (四) 之thrift序列化普通对象

1 /**

2 * Autogenerated by Thrift Compiler (0.8.0)3 *4 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING5 * @generated6 */

7 packagecom.thrift;8

9 importorg.apache.thrift.scheme.IScheme;10 importorg.apache.thrift.scheme.SchemeFactory;11 importorg.apache.thrift.scheme.StandardScheme;12

13 importorg.apache.thrift.scheme.TupleScheme;14 importorg.apache.thrift.protocol.TTupleProtocol;15 importjava.util.List;16 importjava.util.ArrayList;17 importjava.util.Map;18 importjava.util.HashMap;19 importjava.util.EnumMap;20 importjava.util.Set;21 importjava.util.HashSet;22 importjava.util.EnumSet;23 importjava.util.Collections;24 importjava.util.BitSet;25 importjava.nio.ByteBuffer;26 importjava.util.Arrays;27 importorg.slf4j.Logger;28 importorg.slf4j.LoggerFactory;29

30 /**

31 * 测试类32 *33 */

34 public class koalasRequest implements org.apache.thrift.TBase, java.io.Serializable, Cloneable {35 private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("koalasRequest");36

37 private static final org.apache.thrift.protocol.TField AGE_FIELD_DESC = new org.apache.thrift.protocol.TField("age", org.apache.thrift.protocol.TType.I32, (short)1);38 private static final org.apache.thrift.protocol.TField NAME_FIELD_DESC = new org.apache.thrift.protocol.TField("name", org.apache.thrift.protocol.TType.STRING, (short)2);39 private static final org.apache.thrift.protocol.TField ADDRESS_FIELD_DESC = new org.apache.thrift.protocol.TField("address", org.apache.thrift.protocol.TType.STRING, (short)3);40

41 private static final Map, SchemeFactory> schemes = new HashMap, SchemeFactory>();42 static{43 schemes.put(StandardScheme.class, newkoalasRequestStandardSchemeFactory());44 schemes.put(TupleScheme.class, newkoalasRequestTupleSchemeFactory());45 }46

47 public int age; //required

48 public String name; //required

49 public String address; //required

50

51 /**The set of fields this struct contains, along with convenience methods for finding and manipulating them.*/

52 public enum _Fields implementsorg.apache.thrift.TFieldIdEnum {53 AGE((short)1, "age"),54 NAME((short)2, "name"),55 ADDRESS((short)3, "address");56

57 private static final Map byName = new HashMap();58

59 static{60 for (_Fields field : EnumSet.allOf(_Fields.class)) {61 byName.put(field.getFieldName(), field);62 }63 }64

65 /**

66 * Find the _Fields constant that matches fieldId, or null if its not found.67 */

68 public static _Fields findByThriftId(intfieldId) {69 switch(fieldId) {70 case 1: //AGE

71 returnAGE;72 case 2: //NAME

73 returnNAME;74 case 3: //ADDRESS

75 returnADDRESS;76 default:77 return null;78 }79 }80

81 /**

82 * Find the _Fields constant that matches fieldId, throwing an exception83 * if it is not found.84 */

85 public static _Fields findByThriftIdOrThrow(intfieldId) {86 _Fields fields =findByThriftId(fieldId);87 if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!");88 returnfields;89 }90

91 /**

92 * Find the _Fields constant that matches name, or null if its not found.93 */

94 public static_Fields findByName(String name) {95 returnbyName.get(name);96 }97

98 private final short_thriftId;99 private finalString _fieldName;100

101 _Fields(shortthriftId, String fieldName) {102 _thriftId =thriftId;103 _fieldName =fieldName;104 }105

106 public shortgetThriftFieldId() {107 return_thriftId;108 }109

110 publicString getFieldName() {111 return_fieldName;112 }113 }114

115 //isset id assignments

116 private static final int __AGE_ISSET_ID = 0;117 private BitSet __isset_bit_vector = new BitSet(1);118 public static final Map<_fields org.apache.thrift.meta_data.fieldmetadata>metaDataMap;119 static{120 Map<_fields org.apache.thrift.meta_data.fieldmetadata> tmpMap = new EnumMap<_fields org.apache.thrift.meta_data.fieldmetadata>(_Fields.class);121 tmpMap.put(_Fields.AGE, new org.apache.thrift.meta_data.FieldMetaData("age", org.apache.thrift.TFieldRequirementType.DEFAULT,122 neworg.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.I32)));123 tmpMap.put(_Fields.NAME, new org.apache.thrift.meta_data.FieldMetaData("name", org.apache.thrift.TFieldRequirementType.DEFAULT,124 neworg.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));125 tmpMap.put(_Fields.ADDRESS, new org.apache.thrift.meta_data.FieldMetaData("address", org.apache.thrift.TFieldRequirementType.DEFAULT,126 neworg.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));127 metaDataMap =Collections.unmodifiableMap(tmpMap);128 org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(koalasRequest.class, metaDataMap);129 }130

131 publickoalasRequest() {132 }133

134 publickoalasRequest(135 intage,136 String name,137 String address)138 {139 this();140 this.age =age;141 setAgeIsSet(true);142 this.name =name;143 this.address =address;144 }145

146 /**

147 * Performs a deep copy on other.148 */

149 publickoalasRequest(koalasRequest other) {150 __isset_bit_vector.clear();151 __isset_bit_vector.or(other.__isset_bit_vector);152 this.age =other.age;153 if(other.isSetName()) {154 this.name =other.name;155 }156 if(other.isSetAddress()) {157 this.address =other.address;158 }159 }160

161 publickoalasRequest deepCopy() {162 return new koalasRequest(this);163 }164

165 @Override166 public voidclear() {167 setAgeIsSet(false);168 this.age = 0;169 this.name = null;170 this.address = null;171 }172

173 public intgetAge() {174 return this.age;175 }176

177 public koalasRequest setAge(intage) {178 this.age =age;179 setAgeIsSet(true);180 return this;181 }182

183 public voidunsetAge() {184 __isset_bit_vector.clear(__AGE_ISSET_ID);185 }186

187 /**Returns true if field age is set (has been assigned a value) and false otherwise*/

188 public booleanisSetAge() {189 return__isset_bit_vector.get(__AGE_ISSET_ID);190 }191

192 public void setAgeIsSet(booleanvalue) {193 __isset_bit_vector.set(__AGE_ISSET_ID, value);194 }195

196 publicString getName() {197 return this.name;198 }199

200 publickoalasRequest setName(String name) {201 this.name =name;202 return this;203 }204

205 public voidunsetName() {206 this.name = null;207 }208

209 /**Returns true if field name is set (has been assigned a value) and false otherwise*/

210 public booleanisSetName() {211 return this.name != null;212 }213

214 public void setNameIsSet(booleanvalue) {215 if (!value) {216 this.name = null;217 }218 }219

220 publicString getAddress() {221 return this.address;222 }223

224 publickoalasRequest setAddress(String address) {225 this.address =address;226 return this;227 }228

229 public voidunsetAddress() {230 this.address = null;231 }232

233 /**Returns true if field address is set (has been assigned a value) and false otherwise*/

234 public booleanisSetAddress() {235 return this.address != null;236 }237

238 public void setAddressIsSet(booleanvalue) {239 if (!value) {240 this.address = null;241 }242 }243

244 public voidsetFieldValue(_Fields field, Object value) {245 switch(field) {246 caseAGE:247 if (value == null) {248 unsetAge();249 } else{250 setAge((Integer)value);251 }252 break;253

254 caseNAME:255 if (value == null) {256 unsetName();257 } else{258 setName((String)value);259 }260 break;261

262 caseADDRESS:263 if (value == null) {264 unsetAddress();265 } else{266 setAddress((String)value);267 }268 break;269

270 }271 }272

273 publicObject getFieldValue(_Fields field) {274 switch(field) {275 caseAGE:276 returnInteger.valueOf(getAge());277

278 caseNAME:279 returngetName();280

281 caseADDRESS:282 returngetAddress();283

284 }285 throw newIllegalStateException();286 }287

288 /**Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise*/

289 public booleanisSet(_Fields field) {290 if (field == null) {291 throw newIllegalArgumentException();292 }293

294 switch(field) {295 caseAGE:296 returnisSetAge();297 caseNAME:298 returnisSetName();299 caseADDRESS:300 returnisSetAddress();301 }302 throw newIllegalStateException();303 }304

305 @Override306 public booleanequals(Object that) {307 if (that == null)308 return false;309 if (that instanceofkoalasRequest)310 return this.equals((koalasRequest)that);311 return false;312 }313

314 public booleanequals(koalasRequest that) {315 if (that == null)316 return false;317

318 boolean this_present_age = true;319 boolean that_present_age = true;320 if (this_present_age ||that_present_age) {321 if (!(this_present_age &&that_present_age))322 return false;323 if (this.age !=that.age)324 return false;325 }326

327 boolean this_present_name = true && this.isSetName();328 boolean that_present_name = true &&that.isSetName();329 if (this_present_name ||that_present_name) {330 if (!(this_present_name &&that_present_name))331 return false;332 if (!this.name.equals(that.name))333 return false;334 }335

336 boolean this_present_address = true && this.isSetAddress();337 boolean that_present_address = true &&that.isSetAddress();338 if (this_present_address ||that_present_address) {339 if (!(this_present_address &&that_present_address))340 return false;341 if (!this.address.equals(that.address))342 return false;343 }344

345 return true;346 }347

348 @Override349 public inthashCode() {350 return 0;351 }352

353 public intcompareTo(koalasRequest other) {354 if (!getClass().equals(other.getClass())) {355 returngetClass().getName().compareTo(other.getClass().getName());356 }357

358 int lastComparison = 0;359 koalasRequest typedOther =(koalasRequest)other;360

361 lastComparison =Boolean.valueOf(isSetAge()).compareTo(typedOther.isSetAge());362 if (lastComparison != 0) {363 returnlastComparison;364 }365 if(isSetAge()) {366 lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.age, typedOther.age);367 if (lastComparison != 0) {368 returnlastComparison;369 }370 }371 lastComparison =Boolean.valueOf(isSetName()).compareTo(typedOther.isSetName());372 if (lastComparison != 0) {373 returnlastComparison;374 }375 if(isSetName()) {376 lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.name, typedOther.name);377 if (lastComparison != 0) {378 returnlastComparison;379 }380 }381 lastComparison =Boolean.valueOf(isSetAddress()).compareTo(typedOther.isSetAddress());382 if (lastComparison != 0) {383 returnlastComparison;384 }385 if(isSetAddress()) {386 lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.address, typedOther.address);387 if (lastComparison != 0) {388 returnlastComparison;389 }390 }391 return 0;392 }393

394 public _Fields fieldForId(intfieldId) {395 return_Fields.findByThriftId(fieldId);396 }397

398 public void read(org.apache.thrift.protocol.TProtocol iprot) throwsorg.apache.thrift.TException {399 schemes.get(iprot.getScheme()).getScheme().read(iprot, this);400 }401

402 public void write(org.apache.thrift.protocol.TProtocol oprot) throwsorg.apache.thrift.TException {403 schemes.get(oprot.getScheme()).getScheme().write(oprot, this);404 }405

406 @Override407 publicString toString() {408 StringBuilder sb = new StringBuilder("koalasRequest(");409 boolean first = true;410

411 sb.append("age:");412 sb.append(this.age);413 first = false;414 if (!first) sb.append(", ");415 sb.append("name:");416 if (this.name == null) {417 sb.append("null");418 } else{419 sb.append(this.name);420 }421 first = false;422 if (!first) sb.append(", ");423 sb.append("address:");424 if (this.address == null) {425 sb.append("null");426 } else{427 sb.append(this.address);428 }429 first = false;430 sb.append(")");431 returnsb.toString();432 }433

434 public void validate() throwsorg.apache.thrift.TException {435 //check for required fields

436 }437

438 private void writeObject(java.io.ObjectOutputStream out) throwsjava.io.IOException {439 try{440 write(new org.apache.thrift.protocol.TCompactProtocol(neworg.apache.thrift.transport.TIOStreamTransport(out)));441 } catch(org.apache.thrift.TException te) {442 throw newjava.io.IOException(te);443 }444 }445

446 private void readObject(java.io.ObjectInputStream in) throwsjava.io.IOException, ClassNotFoundException {447 try{448 //it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor.

449 __isset_bit_vector = new BitSet(1);450 read(new org.apache.thrift.protocol.TCompactProtocol(neworg.apache.thrift.transport.TIOStreamTransport(in)));451 } catch(org.apache.thrift.TException te) {452 throw newjava.io.IOException(te);453 }454 }455

456 private static class koalasRequestStandardSchemeFactory implementsSchemeFactory {457 publickoalasRequestStandardScheme getScheme() {458 return newkoalasRequestStandardScheme();459 }460 }461

462 private static class koalasRequestStandardScheme extends StandardScheme{463

464 public void read(org.apache.thrift.protocol.TProtocol iprot, koalasRequest struct) throwsorg.apache.thrift.TException {465 org.apache.thrift.protocol.TField schemeField;466 iprot.readStructBegin();467 while (true)468 {469 schemeField =iprot.readFieldBegin();470 if (schemeField.type ==org.apache.thrift.protocol.TType.STOP) {471 break;472 }473 switch(schemeField.id) {474 case 1: //AGE

475 if (schemeField.type ==org.apache.thrift.protocol.TType.I32) {476 struct.age =iprot.readI32();477 struct.setAgeIsSet(true);478 } else{479 org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);480 }481 break;482 case 2: //NAME

483 if (schemeField.type ==org.apache.thrift.protocol.TType.STRING) {484 struct.name =iprot.readString();485 struct.setNameIsSet(true);486 } else{487 org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);488 }489 break;490 case 3: //ADDRESS

491 if (schemeField.type ==org.apache.thrift.protocol.TType.STRING) {492 struct.address =iprot.readString();493 struct.setAddressIsSet(true);494 } else{495 org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);496 }497 break;498 default:499 org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);500 }501 iprot.readFieldEnd();502 }503 iprot.readStructEnd();504

505 //check for required fields of primitive type, which can't be checked in the validate method

506 struct.validate();507 }508

509 public void write(org.apache.thrift.protocol.TProtocol oprot, koalasRequest struct) throwsorg.apache.thrift.TException {510 struct.validate();511

512 oprot.writeStructBegin(STRUCT_DESC);513 oprot.writeFieldBegin(AGE_FIELD_DESC);514 oprot.writeI32(struct.age);515 oprot.writeFieldEnd();516 if (struct.name != null) {517 oprot.writeFieldBegin(NAME_FIELD_DESC);518 oprot.writeString(struct.name);519 oprot.writeFieldEnd();520 }521 if (struct.address != null) {522 oprot.writeFieldBegin(ADDRESS_FIELD_DESC);523 oprot.writeString(struct.address);524 oprot.writeFieldEnd();525 }526 oprot.writeFieldStop();527 oprot.writeStructEnd();528 }529

530 }531

532 private static class koalasRequestTupleSchemeFactory implementsSchemeFactory {533 publickoalasRequestTupleScheme getScheme() {534 return newkoalasRequestTupleScheme();535 }536 }537

538 private static class koalasRequestTupleScheme extends TupleScheme{539

540 @Override541 public void write(org.apache.thrift.protocol.TProtocol prot, koalasRequest struct) throwsorg.apache.thrift.TException {542 TTupleProtocol oprot =(TTupleProtocol) prot;543 BitSet optionals = newBitSet();544 if(struct.isSetAge()) {545 optionals.set(0);546 }547 if(struct.isSetName()) {548 optionals.set(1);549 }550 if(struct.isSetAddress()) {551 optionals.set(2);552 }553 oprot.writeBitSet(optionals, 3);554 if(struct.isSetAge()) {555 oprot.writeI32(struct.age);556 }557 if(struct.isSetName()) {558 oprot.writeString(struct.name);559 }560 if(struct.isSetAddress()) {561 oprot.writeString(struct.address);562 }563 }564

565 @Override566 public void read(org.apache.thrift.protocol.TProtocol prot, koalasRequest struct) throwsorg.apache.thrift.TException {567 TTupleProtocol iprot =(TTupleProtocol) prot;568 BitSet incoming = iprot.readBitSet(3);569 if (incoming.get(0)) {570 struct.age =iprot.readI32();571 struct.setAgeIsSet(true);572 }573 if (incoming.get(1)) {574 struct.name =iprot.readString();575 struct.setNameIsSet(true);576 }577 if (incoming.get(2)) {578 struct.address =iprot.readString();579 struct.setAddressIsSet(true);580 }581 }582 }583

584 }