1 /* 2 * Copyright (c) 2018 Google, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.truth.extensions.proto; 18 19 import com.google.auto.value.AutoOneOf; 20 import com.google.protobuf.Descriptors.FieldDescriptor; 21 22 @AutoOneOf(SubScopeId.Kind.class) 23 abstract class SubScopeId { 24 enum Kind { 25 FIELD_DESCRIPTOR, 26 UNKNOWN_FIELD_DESCRIPTOR; 27 } 28 kind()29 abstract Kind kind(); 30 fieldDescriptor()31 abstract FieldDescriptor fieldDescriptor(); 32 unknownFieldDescriptor()33 abstract UnknownFieldDescriptor unknownFieldDescriptor(); 34 35 /** Returns a short, human-readable version of this identifier. */ shortName()36 final String shortName() { 37 switch (kind()) { 38 case FIELD_DESCRIPTOR: 39 return fieldDescriptor().isExtension() 40 ? "[" + fieldDescriptor() + "]" 41 : fieldDescriptor().getName(); 42 case UNKNOWN_FIELD_DESCRIPTOR: 43 return String.valueOf(unknownFieldDescriptor().fieldNumber()); 44 } 45 throw new AssertionError(kind()); 46 } 47 of(FieldDescriptor fieldDescriptor)48 static SubScopeId of(FieldDescriptor fieldDescriptor) { 49 return AutoOneOf_SubScopeId.fieldDescriptor(fieldDescriptor); 50 } 51 of(UnknownFieldDescriptor unknownFieldDescriptor)52 static SubScopeId of(UnknownFieldDescriptor unknownFieldDescriptor) { 53 return AutoOneOf_SubScopeId.unknownFieldDescriptor(unknownFieldDescriptor); 54 } 55 } 56