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

wavefront.templates.record.vm Maven / Gradle / Ivy

There is a newer version: 2023-22.3
Show newest version
##
## Licensed to the Apache Software Foundation (ASF) under one
## or more contributor license agreements.  See the NOTICE file
## distributed with this work for additional information
## regarding copyright ownership.  The ASF licenses this file
## to you under the Apache License, Version 2.0 (the
## "License"); you may not use this file except in compliance
## with the License.  You may obtain a copy of the License at
##
##     https://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
#if ($schema.getNamespace())
package $schema.getNamespace();
#end

import org.apache.avro.generic.GenericArray;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.util.Utf8;
#if (!$schema.isError())
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
#end
#if (${this.gettersReturnOptional} || ${this.createOptionalGetters})import java.util.Optional;#end

#if ($schema.getDoc())
/** $schema.getDoc() */
#end
#foreach ($annotation in $this.javaAnnotations($schema))
@$annotation
#end
@org.apache.avro.specific.AvroGenerated
public class ${this.mangle($schema.getName())}#if ($schema.isError()) extends org.apache.avro.specific.SpecificExceptionBase#else extends org.apache.avro.specific.SpecificRecordBase#end implements org.apache.avro.specific.SpecificRecord {
  private static final long serialVersionUID = ${this.fingerprint64($schema)}L;
  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse(${this.javaSplit($schema.toString())});
  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }

  private static SpecificData MODEL$ = new SpecificData();
#set ($usedConversions = $this.getUsedConversionClasses($schema))
#if (!$usedConversions.isEmpty())
static {
#foreach ($conversion in $usedConversions)
    MODEL$.addLogicalTypeConversion(new ${conversion}());
#end
  }
#end

#if (!$schema.isError())
  private static final BinaryMessageEncoder<${this.mangle($schema.getName())}> ENCODER =
      new BinaryMessageEncoder<${this.mangle($schema.getName())}>(MODEL$, SCHEMA$);

  private static final BinaryMessageDecoder<${this.mangle($schema.getName())}> DECODER =
      new BinaryMessageDecoder<${this.mangle($schema.getName())}>(MODEL$, SCHEMA$);

  /**
   * Return the BinaryMessageEncoder instance used by this class.
   * @return the message encoder used by this class
   */
  public static BinaryMessageEncoder<${this.mangle($schema.getName())}> getEncoder() {
    return ENCODER;
  }

  /**
   * Return the BinaryMessageDecoder instance used by this class.
   * @return the message decoder used by this class
   */
  public static BinaryMessageDecoder<${this.mangle($schema.getName())}> getDecoder() {
    return DECODER;
  }

  /**
   * Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
   * @param resolver a {@link SchemaStore} used to find schemas by fingerprint
   * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
   */
  public static BinaryMessageDecoder<${this.mangle($schema.getName())}> createDecoder(SchemaStore resolver) {
    return new BinaryMessageDecoder<${this.mangle($schema.getName())}>(MODEL$, SCHEMA$, resolver);
  }

  /**
   * Serializes this ${schema.getName()} to a ByteBuffer.
   * @return a buffer holding the serialized data for this instance
   * @throws java.io.IOException if this instance could not be serialized
   */
  public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
    return ENCODER.encode(this);
  }

  /**
   * Deserializes a ${schema.getName()} from a ByteBuffer.
   * @param b a byte buffer holding serialized data for an instance of this class
   * @return a ${schema.getName()} instance decoded from the given buffer
   * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
   */
  public static ${this.mangle($schema.getName())} fromByteBuffer(
      java.nio.ByteBuffer b) throws java.io.IOException {
    return DECODER.decode(b);
  }
#end

#foreach ($field in $schema.getFields())
#if ($field.doc())
  /** $field.doc() */
#end
#foreach ($annotation in $this.javaAnnotations($field))
  @$annotation
#end
  #if (${this.deprecatedFields()})@Deprecated#end #if (${this.publicFields()})public#elseif (${this.privateFields()})private#end ${this.javaUnbox($field.schema())} ${this.mangle($field.name(), $schema.isError())};
#end
#if ($schema.isError())

  public ${this.mangle($schema.getName())}() {
    super();
  }

  public ${this.mangle($schema.getName())}(Object value) {
    super(value);
  }

  public ${this.mangle($schema.getName())}(Throwable cause) {
    super(cause);
  }

  public ${this.mangle($schema.getName())}(Object value, Throwable cause) {
    super(value, cause);
  }

#else
#if ($schema.getFields().size() > 0)

  /**
   * Default constructor.  Note that this does not initialize fields
   * to their default values from the schema.  If that is desired then
   * one should use newBuilder().
   */
  public ${this.mangle($schema.getName())}() {}
#if ($this.isCreateAllArgsConstructor())

  /**
   * All-args constructor.
#foreach ($field in $schema.getFields())
#if ($field.doc())   * @param ${this.mangle($field.name())} $field.doc()
#else   * @param ${this.mangle($field.name())} The new value for ${field.name()}
#end
#end
   */
  public ${this.mangle($schema.getName())}(#foreach($field in $schema.getFields())${this.javaType($field.schema())} ${this.mangle($field.name())}#if($foreach.count < $schema.getFields().size()), #end#end) {
#foreach ($field in $schema.getFields())
    ${this.generateSetterCode($field.schema(), ${this.mangle($field.name())}, ${this.mangle($field.name())})}
#end
  }
#else
  /**
   * This schema contains more than 254 fields which exceeds the maximum number
   * of permitted constructor parameters in the JVM. An all-args constructor
   * will not be generated. Please use newBuilder() to instantiate
   * objects instead.
   */
#end
#end

#end
  public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
  public org.apache.avro.Schema getSchema() { return SCHEMA$; }
  // Used by DatumWriter.  Applications should not call.
  public java.lang.Object get(int field$) {
    switch (field$) {
#set ($i = 0)
#foreach ($field in $schema.getFields())
    case $i: return ${this.mangle($field.name(), $schema.isError())};
#set ($i = $i + 1)
#end
    default: throw new org.apache.avro.AvroRuntimeException("Bad index");
    }
  }

#if ($this.hasLogicalTypeField($schema))
  private static final org.apache.avro.Conversion[] conversions =
      new org.apache.avro.Conversion[] {
#foreach ($field in $schema.getFields())
      ${this.conversionInstance($field.schema())},
#end
      null
  };

  @Override
  public org.apache.avro.Conversion getConversion(int field) {
    return conversions[field];
  }

#end
  // Used by DatumReader.  Applications should not call.
  @SuppressWarnings(value="unchecked")
  public void put(int field$, java.lang.Object value$) {
    switch (field$) {
#set ($i = 0)
#foreach ($field in $schema.getFields())
    case $i: ${this.mangle($field.name(), $schema.isError())} = #if(${this.javaType($field.schema())} != "java.lang.Object" && ${this.javaType($field.schema())} != "java.lang.String")(${this.javaType($field.schema())})#{end}value$#if(${this.javaType($field.schema())} == "java.lang.String") != null ? value$.toString() : null#{end}; break;
#set ($i = $i + 1)
#end
    default: throw new org.apache.avro.AvroRuntimeException("Bad index");
    }
  }

#foreach ($field in $schema.getFields())
#if (${this.gettersReturnOptional})
  /**
   * Gets the value of the '${this.mangle($field.name(), $schema.isError())}' field as an Optional<${this.javaType($field.schema())}>.
#if ($field.doc())      * $field.doc()
#end
   * @return The value wrapped in an Optional<${this.javaType($field.schema())}>.
   */
  public Optional<${this.javaType($field.schema())}> ${this.generateGetMethod($schema, $field)}() {
    return Optional.<${this.javaType($field.schema())}>ofNullable(${this.mangle($field.name(), $schema.isError())});
  }
#else
  /**
   * Gets the value of the '${this.mangle($field.name(), $schema.isError())}' field.
#if ($field.doc())   * @return $field.doc()
#else   * @return The value of the '${this.mangle($field.name(), $schema.isError())}' field.
#end
   */
  public ${this.javaUnbox($field.schema())} ${this.generateGetMethod($schema, $field)}() {
    return ${this.mangle($field.name(), $schema.isError())};
  }
#end

#if (${this.createOptionalGetters})
  /**
   * Gets the value of the '${this.mangle($field.name(), $schema.isError())}' field as an Optional<${this.javaType($field.schema())}>.
#if ($field.doc())      * $field.doc()
#end
   * @return The value wrapped in an Optional<${this.javaType($field.schema())}>.
   */
  public Optional<${this.javaType($field.schema())}> ${this.generateGetOptionalMethod($schema, $field)}() {
    return Optional.<${this.javaType($field.schema())}>ofNullable(${this.mangle($field.name(), $schema.isError())});
  }
