/* * Copyright (C) 2014 Google, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package dagger.internal.codegen.writer; import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Set; public final class InterfaceWriter extends TypeWriter { private final List typeVariables; InterfaceWriter(ClassName name) { super(name); this.typeVariables = Lists.newArrayList(); } public void addTypeVariable(TypeVariableName typeVariable) { this.typeVariables.add(typeVariable); } @Override public Appendable write(Appendable appendable, Context context) throws IOException { context = context.createSubcontext(FluentIterable.from(nestedTypeWriters) .transform(new Function() { @Override public ClassName apply(TypeWriter input) { return input.name; } }) .toSet()); writeAnnotations(appendable, context); writeModifiers(appendable).append("interface ").append(name.simpleName()); if (!typeVariables.isEmpty()) { appendable.append('<'); Joiner.on(", ").appendTo(appendable, typeVariables); appendable.append('>'); } Iterator implementedTypesIterator = implementedTypes.iterator(); if (implementedTypesIterator.hasNext()) { appendable.append(" extends "); implementedTypesIterator.next().write(appendable, context); while (implementedTypesIterator.hasNext()) { appendable.append(", "); implementedTypesIterator.next().write(appendable, context); } } appendable.append(" {"); for (MethodWriter methodWriter : methodWriters) { appendable.append('\n'); methodWriter.write(new IndentingAppendable(appendable), context); } for (TypeWriter nestedTypeWriter : nestedTypeWriters) { appendable.append('\n'); nestedTypeWriter.write(new IndentingAppendable(appendable), context); } appendable.append("}\n"); return appendable; } @Override public Set referencedClasses() { return FluentIterable.from(ImmutableList.of()) .append(nestedTypeWriters) .append(methodWriters) .append(implementedTypes) .append(annotations) .transformAndConcat(HasClassReferences.COMBINER) .toSet(); } }