1 /* 2 * Copyright (C) 2015 The Dagger Authors. 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 dagger.producers.monitoring; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 21 import dagger.producers.Produces; 22 import java.util.Objects; 23 import org.checkerframework.checker.nullness.compatqual.NullableDecl; 24 25 /** A token that represents an individual {@linkplain Produces producer method}. */ 26 public final class ProducerToken { 27 @NullableDecl private final Class<?> classToken; 28 @NullableDecl private final String methodName; 29 ProducerToken(@ullableDecl Class<?> classToken, @NullableDecl String methodName)30 private ProducerToken(@NullableDecl Class<?> classToken, @NullableDecl String methodName) { 31 this.classToken = classToken; 32 this.methodName = methodName; 33 } 34 35 /** 36 * Creates a token for a class token that represents the generated factory for a producer method. 37 * 38 * <p><b>Do not use this!</b> This is intended to be called by generated code only, and its 39 * signature may change at any time. 40 */ create(Class<?> classToken)41 public static ProducerToken create(Class<?> classToken) { 42 return new ProducerToken(checkNotNull(classToken), null); 43 } 44 45 /** 46 * Creates a token for a producer method. 47 * 48 * <p><b>Do not use this!</b> This is intended to be called by generated code only, and its 49 * signature may change at any time. 50 */ create(String methodName)51 public static ProducerToken create(String methodName) { 52 return new ProducerToken(null, checkNotNull(methodName)); 53 } 54 55 /** Two tokens are equal if they represent the same method. */ 56 @Override equals(Object o)57 public boolean equals(Object o) { 58 if (o == this) { 59 return true; 60 } else if (o instanceof ProducerToken) { 61 ProducerToken that = (ProducerToken) o; 62 return Objects.equals(this.classToken, that.classToken) 63 && Objects.equals(this.methodName, that.methodName); 64 } else { 65 return false; 66 } 67 } 68 69 /** Returns an appropriate hash code to match {@link #equals(Object)}. */ 70 @Override hashCode()71 public int hashCode() { 72 int h = 1; 73 h *= 1000003; 74 h ^= Objects.hashCode(this.classToken); 75 h *= 1000003; 76 h ^= Objects.hashCode(this.methodName); 77 return h; 78 } 79 80 /** Returns a representation of the method. */ 81 @Override toString()82 public String toString() { 83 if (methodName != null) { 84 return methodName; 85 } else if (classToken != null) { 86 return classToken.getCanonicalName(); 87 } else { 88 throw new IllegalStateException(); 89 } 90 } 91 } 92