#end

#if ($this.createSetters)
  /**
   * Sets the value of the '${this.mangle($field.name(), $schema.isError())}' field.
#if ($field.doc())   * $field.doc()
#end
   * @param value the value to set.
   */
    #if ($field.schema().getType().name() == "UNION" && $this.javaType($field.schema()) == "java.lang.Object")
        #foreach ($type in $field.schema().getTypes())
        public void ${this.generateSetMethod($schema, $field)}(${this.javaType($type)} value) {
        this.${this.mangle($field.name(), $schema.isError())} = value;
        }
        #end
    #else
  public void ${this.generateSetMethod($schema, $field)}(${this.javaUnbox($field.schema())} value) {
    ${this.generateSetterCode($field.schema(), ${this.mangle($field.name(), $schema.isError())}, "value")}
  }
  #end
#end

#end
  /**
   * Creates a new ${this.mangle($schema.getName())} RecordBuilder.
   * @return A new ${this.mangle($schema.getName())} RecordBuilder
   */
  public static #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder newBuilder() {
    return new #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder();
  }

  /**
   * Creates a new ${this.mangle($schema.getName())} RecordBuilder by copying an existing Builder.
   * @param other The existing builder to copy.
   * @return A new ${this.mangle($schema.getName())} RecordBuilder
   */
  public static #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder newBuilder(#if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder other) {
    if (other == null) {
      return new #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder();
    } else {
      return new #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder(other);
    }
  }

  /**
   * Creates a new ${this.mangle($schema.getName())} RecordBuilder by copying an existing $this.mangle($schema.getName()) instance.
   * @param other The existing instance to copy.
   * @return A new ${this.mangle($schema.getName())} RecordBuilder
   */
  public static #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder newBuilder(#if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())} other) {
    if (other == null) {
      return new #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder();
    } else {
      return new #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder(other);
    }
  }

  /**
   * RecordBuilder for ${this.mangle($schema.getName())} instances.
   */
  @org.apache.avro.specific.AvroGenerated
  public static class Builder extends#if ($schema.isError()) org.apache.avro.specific.SpecificErrorBuilderBase<${this.mangle($schema.getName())}>#else org.apache.avro.specific.SpecificRecordBuilderBase<${this.mangle($schema.getName())}>#end

    implements#if ($schema.isError()) org.apache.avro.data.ErrorBuilder<${this.mangle($schema.getName())}>#else org.apache.avro.data.RecordBuilder<${this.mangle($schema.getName())}>#end {

#foreach ($field in $schema.getFields())
#if ($field.doc())
    /** $field.doc() */
#end
    private ${this.javaUnbox($field.schema())} ${this.mangle($field.name(), $schema.isError())};
#if (${this.hasBuilder($field.schema())})
    private ${this.javaUnbox($field.schema())}.Builder ${this.mangle($field.name(), $schema.isError())}Builder;
#end
#end

    /** Creates a new Builder */
    private Builder() {
      super(SCHEMA$);
    }

    /**
     * Creates a Builder by copying an existing Builder.
     * @param other The existing Builder to copy.
     */
    private Builder(#if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder other) {
      super(other);
#foreach ($field in $schema.getFields())
      if (isValidValue(fields()[$field.pos()], other.${this.mangle($field.name(), $schema.isError())})) {
        this.${this.mangle($field.name(), $schema.isError())} = data().deepCopy(fields()[$field.pos()].schema(), other.${this.mangle($field.name(), $schema.isError())});
        fieldSetFlags()[$field.pos()] = other.fieldSetFlags()[$field.pos()];
      }
#if (${this.hasBuilder($field.schema())})
      if (other.${this.generateHasBuilderMethod($schema, $field)}()) {
        this.${this.mangle($field.name(), $schema.isError())}Builder = ${this.javaType($field.schema())}.newBuilder(other.${this.generateGetBuilderMethod($schema, $field)}());
      }
#end
#end
    }

    /**
     * Creates a Builder by copying an existing $this.mangle($schema.getName()) instance
     * @param other The existing instance to copy.
     */
    private Builder(#if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())} other) {
#if ($schema.isError())      super(other)#else
      super(SCHEMA$)#end;
#foreach ($field in $schema.getFields())
      if (isValidValue(fields()[$field.pos()], other.${this.mangle($field.name(), $schema.isError())})) {
        this.${this.mangle($field.name(), $schema.isError())} = data().deepCopy(fields()[$field.pos()].schema(), other.${this.mangle($field.name(), $schema.isError())});
        fieldSetFlags()[$field.pos()] = true;
      }
#if (${this.hasBuilder($field.schema())})
      this.${this.mangle($field.name(), $schema.isError())}Builder = null;
#end
#end
    }
#if ($schema.isError())

    @Override
    public #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder setValue(Object value) {
      super.setValue(value);
      return this;
    }

    @Override
    public #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder clearValue() {
      super.clearValue();
      return this;
    }

    @Override
    public #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder setCause(Throwable cause) {
      super.setCause(cause);
      return this;
    }

    @Override
    public #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder clearCause() {
      super.clearCause();
      return this;
    }
#end

#foreach ($field in $schema.getFields())
    /**
      * Gets the value of the '${this.mangle($field.name(), $schema.isError())}' field.
#if ($field.doc())      * $field.doc()
#end
      * @return The value.
      */
    public ${this.javaUnbox($field.schema())} ${this.generateGetMethod($schema, $field)}() {
      return ${this.mangle($field.name(), $schema.isError())};
    }
    #if ($field.schema().getType().name() == "UNION" && $this.javaType($field.schema()) == "java.lang.Object")
        #foreach ($type in $field.schema().getTypes())

        /**
        * Sets the value of the '${this.mangle($field.name(), $schema.isError())}' field.
            #if ($field.doc())     * $field.doc()
            #end
        * @param value The value of '${this.mangle($field.name(), $schema.isError())}'.
        * @return This builder.
        */
        public #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder ${this.generateSetMethod($schema, $field)}(${this.javaUnbox($type)} value) {
        validate(fields()[$field.pos()], value);
            #if (${this.hasBuilder($field.schema())})
            this.${this.mangle($field.name(), $schema.isError())}Builder = null;
            #end
        this.${this.mangle($field.name(), $schema.isError())} = value;
        fieldSetFlags()[$field.pos()] = true;
        return this;
        }
        #end
    #else

    /**
      * Sets the value of the '${this.mangle($field.name(), $schema.isError())}' field.
#if ($field.doc())      * $field.doc()
#end
      * @param value The value of '${this.mangle($field.name(), $schema.isError())}'.
      * @return This builder.
      */
    public #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder ${this.generateSetMethod($schema, $field)}(${this.javaUnbox($field.schema())} value) {
      validate(fields()[$field.pos()], value);
#if (${this.hasBuilder($field.schema())})
      this.${this.mangle($field.name(), $schema.isError())}Builder = null;
#end
      ${this.generateSetterCode($field.schema(), ${this.mangle($field.name(), $schema.isError())}, "value")}
      fieldSetFlags()[$field.pos()] = true;
      return this;
    }
    #end

    #if (${this.createOptionalGetters})
    /**
    * Gets the value of the '${this.mangle($field.name(), $schema.isError())}' field as an Optional<${this.javaType($field.schema())}>.
        #if ($field.doc())      * $field.doc()
        #end
    * @return The Optional<value>.
    */
    public Optional<${this.javaType($field.schema())}> ${this.generateGetOptionalMethod($schema, $field)}() {
    return Optional.<${this.javaType($field.schema())}>ofNullable(${this.mangle($field.name(), $schema.isError())});
    }
    #end

    /**
      * Checks whether the '${this.mangle($field.name(), $schema.isError())}' field has been set.
#if ($field.doc())      * $field.doc()
#end
      * @return True if the '${this.mangle($field.name(), $schema.isError())}' field has been set, false otherwise.
      */
    public boolean ${this.generateHasMethod($schema, $field)}() {
      return fieldSetFlags()[$field.pos()];
    }

#if (${this.hasBuilder($field.schema())})
    /**
     * Gets the Builder instance for the '${this.mangle($field.name(), $schema.isError())}' field and creates one if it doesn't exist yet.
#if ($field.doc())     * $field.doc()
#end
     * @return This builder.
     */
    public ${this.javaType($field.schema())}.Builder ${this.generateGetBuilderMethod($schema, $field)}() {
      if (${this.mangle($field.name(), $schema.isError())}Builder == null) {
        if (${this.generateHasMethod($schema, $field)}()) {
          ${this.generateSetBuilderMethod($schema, $field)}(${this.javaType($field.schema())}.newBuilder(${this.mangle($field.name(), $schema.isError())}));
        } else {
          ${this.generateSetBuilderMethod($schema, $field)}(${this.javaType($field.schema())}.newBuilder());
        }
      }
      return ${this.mangle($field.name(), $schema.isError())}Builder;
    }

    /**
     * Sets the Builder instance for the '${this.mangle($field.name(), $schema.isError())}' field
#if ($field.doc())     * $field.doc()
#end
     * @param value The builder instance that must be set.
     * @return This builder.
     */
    public #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder ${this.generateSetBuilderMethod($schema, $field)}(${this.javaUnbox($field.schema())}.Builder value) {
      ${this.generateClearMethod($schema, $field)}();
      ${this.mangle($field.name(), $schema.isError())}Builder = value;
      return this;
    }

    /**
     * Checks whether the '${this.mangle($field.name(), $schema.isError())}' field has an active Builder instance
#if ($field.doc())     * $field.doc()
#end
     * @return True if the '${this.mangle($field.name(), $schema.isError())}' field has an active Builder instance
     */
    public boolean ${this.generateHasBuilderMethod($schema, $field)}() {
      return ${this.mangle($field.name(), $schema.isError())}Builder != null;
    }
#end

    /**
      * Clears the value of the '${this.mangle($field.name(), $schema.isError())}' field.
#if ($field.doc())      * $field.doc()
#end
      * @return This builder.
      */
    public #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder ${this.generateClearMethod($schema, $field)}() {
#if (${this.isUnboxedJavaTypeNullable($field.schema())})
      ${this.mangle($field.name(), $schema.isError())} = null;
#end
#if (${this.hasBuilder($field.schema())})
      ${this.mangle($field.name(), $schema.isError())}Builder = null;
#end
      fieldSetFlags()[$field.pos()] = false;
      return this;
    }

#end
    @Override
    @SuppressWarnings("unchecked")
    public ${this.mangle($schema.getName())} build() {
      try {
        ${this.mangle($schema.getName())} record = new ${this.mangle($schema.getName())}(#if ($schema.isError())getValue(), getCause()#end);
#foreach ($field in $schema.getFields())
#if (${this.hasBuilder($field.schema())})
        if (${this.mangle($field.name(), $schema.isError())}Builder != null) {
          try {
            record.${this.mangle($field.name(), $schema.isError())} = this.${this.mangle($field.name(), $schema.isError())}Builder.build();
          } catch (org.apache.avro.AvroMissingFieldException e) {
            e.addParentField(record.getSchema().getField("${this.mangle($field.name(), $schema.isError())}"));
            throw e;
          }
        } else {
          record.${this.mangle($field.name(), $schema.isError())} = fieldSetFlags()[$field.pos()] ? this.${this.mangle($field.name(), $schema.isError())} : #if(${this.javaType($field.schema())} != "java.lang.Object")(${this.javaType($field.schema())})#{end} defaultValue(fields()[$field.pos()]);
        }
#else
        record.${this.mangle($field.name(), $schema.isError())} = fieldSetFlags()[$field.pos()] ? this.${this.mangle($field.name(), $schema.isError())} : #if(${this.javaType($field.schema())} != "java.lang.Object")(${this.javaType($field.schema())})#{end} defaultValue(fields()[$field.pos()]);
#end
#end
        return record;
      } catch (org.apache.avro.AvroMissingFieldException e) {
        throw e;
      } catch (java.lang.Exception e) {
        throw new org.apache.avro.AvroRuntimeException(e);
      }
    }
  }

  @SuppressWarnings("unchecked")
  private static final org.apache.avro.io.DatumWriter<${this.mangle($schema.getName())}>
    WRITER$ = (org.apache.avro.io.DatumWriter<${this.mangle($schema.getName())}>)MODEL$.createDatumWriter(SCHEMA$);

  @Override public void writeExternal(java.io.ObjectOutput out)
    throws java.io.IOException {
    WRITER$.write(this, SpecificData.getEncoder(out));
  }

  @SuppressWarnings("unchecked")
  private static final org.apache.avro.io.DatumReader<${this.mangle($schema.getName())}>
    READER$ = (org.apache.avro.io.DatumReader<${this.mangle($schema.getName())}>)MODEL$.createDatumReader(SCHEMA$);

  @Override public void readExternal(java.io.ObjectInput in)
    throws java.io.IOException {
    READER$.read(this, SpecificData.getDecoder(in));
  }

#if ($this.isCustomCodable($schema))
  @Override protected boolean hasCustomCoders() { return true; }

  @Override public void customEncode(org.apache.avro.io.Encoder out)
    throws java.io.IOException
  {
#set ($nv = 0)## Counter to ensure unique var-names
#set ($maxnv = 0)## Holds high-water mark during recursion
#foreach ($field in $schema.getFields())
#set ($n = $this.mangle($field.name(), $schema.isError()))
#set ($s = $field.schema())
#encodeVar(0 "this.${n}" $s)

#set ($nv = $maxnv)
#end
  }

  @Override public void customDecode(org.apache.avro.io.ResolvingDecoder in)
    throws java.io.IOException
  {
    org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
    if (fieldOrder == null) {
## Common case: order of fields hasn't changed, so read them in a
## fixed order according to reader's schema
#set ($nv = 0)## Counter to ensure unique var-names
#set ($maxnv = 0)## Holds high-water mark during recursion
#foreach ($field in $schema.getFields())
#set ($n = $this.mangle($field.name(), $schema.isError()))
#set ($s = $field.schema())
#set ($rs = "SCHEMA$.getField(""${n}"").schema()")
#decodeVar(2 "this.${n}" $s $rs)

#set ($nv = $maxnv)
#end
    } else {
      for (int i = 0; i < $schema.getFields().size(); i++) {
        switch (fieldOrder[i].pos()) {
#set ($fieldno = 0)
#set ($nv = 0)## Counter to ensure unique var-names
#set ($maxnv = 0)## Holds high-water mark during recursion
#foreach ($field in $schema.getFields())
        case $fieldno:
#set ($n = $this.mangle($field.name(), $schema.isError()))
#set ($s = $field.schema())
#set ($rs = "SCHEMA$.getField(""${n}"").schema()")
#decodeVar(6 "this.${n}" $s $rs)
          break;

#set ($nv = $maxnv)
#set ($fieldno = $fieldno + 1)
#end
        default:
          throw new java.io.IOException("Corrupt ResolvingDecoder.");
        }
      }
    }
  }
#end
}

#macro( encodeVar $indent $var $s )
#set ($I = $this.indent($indent))
##### Compound types (array, map, and union) require calls
##### that will recurse back into this encodeVar macro:
#if ($s.Type.Name.equals("array"))
#encodeArray($indent $var $s)
#elseif ($s.Type.Name.equals("map"))
#encodeMap($indent $var $s)
#elseif ($s.Type.Name.equals("union"))
#encodeUnion($indent $var $s)
##### Use the generated "encode" method as fast way to write
##### (specific) record types:
#elseif ($s.Type.Name.equals("record"))
$I    ${var}.customEncode(out);
##### For rest of cases, generate calls out.writeXYZ:
#elseif ($s.Type.Name.equals("null"))
$I    out.writeNull();
#elseif ($s.Type.Name.equals("boolean"))
$I    out.writeBoolean(${var});
#elseif ($s.Type.Name.equals("int"))
$I    out.writeInt(${var});
#elseif ($s.Type.Name.equals("long"))
$I    out.writeLong(${var});
#elseif ($s.Type.Name.equals("float"))
$I    out.writeFloat(${var});
#elseif ($s.Type.Name.equals("double"))
$I    out.writeDouble(${var});
#elseif ($s.Type.Name.equals("string"))
#if ($this.isStringable($s))
$I    out.writeString(${var}.toString());
#else
$I    out.writeString(${var});
#end
#elseif ($s.Type.Name.equals("bytes"))
$I    out.writeBytes(${var});
#elseif ($s.Type.Name.equals("fixed"))
$I    out.writeFixed(${var}.bytes(), 0, ${s.FixedSize});
#elseif ($s.Type.Name.equals("enum"))
$I    out.writeEnum(${var}.ordinal());
#else
## TODO -- singal a code-gen-time error
#end
#end

