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

rythm.union.txt Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
@args com.linkedin.pegasus.generator.spec.UnionTemplateSpec union
@import com.linkedin.pegasus.generator.spec.UnionTemplateSpec.Member
@import org.coursera.courier.android.JavaSyntax

@maybePackageAndImports(union)

@{ String className = JavaSyntax.escapeKeyword(union.getClassName()) }

@if (union.getTyperefClass() != null) {
  @classAnnotations(union.getTyperefClass().getSchema())
}
@@JsonAdapter(@(className).Adapter.class)
public interface @className {
  @for(Member member: union.getMembers()) {
    @{ String unionMemberName = JavaSyntax.toUnionMemberName(member.getClassTemplateSpec()) }
    public @if (union.getEnclosingClass() != null) {static} class @unionMemberName implements @className {
      @if(member.getClassTemplateSpec().getSchema() == null) {
        // TODO: custom types (@JavaSyntax.toType(member.getClassTemplateSpec()))
      } else {
        private static final String MEMBER_KEY = "@member.getClassTemplateSpec().getSchema().getUnionMemberKey()";

        @@SerializedName(MEMBER_KEY)
        public @JavaSyntax.toType(member.getClassTemplateSpec()) member;

        @@Override
        public int hashCode() {
          return Objects.hash(this.getClass(), member);
        }

        @@Override
        public boolean equals(Object obj) {
          if (!(obj instanceof @unionMemberName)) return false;
          @unionMemberName other = (@unionMemberName)obj;
          if (other == this) return true;
          return Objects.deepEquals(this.member, other.member);
        }
      }
    }
  }

  public final class $UnknownMember implements @className {
    private $UnknownMember() { }
  }

  final class Adapter extends UnionAdapterFactory<@className> {
    Adapter() {
      super(@(className).class, new UnionAdapterFactory.Resolver<@className>() {
        @@Override
        public Class resolve(String memberKey) {
          switch(memberKey) {
            @for(Member member: union.getMembers()) {
              @{String unionMemberName = JavaSyntax.toUnionMemberName(member.getClassTemplateSpec())} case @(unionMemberName).MEMBER_KEY: return @(unionMemberName).class;
            }
            default: return $UnknownMember.class;
          }
        }
      });
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy