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

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

Go to download

Core classes for Anoa library, which aims to be a safe, convenient and fast record de/serialization wrapper for the Avro, Thrift and Jackson libraries, using the functional idioms of Java 8. The anoa-core module tries to keep upstream dependencies to a minimum.

There is a newer version: 3.1.2
Show newest version
package com.adgear.anoa.read;

import com.adgear.anoa.AnoaJacksonTypeException;

import org.apache.avro.generic.IndexedRecord;

import java.util.List;

class AvroRecordWrapper {

  final protected R record;
  final protected List fieldWrappers;
  final protected boolean[] flag;

  AvroRecordWrapper(R record, List fieldWrappers) {
    this.record = record;
    this.fieldWrappers = fieldWrappers;
    flag = new boolean[fieldWrappers.size()];
  }

  void put(AvroFieldWrapper fieldWrapper, Object value) {
    flag[fieldWrapper.index] = true;
    if (value != null) {
      record.put(fieldWrapper.field.pos(), value);
    } else if (fieldWrapper.defaultValue == null) {
      if (fieldWrapper.unboxed) {
        throw new AnoaJacksonTypeException(
            "Cannot set unboxed field to null: " + fieldWrapper.field.name());
      }
      record.put(fieldWrapper.field.pos(), null);
    } else {
      record.put(fieldWrapper.field.pos(), fieldWrapper.defaultValueCopy());
    }
  }

  R get() {
    for (AvroFieldWrapper fieldWrapper : fieldWrappers) {
      if (!flag[fieldWrapper.index]) {
        if (fieldWrapper.defaultValue != null) {
          record.put(fieldWrapper.field.pos(), fieldWrapper.defaultValueCopy());
        } else if (fieldWrapper.unboxed) {
          throw new AnoaJacksonTypeException(
              "Cannot leave unboxed field unset: " + fieldWrapper.field.name());
        }
      }
    }
    return record;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy