FieldDeclaration.java

  1. package net.morimekta.providence.reflect.model;

  2. import net.morimekta.providence.descriptor.PRequirement;
  3. import net.morimekta.providence.reflect.parser.ThriftToken;

  4. import javax.annotation.Nonnull;
  5. import javax.annotation.Nullable;
  6. import java.util.List;

  7. /**
  8.  * Represents the declaration of a single field param or thrown exception.
  9.  *
  10.  * <pre>{@code
  11.  * field ::= ({id} ':')? {optionality}? {type} {name} ('=' {defaultValue}) {annotations}?
  12.  * }</pre>
  13.  */
  14. public class FieldDeclaration extends Declaration {
  15.     private final ThriftToken       id;
  16.     private final int               fieldId;
  17.     private final ThriftToken       requirement;
  18.     private final List<ThriftToken> type;
  19.     private final List<ThriftToken> defaultValue;

  20.     public FieldDeclaration(@Nullable String documentation,
  21.                             @Nullable ThriftToken id,
  22.                             int fieldId,
  23.                             @Nullable ThriftToken requirement,
  24.                             @Nonnull ThriftToken name,
  25.                             @Nonnull List<ThriftToken> type,
  26.                             @Nullable List<ThriftToken> defaultValue,
  27.                             @Nullable List<AnnotationDeclaration> annotations) {
  28.         super(documentation, name, annotations);
  29.         this.id = id;
  30.         this.fieldId = fieldId;
  31.         this.requirement = requirement;
  32.         this.type = type;
  33.         this.defaultValue = defaultValue;
  34.     }

  35.     public int getId() {
  36.         return fieldId;
  37.     }

  38.     public ThriftToken getIdToken() {
  39.         return id;
  40.     }

  41.     @Nonnull
  42.     public PRequirement getRequirement() {
  43.         if (requirement != null) {
  44.             switch (requirement.toString()) {
  45.                 case "required": return PRequirement.REQUIRED;
  46.                 case "optional": return PRequirement.OPTIONAL;
  47.             }
  48.         }
  49.         return PRequirement.DEFAULT;
  50.     }

  51.     @Nullable
  52.     public ThriftToken getRequirementToken() {
  53.         return requirement;
  54.     }

  55.     public String getDefaultValue() {
  56.         if (defaultValue != null) {
  57.             return DeclarationUtil.toValueString(defaultValue);
  58.         }
  59.         return null;
  60.     }

  61.     @Nullable
  62.     public List<ThriftToken> getDefaultValueTokens() {
  63.         return defaultValue;
  64.     }

  65.     @Nonnull
  66.     public String getType() {
  67.         return DeclarationUtil.toTypeString(type);
  68.     }

  69.     @Nonnull
  70.     public ThriftToken getTypeToken() {
  71.         return DeclarationUtil.toTypeToken(type);
  72.     }

  73.     @Nonnull
  74.     public List<ThriftToken> getTypeTokens() {
  75.         return type;
  76.     }
  77. }