1 /* 2 * Copyright (C) 2014 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 package dagger.internal.codegen.writer; 17 18 import com.google.common.collect.FluentIterable; 19 import com.google.common.collect.ImmutableList; 20 import java.io.IOException; 21 import java.util.Set; 22 23 import static com.google.common.base.Preconditions.checkNotNull; 24 25 public class VariableWriter extends Modifiable implements Writable, HasClassReferences { 26 private final TypeName type; 27 private final String name; 28 29 VariableWriter(TypeName type, String name) { 30 this.type = checkNotNull(type); 31 this.name = checkNotNull(name); 32 } 33 34 public TypeName type() { 35 return type; 36 } 37 38 public String name() { 39 return name; 40 } 41 42 @Override 43 public Appendable write(Appendable appendable, Context context) throws IOException { 44 writeAnnotations(appendable, context); 45 writeModifiers(appendable); 46 type.write(appendable, context); 47 return appendable.append(' ').append(name); 48 } 49 50 @Override 51 public Set<ClassName> referencedClasses() { 52 return FluentIterable.from(ImmutableList.<HasClassReferences>of()) 53 .append(annotations) 54 .append(type) 55 .transformAndConcat(HasClassReferences.COMBINER) 56 .toSet(); 57 } 58 } 59