#macro( encodeArray $indent $var $s )
#set ($I = $this.indent($indent))
#set ($et = $this.javaType($s.ElementType))
$I    long size${nv} = ${var}.size();
$I    out.writeArrayStart();
$I    out.setItemCount(size${nv});
$I    long actualSize${nv} = 0;
$I    for ($et e${nv}: ${var}) {
$I      actualSize${nv}++;
$I      out.startItem();
#set ($var = "e${nv}")
#set ($nv = $nv + 1)
#set ($maxnv = $nv)
#set ($indent = $indent + 2)
#encodeVar($indent $var $s.ElementType)
#set ($nv = $nv - 1)
#set ($indent = $indent - 2)
#set ($I = $this.indent($indent))
$I    }
$I    out.writeArrayEnd();
$I    if (actualSize${nv} != size${nv})
$I      throw new java.util.ConcurrentModificationException("Array-size written was " + size${nv} + ", but element count was " + actualSize${nv} + ".");
#end

#macro( encodeMap $indent $var $s )
#set ($I = $this.indent($indent))
#set ($kt = $this.getStringType($s))
#set ($vt = $this.javaType($s.ValueType))
$I    long size${nv} = ${var}.size();
$I    out.writeMapStart();
$I    out.setItemCount(size${nv});
$I    long actualSize${nv} = 0;
$I    for (java.util.Map.Entry<$kt, $vt> e${nv}: ${var}.entrySet()) {
$I      actualSize${nv}++;
$I      out.startItem();
#if ($this.isStringable($s))
$I      out.writeString(e${nv}.getKey().toString());
#else
$I      out.writeString(e${nv}.getKey());
#end
$I      $vt v${nv} = e${nv}.getValue();
#set ($var = "v${nv}")
#set ($nv = $nv + 1)
#set ($maxnv = $nv)
#set ($indent = $indent + 2)
#encodeVar($indent $var $s.ValueType)
#set ($nv = $nv - 1)
#set ($indent = $indent - 2)
#set ($I = $this.indent($indent))
$I    }
$I    out.writeMapEnd();
$I    if (actualSize${nv} != size${nv})
      throw new java.util.ConcurrentModificationException("Map-size written was " + size${nv} + ", but element count was " + actualSize${nv} + ".");
#end

