1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf; 9 10 /** Enum that identifies the Java types required to store protobuf fields. */ 11 @ExperimentalApi 12 public enum JavaType { 13 VOID(Void.class, Void.class, null), 14 INT(int.class, Integer.class, 0), 15 LONG(long.class, Long.class, 0L), 16 FLOAT(float.class, Float.class, 0F), 17 DOUBLE(double.class, Double.class, 0D), 18 BOOLEAN(boolean.class, Boolean.class, false), 19 STRING(String.class, String.class, ""), 20 BYTE_STRING(ByteString.class, ByteString.class, ByteString.EMPTY), 21 ENUM(int.class, Integer.class, null), 22 MESSAGE(Object.class, Object.class, null); 23 24 private final Class<?> type; 25 private final Class<?> boxedType; 26 private final Object defaultDefault; 27 JavaType(Class<?> type, Class<?> boxedType, Object defaultDefault)28 JavaType(Class<?> type, Class<?> boxedType, Object defaultDefault) { 29 this.type = type; 30 this.boxedType = boxedType; 31 this.defaultDefault = defaultDefault; 32 } 33 34 /** The default default value for fields of this type, if it's a primitive type. */ getDefaultDefault()35 public Object getDefaultDefault() { 36 return defaultDefault; 37 } 38 39 /** Gets the required type for a field that would hold a value of this type. */ getType()40 public Class<?> getType() { 41 return type; 42 } 43 44 /** @return the boxedType */ getBoxedType()45 public Class<?> getBoxedType() { 46 return boxedType; 47 } 48 49 /** Indicates whether or not this {@link JavaType} can be applied to a field of the given type. */ isValidType(Class<?> t)50 public boolean isValidType(Class<?> t) { 51 return type.isAssignableFrom(t); 52 } 53 } 54