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

com.google.auto.value.processor.autovalue.vm Maven / Gradle / Ivy

## Template for each generated AutoValue_Foo class.
## This template uses the Apache Velocity Template Language (VTL).
## The variables ($pkg, $props, and so on) are defined by the fields of AutoValueTemplateVars.
##
## Comments, like this one, begin with ##. The comment text extends up to and including the newline
## character at the end of the line. So comments also serve to join a line to the next one.
## Velocity deletes a newline after a directive (#if, #foreach, #end etc) so ## is not needed there.
## That does mean that we sometimes need an extra blank line after such a directive.
##
## A post-processing step will remove unwanted spaces and blank lines, but will not join two lines.

#if (!$pkg.empty)
package $pkg;
#end

#foreach ($i in $imports)
import $i;
#end

${gwtCompatibleAnnotation}
#foreach ($a in $annotations)
$a
#end
#if ($generated != "")
@${generated}("com.google.auto.value.processor.AutoValueProcessor")
#end
#if ($isFinal) final #else abstract #end class $subclass$formalTypes extends $origClass$actualTypes {

## Fields

#foreach ($p in $props)
  private final $p.type $p;
#end

## Constructor

#if ($isFinal && $builderTypeName != "")
  private ##
#end
  $subclass(
#foreach ($p in $props)

      ${p.nullableAnnotation}$p.type $p #if ($foreach.hasNext) , #end
#end ) {
#foreach ($p in $props)
  #if (!$p.kind.primitive && !$p.nullable && ($builderTypeName == "" || !$isFinal))
    ## We don't need a null check if the type is primitive or @Nullable. We also don't need it
    ## if there is a builder, since the build() method will check for us. However, if there is a
    ## builder but there are also extensions (!$isFinal) then we can't omit the null check because
    ## the constructor is called from the extension code.

    if ($p == null) {
      throw new NullPointerException("Null $p.name");
    }

  #end

    this.$p = $p;
#end
  }

## Property getters

#foreach ($p in $props)

  #foreach ($a in ${p.annotations})

  ${a}##
  #end

  @Override
  ${p.access}${p.type} ${p.getter}() {
    return $p;
  }

#end

#if ($toString)

  @Override
  public String toString() {
    return "$simpleClassName{"

  #foreach ($p in $props)

        + "$p.name=" ##
        + #if ($p.kind == "ARRAY") ${arrays}.toString($p) #else $p #end
        #if ($foreach.hasNext) + ", " #end

  #end

        + "}";
  }

#end

#if ($equals)
  #macro (equalsThatExpression $p)
    #if ($p.kind == "FLOAT")
      Float.floatToIntBits(this.$p) == Float.floatToIntBits(that.${p.getter}()) ##
    #elseif ($p.kind == "DOUBLE")
      Double.doubleToLongBits(this.$p) == Double.doubleToLongBits(that.${p.getter}()) ##
    #elseif ($p.kind.primitive)
      this.$p == that.${p.getter}() ##
    #elseif ($p.kind == "ARRAY")
      ${arrays}.equals(this.$p, ##
          (that instanceof $subclass) ? (($subclass) that).$p : that.${p.getter}()) ##
    #else
      #if ($p.nullable) (this.$p == null) ? (that.${p.getter}() == null) : #end ##
          this.${p}.equals(that.${p.getter}()) ##
    #end
  #end

  @Override
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (o instanceof $origClass) {

  #if ($props.empty)

      return true;

  #else

      $origClass$wildcardTypes that = ($origClass$wildcardTypes) o;
      return ##
           #foreach ($p in $props)
           (#equalsThatExpression ($p))##
             #if ($foreach.hasNext)

           && ##
             #end
           #end
           ;
  #end

    }
    return false;
  }

#end

#if ($hashCode)
  #macro (hashCodeExpression $p)
    #if ($p.kind == "BYTE" || $p.kind == "SHORT" || $p.kind == "CHAR" || $p.kind == "INT")
      this.$p ##
    #elseif ($p.kind == "LONG")
      (this.$p >>> 32) ^ this.$p ##
    #elseif ($p.kind == "FLOAT")
      Float.floatToIntBits(this.$p) ##
    #elseif ($p.kind == "DOUBLE")
      (Double.doubleToLongBits(this.$p) >>> 32) ^ Double.doubleToLongBits(this.$p) ##
    #elseif ($p.kind == "BOOLEAN")
      this.$p ? 1231 : 1237 ##
    #elseif ($p.kind == "ARRAY")
      ${arrays}.hashCode(this.$p) ##
    #else
      #if ($p.nullable) ($p == null) ? 0 : #end this.${p}.hashCode() ##
    #end
  #end

  @Override
  public int hashCode() {
    int h = 1;

  #foreach ($p in $props)

    h *= 1000003;
    h ^= #hashCodeExpression($p);

  #end

    return h;
  }
#end

#if (!$serialVersionUID.empty)
  private static final long serialVersionUID = $serialVersionUID;
#end

#if ($builderTypeName != "")

  #foreach ($m in $toBuilderMethods)

  @Override
  ${m.access}${builderTypeName}${builderActualTypes} ${m.name}() {
    return new Builder${builderActualTypes}(this);
  }

  #end

  static final class Builder${builderFormalTypes} ##
  #if ($builderIsInterface) implements #else extends #end
      ${builderTypeName}${builderActualTypes} {

  #foreach ($p in $props)

    #if ($p.kind.primitive)

    private $types.boxedClass($p.typeMirror).simpleName $p;

    #else

      #if ($builderPropertyBuilders[$p.name])
      ## If you have ImmutableList.Builder stringsBuilder() then we define two fields:
      ## private ImmutableList.Builder stringsBuilder$;
      ## private ImmutableList strings;

    private ${builderPropertyBuilders[$p.name].builderType} ##
        ${builderPropertyBuilders[$p.name].name};

      #end

    private $p.type $p #if ($p.optional && !$p.nullable) = $p.optional.empty #end ;

    #end
  #end

    Builder() {
    }

  #if (!$toBuilderMethods.empty)

    private Builder(${origClass}${actualTypes} source) {

    #foreach ($p in $props)

      this.$p = source.${p.getter}();

    #end

    }

  #end

  #foreach ($p in $props)

    ## The following is either null or an instance of PropertyBuilderClassifier.PropertyBuilder
    #set ($propertyBuilder = $builderPropertyBuilders[$p.name])

    ## Setter and/or property builder

    #foreach ($setter in $builderSetters[$p.name])

    #if ($p.nullable)
      #set ($nullableAnnotation = $p.nullableAnnotation)
    #else
      #set ($nullableAnnotation = $setter.nullableAnnotation)
    #end

    @Override
    ${setter.access}${builderTypeName}${builderActualTypes} ##
        ${setter.name}(${nullableAnnotation}$setter.parameterType $p) {

      #if (!${setter.primitiveParameter} && ${nullableAnnotation.isEmpty()})

      if ($p == null) {
        throw new NullPointerException("Null $p.name");
      }

      #end

      #if ($propertyBuilder)

      if (${propertyBuilder.name} != null) {
        throw new IllegalStateException("Cannot set $p after calling ${p.name}Builder()");
      }

      #end

      this.$p = ${setter.copy($p)};
      return this;
    }

    #end

    #if ($propertyBuilder)

    @Override
    ${propertyBuilder.access}$propertyBuilder.builderType ${p.name}Builder() {
      if (${propertyBuilder.name} == null) {

        ## This is the first time someone has asked for the builder. If the property it sets already
        ## has a value (because it came from a toBuilder() call on the AutoValue class, or because
        ## there is also a setter for this property) then we copy that value into the builder.
        ## Otherwise the builder starts out empty.
        ## If we have neither a setter nor a toBuilder() method, then the builder always starts
        ## off empty.

        #if ($builderSetters[$p.name].empty && $toBuilderMethods.empty)

        ${propertyBuilder.name} = ${propertyBuilder.initializer};

        #else

        if (${p.presenseCheck}) {
          #if (${propertyBuilder.builtToBuilder})

          ${propertyBuilder.name} = ${p.valueRetrieval}.${propertyBuilder.builtToBuilder}();

          #else

          ${propertyBuilder.name} = ${propertyBuilder.initializer};
          ${propertyBuilder.name}.${propertyBuilder.copyAll}($p);

          #end

          $p = null;
        } else {
          ${propertyBuilder.name} = ${propertyBuilder.initializer};
        }

        #end

      }
      return $propertyBuilder.name;
    }

    #end

    ## Getter

    #if ($builderGetters[$p.name])

    @Override
    ${p.nullableAnnotation}${builderGetters[$p.name].access}$builderGetters[$p.name].type ${p.getter}() {
      #if ($builderGetters[$p.name].optional)

      if ($p == null) {
        return $builderGetters[$p.name].optional.empty;
      } else {
        return ${builderGetters[$p.name].optional.rawType}.of($p);
      }

      #else
        #if ($builderRequiredProperties.contains($p))

      if ($p == null) {
        throw new IllegalStateException("Property \"$p.name\" has not been set");
      }

        #end

        #if ($propertyBuilder)

      if (${propertyBuilder.name} != null) {
        return ${propertyBuilder.build};
      }
          #if (!${p.optional})

      if ($p == null) {
        ${propertyBuilder.beforeInitDefault}
        $p = ${propertyBuilder.initDefault};
      }

          #end
        #end

      return $p;

      #end

    }

    #end
  #end

    @Override
    ${buildMethod.get().access}${origClass}${actualTypes} ${buildMethod.get().name}() {

  #foreach ($p in $props)
    #set ($propertyBuilder = $builderPropertyBuilders[$p.name])
    #if ($propertyBuilder)

      if (${propertyBuilder.name} != null) {
        this.$p = ${propertyBuilder.build};
      } #if (!${p.optional}) else if (this.$p == null) {
        ${propertyBuilder.beforeInitDefault}
        this.$p = ${propertyBuilder.initDefault};
      } #end

    #end
  #end

  #if (!$builderRequiredProperties.empty)

      String missing = "";

    #foreach ($p in $builderRequiredProperties)

      if (this.$p == null) {
        missing += " $p.name";
      }

    #end

      if (!missing.isEmpty()) {
        throw new IllegalStateException("Missing required properties:" + missing);
      }
  #end

      return new ${finalSubclass}${actualTypes}(
  #foreach ($p in $props)

          this.$p #if ($foreach.hasNext) , #end
  #end );
    }
  }
#end

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy