PPrimitive.java

  1. /*
  2.  * Copyright 2015 Providence Authors
  3.  *
  4.  * Licensed to the Apache Software Foundation (ASF) under one
  5.  * or more contributor license agreements. See the NOTICE file
  6.  * distributed with this work for additional information
  7.  * regarding copyright ownership. The ASF licenses this file
  8.  * to you under the Apache License, Version 2.0 (the
  9.  * "License"); you may not use this file except in compliance
  10.  * with the License. You may obtain a copy of the License at
  11.  *
  12.  *   http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing,
  15.  * software distributed under the License is distributed on an
  16.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17.  * KIND, either express or implied. See the License for the
  18.  * specific language governing permissions and limitations
  19.  * under the License.
  20.  */
  21. package net.morimekta.providence.descriptor;

  22. import net.morimekta.providence.PType;
  23. import net.morimekta.util.Binary;

  24. import javax.annotation.Nonnull;
  25. import javax.annotation.concurrent.Immutable;
  26. import java.lang.reflect.Type;

  27. /**
  28.  * Descriptors for primitive types.
  29.  * <p>
  30.  * These are the basic types used in the thrift structure except containers.
  31.  */
  32. public enum PPrimitive implements PDescriptor {
  33.     VOID  (PType.VOID,   Void.TYPE,     null, true),
  34.     BOOL  (PType.BOOL,   Boolean.TYPE,  false, true),
  35.     BYTE  (PType.BYTE,   Byte.TYPE,      (byte) 0, true),
  36.     I16   (PType.I16,    Short.TYPE,     (short) 0, true),
  37.     I32   (PType.I32,    Integer.TYPE,   0, true),
  38.     I64   (PType.I64,    Long.TYPE,      (long) 0, true),
  39.     DOUBLE(PType.DOUBLE, Double.TYPE,    0.0, true),
  40.     STRING(PType.STRING, String.class,   "", false),
  41.     BINARY(PType.BINARY, Binary.class,   Binary.empty(), false),
  42.     ;

  43.     private final PPrimitiveProvider provider;
  44.     private final PType              type;
  45.     private final Object             defaultValue;
  46.     private final boolean            nativePrimitive;
  47.     private final Type               nativeType;

  48.     PPrimitive(PType type, Type nativeType, Object defValue, boolean nativePrimitive) {
  49.         this.type = type;
  50.         this.nativeType = nativeType;
  51.         this.provider = new PPrimitiveProvider(this);
  52.         this.defaultValue = defValue;
  53.         this.nativePrimitive = nativePrimitive;
  54.     }

  55.     /**
  56.      * Get the descriptor provider for the primitive.
  57.      *
  58.      * @return The descriptor provider.
  59.      */
  60.     @Nonnull
  61.     public PPrimitiveProvider provider() {
  62.         return provider;
  63.     }

  64.     @Override
  65.     public String getProgramName() {
  66.         return null;
  67.     }

  68.     @Nonnull
  69.     @Override
  70.     public String getName() {
  71.         return type.toString();
  72.     }

  73.     @Nonnull
  74.     @Override
  75.     public String getQualifiedName(String programContext) {
  76.         return type.toString();
  77.     }

  78.     @Override
  79.     public String toString() {
  80.         return type.toString();
  81.     }

  82.     @Nonnull
  83.     @Override
  84.     public PType getType() {
  85.         return type;
  86.     }

  87.     @Override
  88.     public Object getDefaultValue() {
  89.         return defaultValue;
  90.     }

  91.     /**
  92.      * @return Returns false if the primitive type allows null values.
  93.      *         If this method returns true, getter and setter methods
  94.      *         will have parameters and return type with the native
  95.      *         primitive type, not it's boxed variant.
  96.      */
  97.     public boolean isNativePrimitive() {
  98.         return nativePrimitive;
  99.     }

  100.     /**
  101.      * Find primitive by name.
  102.      * @param name The name of the primitive.
  103.      * @return The primitive descriptor.
  104.      */
  105.     public static PPrimitive findByName(@Nonnull String name) {
  106.         switch (name) {
  107.             case "void":
  108.                 return VOID;
  109.             case "bool":
  110.                 return BOOL;
  111.             case "byte":
  112.             case "i8":
  113.                 return BYTE;
  114.             case "i16":
  115.                 return I16;
  116.             case "i32":
  117.                 return I32;
  118.             case "i64":
  119.                 return I64;
  120.             case "double":
  121.                 return DOUBLE;
  122.             case "string":
  123.                 return STRING;
  124.             case "binary":
  125.                 return BINARY;
  126.         }
  127.         return null;
  128.     }

  129.     /**
  130.      * Get the java native type for the given primitive.
  131.      * @return The java type.
  132.      */
  133.     public Type getNativeType() {
  134.         return nativeType;
  135.     }
  136. }