All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adgear.anoa.read.ThriftRecordWrapper Maven / Gradle / Ivy

package com.adgear.anoa.read;

import com.adgear.anoa.AnoaJacksonTypeException;

import org.apache.thrift.TBase;
import org.apache.thrift.TFieldIdEnum;

import java.util.List;

class ThriftRecordWrapper>
    implements RecordWrapper> {

  final protected T record;
  final protected List> fieldWrappers;
  final protected int nRequired;
  private int n = 0;

  ThriftRecordWrapper(T record, List> fieldWrappers, int nRequired) {
    this.record = record;
    this.fieldWrappers = fieldWrappers;
    this.nRequired = nRequired;
  }

  @Override
  public void put(ThriftFieldWrapper fieldWrapper, Object value) {
    F field = fieldWrapper.tFieldIdEnum;
    if (fieldWrapper.isRequired) {
      ++n;
    }
    record.setFieldValue(field, (value == null) ? fieldWrapper.defaultValueSupplier.get() : value);
  }

  @Override
  public T get() {
    if (n < nRequired) {
      for (ThriftFieldWrapper fieldWrapper : fieldWrappers) {
        if (fieldWrapper.isRequired && !record.isSet(fieldWrapper.tFieldIdEnum)) {
          throw new AnoaJacksonTypeException(
              "Required field not set: " + fieldWrapper.tFieldIdEnum.getFieldName());
        }
      }
    }
    return record;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy