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.base.Function; 19 import com.google.common.base.Joiner; 20 import com.google.common.collect.FluentIterable; 21 import com.google.common.collect.ImmutableList; 22 import com.google.common.collect.Lists; 23 import java.io.IOException; 24 import java.util.Iterator; 25 import java.util.List; 26 import java.util.Set; 27 28 public final class InterfaceWriter extends TypeWriter { 29 private final List<TypeVariableName> typeVariables; InterfaceWriter(ClassName name)30 InterfaceWriter(ClassName name) { 31 super(name); 32 this.typeVariables = Lists.newArrayList(); 33 } 34 addTypeVariable(TypeVariableName typeVariable)35 public void addTypeVariable(TypeVariableName typeVariable) { 36 this.typeVariables.add(typeVariable); 37 } 38 39 @Override write(Appendable appendable, Context context)40 public Appendable write(Appendable appendable, Context context) throws IOException { 41 context = context.createSubcontext(FluentIterable.from(nestedTypeWriters) 42 .transform(new Function<TypeWriter, ClassName>() { 43 @Override public ClassName apply(TypeWriter input) { 44 return input.name; 45 } 46 }) 47 .toSet()); 48 writeAnnotations(appendable, context); 49 writeModifiers(appendable).append("interface ").append(name.simpleName()); 50 if (!typeVariables.isEmpty()) { 51 appendable.append('<'); 52 Joiner.on(", ").appendTo(appendable, typeVariables); 53 appendable.append('>'); 54 } 55 Iterator<TypeName> implementedTypesIterator = implementedTypes.iterator(); 56 if (implementedTypesIterator.hasNext()) { 57 appendable.append(" extends "); 58 implementedTypesIterator.next().write(appendable, context); 59 while (implementedTypesIterator.hasNext()) { 60 appendable.append(", "); 61 implementedTypesIterator.next().write(appendable, context); 62 } 63 } 64 appendable.append(" {"); 65 for (MethodWriter methodWriter : methodWriters) { 66 appendable.append('\n'); 67 methodWriter.write(new IndentingAppendable(appendable), context); 68 } 69 for (TypeWriter nestedTypeWriter : nestedTypeWriters) { 70 appendable.append('\n'); 71 nestedTypeWriter.write(new IndentingAppendable(appendable), context); 72 } 73 appendable.append("}\n"); 74 return appendable; 75 } 76 77 @Override referencedClasses()78 public Set<ClassName> referencedClasses() { 79 return FluentIterable.from(ImmutableList.<HasClassReferences>of()) 80 .append(nestedTypeWriters) 81 .append(methodWriters) 82 .append(implementedTypes) 83 .append(annotations) 84 .transformAndConcat(HasClassReferences.COMBINER) 85 .toSet(); 86 } 87 } 88