1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.google.api.generator.engine.ast; 16 17 import com.google.auto.value.AutoValue; 18 import com.google.common.base.Preconditions; 19 20 @AutoValue 21 public abstract class ThisObjectValue implements ObjectValue { 22 private static final String THIS_VALUE = "this"; 23 24 @Override type()25 public abstract TypeNode type(); 26 27 @Override value()28 public String value() { 29 return THIS_VALUE; 30 } 31 withType(TypeNode type)32 public static ThisObjectValue withType(TypeNode type) { 33 return builder().setType(type).build(); 34 } 35 builder()36 private static Builder builder() { 37 return new AutoValue_ThisObjectValue.Builder(); 38 } 39 40 @AutoValue.Builder 41 abstract static class Builder { setType(TypeNode type)42 abstract Builder setType(TypeNode type); 43 autoBuild()44 abstract ThisObjectValue autoBuild(); 45 build()46 private ThisObjectValue build() { 47 ThisObjectValue thisObjectValue = autoBuild(); 48 Preconditions.checkState( 49 TypeNode.isReferenceType(thisObjectValue.type()), 50 "The \"this\" object can only refer to object types"); 51 return thisObjectValue; 52 } 53 } 54 } 55