#macro( encodeUnion $indent $var $s )
#set ($I = $this.indent($indent))
#set ($et = $this.javaType($s.Types.get($this.getNonNullIndex($s))))
$I    if (${var} == null) {
$I      out.writeIndex(#if($this.getNonNullIndex($s)==0)1#{else}0#end);
$I      out.writeNull();
$I    } else {
$I      out.writeIndex(${this.getNonNullIndex($s)});
#set ($indent = $indent + 2)
#encodeVar($indent $var $s.Types.get($this.getNonNullIndex($s)))
#set ($indent = $indent - 2)
#set ($I = $this.indent($indent))
$I    }
#end


#macro( decodeVar $indent $var $s $rs )
#set ($I = $this.indent($indent))
##### Compound types (array, map, and union) require calls
##### that will recurse back into this decodeVar macro:
#if ($s.Type.Name.equals("array"))
#decodeArray($indent $var $s $rs)
#elseif ($s.Type.Name.equals("map"))
#decodeMap($indent $var $s $rs)
#elseif ($s.Type.Name.equals("union"))
#decodeUnion($indent $var $s $rs)
##### Use the generated "decode" method as fast way to write
##### (specific) record types:
#elseif ($s.Type.Name.equals("record"))
$I    if (${var} == null) {
$I      ${var} = new ${this.javaType($s)}();
$I    }
$I    ${var}.customDecode(in);
##### For rest of cases, generate calls in.readXYZ:
#elseif ($s.Type.Name.equals("null"))
$I    in.readNull();
#elseif ($s.Type.Name.equals("boolean"))
$I    $var = in.readBoolean();
#elseif ($s.Type.Name.equals("int"))
$I    $var = in.readInt();
#elseif ($s.Type.Name.equals("long"))
$I    $var = in.readLong();
#elseif ($s.Type.Name.equals("float"))
$I    $var = in.readFloat();
#elseif ($s.Type.Name.equals("double"))
$I    $var = in.readDouble();
#elseif ($s.Type.Name.equals("string"))
#decodeString( "$I" $var $s )
#elseif ($s.Type.Name.equals("bytes"))
$I    $var = in.readBytes(${var});
#elseif ($s.Type.Name.equals("fixed"))
$I    if (${var} == null) {
$I      ${var} = new ${this.javaType($s)}();
$I    }
$I    in.readFixed(${var}.bytes(), 0, ${s.FixedSize});
#elseif ($s.Type.Name.equals("enum"))
$I    $var = ${this.javaType($s)}.values()[in.readEnum()];
#else
## TODO -- singal a code-gen-time error
#end
#end

#macro( decodeString $II $var $s )
#set ($st = ${this.getStringType($s)})
#if ($this.isStringable($s))
$II    ${var} = new ${st}(in.readString());
#elseif ($st.equals("java.lang.String"))
$II    $var = in.readString();
#elseif ($st.equals("org.apache.avro.util.Utf8"))
$II    $var = in.readString(${var});
#else
$II    $var = in.readString(${var} instanceof Utf8 ? (Utf8)${var} : null);
#end
#end

#macro( decodeArray $indent $var $s $rs )
#set ($I = $this.indent($indent))
#set ($t = $this.javaType($s))
#set ($et = $this.javaType($s.ElementType))
#set ($gat = "SpecificData.Array<${et}>")
$I    long size${nv} = in.readArrayStart();
## Need fresh variable name due to limitation of macro system
$I    $t a${nv} = ${var};
$I    if (a${nv} == null) {
$I      a${nv} = new ${gat}((int)size${nv}, ${rs});
$I      $var = a${nv};
$I    } else a${nv}.clear();
$I    $gat ga${nv} = (a${nv} instanceof SpecificData.Array ? (${gat})a${nv} : null);
$I    for ( ; 0 < size${nv}; size${nv} = in.arrayNext()) {
$I      for ( ; size${nv} != 0; size${nv}--) {
$I        $et e${nv} = (ga${nv} != null ? ga${nv}.peek() : null);
#set ($var = "e${nv}")
#set ($nv = $nv + 1)
#set ($maxnv = $nv)
#set ($indent = $indent + 4)
#decodeVar($indent $var $s.ElementType "${rs}.getElementType()")
#set ($nv = $nv - 1)
#set ($indent = $indent - 4)
#set ($I = $this.indent($indent))
$I        a${nv}.add(e${nv});
$I      }
$I    }
#end

#macro( decodeMap $indent $var $s $rs )
#set ($I = $this.indent($indent))
#set ($t = $this.javaType($s))
#set ($kt = $this.getStringType($s))
#set ($vt = $this.javaType($s.ValueType))
$I    long size${nv} = in.readMapStart();
$I    $t m${nv} = ${var}; // Need fresh name due to limitation of macro system
$I    if (m${nv} == null) {
$I      m${nv} = new java.util.HashMap<${kt},${vt}>((int)size${nv});
$I      $var = m${nv};
$I    } else m${nv}.clear();
$I    for ( ; 0 < size${nv}; size${nv} = in.mapNext()) {
$I      for ( ; size${nv} != 0; size${nv}--) {
$I        $kt k${nv} = null;
#decodeString( "$I    " "k${nv}" $s )
$I        $vt v${nv} = null;
#set ($var = "v${nv}")
#set ($nv = $nv + 1)
#set ($maxnv = $nv)
#set ($indent = $indent + 4)
#decodeVar($indent $var $s.ValueType "${rs}.getValueType()")
#set ($nv = $nv - 1)
#set ($indent = $indent - 4)
#set ($I = $this.indent($indent))
$I        m${nv}.put(k${nv}, v${nv});
$I      }
$I    }
#end

#macro( decodeUnion $indent $var $s $rs )
#set ($I = $this.indent($indent))
#set ($et = $this.javaType($s.Types.get($this.getNonNullIndex($s))))
#set ($si = $this.getNonNullIndex($s))
$I    if (in.readIndex() != ${si}) {
$I      in.readNull();
$I      ${var} = null;
$I    } else {
#set ($indent = $indent + 2)
#decodeVar($indent $var $s.Types.get($si) "${rs}.getTypes().get(${si})")
#set ($indent = $indent - 2)
#set ($I = $this.indent($indent))
$I    }
#end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy