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

auto.parcelgson.processor.autoparcel.vm Maven / Gradle / Ivy

## Template for each generated AutoParcelGson_Foo class.
## This template uses the Apache Velocity Template Language (VTL).
## The variables ($pkg, $props, and so on) are defined by the fields of MutableAutoParcelTemplateVars.
##
## 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

#foreach ($a in $classAnnotations)

${a}##
#end

${gwtCompatibleAnnotation}
final class $subclass$formalTypes extends $origClass$actualTypes {

## Fields

#foreach ($p in $props)

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

  ${a}##
  #end
  
  private final $p.type $p;
#end

## Constructor

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

      $p.type $p #if ($foreach.hasNext) , #end
#end ) {
#foreach ($p in $props)
  #if (!$p.kind.primitive && !$p.nullable)

    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}() {

  #if ($p.kind == "ARRAY")

    #if ($p.nullable)

    return $p == null ? null : ${p}.clone();

    #else

    return ${p}.clone();

    #end

  #else

    return $p;

  #end

  }

#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")
      $p ##
    #elseif ($p.kind == "LONG")
      ($p >>> 32) ^ $p ##
    #elseif ($p.kind == "FLOAT")
      Float.floatToIntBits($p) ##
    #elseif ($p.kind == "DOUBLE")
      (Double.doubleToLongBits($p) >>> 32) ^ Double.doubleToLongBits($p) ##
    #elseif ($p.kind == "BOOLEAN")
      $p ? 1231 : 1237 ##
    #elseif ($p.kind == "ARRAY")
      ${arrays}.hashCode($p) ##
    #else
      #if ($p.nullable) ($p == null) ? 0 : #end ${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 ($parcelable)
  public static final android.os.Parcelable.Creator<$subclass> CREATOR = new android.os.Parcelable.Creator<$subclass>() {
    @Override
    public $subclass createFromParcel(android.os.Parcel in) {
      return new $subclass(in);
    }

    @Override
    public ${subclass}[] newArray(int size) {
      return new ${subclass}[size];
    }
  };

  private final static java.lang.ClassLoader CL = ${subclass}.class.getClassLoader();

  private $subclass(android.os.Parcel in) {
    this(#foreach ($p in $props)
      ($p.castType) in.readValue(CL)#if ($foreach.hasNext), #end
    #end);
  }

  @Override
  public void writeToParcel(android.os.Parcel dest, int flags) {
    #foreach ($p in $props)
      dest.writeValue($p);
    #end
  }

  @Override
  public int describeContents() {
    return 0;
  }
#end

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

#if ($builderTypeName != "")

  #foreach ($m in $toBuilderMethods)

  @Override
  public ${builderTypeName}${builderActualTypes} ${m}() {
    return new Builder${builderActualTypes}(this);
  }

  #end

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

    private final $bitSet set$ = new ${bitSet}();

    #foreach ($p in $props)

    private $p.type $p;

    #end

    Builder() {
    }

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

      #foreach ($p in $props)

      $builderSetterNames[$p.name](source.${p.getter}());
      ## We use the setter methods rather than assigning the fields directly so we don't have
      ## to duplicate the logic for cloning arrays. Not cloning arrays would conceivably be
      ## a security problem.

      #end

    }

#set ($index = 0)
#foreach ($p in $props)

    @Override
    public ${builderTypeName}${builderActualTypes} $builderSetterNames[$p.name]($p.type $p) {
      #if ($p.kind == "ARRAY")
        #if ($p.nullable)

      this.$p = ($p == null) ? null : ${p}.clone();

        #else

      this.$p = ${p}.clone();

        #end
      #else

      this.$p = $p;

      #end

      #if (!$p.nullable)

      set$.set($index);
      #set ($index = $index + 1)

      #end
      return this;
    }

    #end

    @Override
    public ${origClass}${actualTypes} ${buildMethodName}() {
      if (set$.cardinality() < $index) {
        String[] propertyNames = {

         #foreach ($p in $props) #if (!$p.nullable) "$p", #end #end

        };
        StringBuilder missing = new StringBuilder();
        for (int i = 0; i < $index; i++) {
          if (!set$.get(i)) {
            missing.append(' ').append(propertyNames[i]);
          }
        }
        throw new IllegalStateException("Missing required properties:" + missing);
      }
      ${origClass}${actualTypes} result = new ${subclass}${actualTypes}(
    #foreach ($p in $props)

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

    #foreach ($v in $validators)

      result.${v}();

    #end

      return result;
    }
  }
#end
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy