001/**
002 * Copyright 2013 John Ericksen
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.androidtransfuse.processor;
017
018import com.google.common.collect.ImmutableSet;
019import org.androidtransfuse.TransfuseAnalysisException;
020import org.androidtransfuse.adapter.ASTType;
021import org.androidtransfuse.transaction.TransactionProcessorBuilder;
022
023import javax.inject.Inject;
024import javax.inject.Provider;
025import javax.inject.Singleton;
026import java.lang.annotation.Annotation;
027import java.util.Collection;
028
029/**
030 * @author John Ericksen
031 */
032@Singleton
033public class TransfuseProcessor {
034
035    private final GeneratorRepository generatorRepository;
036
037    @Inject
038    public TransfuseProcessor(GeneratorRepository generatorRepository) {
039        this.generatorRepository = generatorRepository;
040    }
041
042    public void submit(Class<? extends Annotation> componentAnnotation, Collection<Provider<ASTType>> astProviders) {
043        for (Provider<ASTType> astProvider : astProviders) {
044           submit(componentAnnotation, astProvider);
045        }
046    }
047
048    public void submit(Class<? extends Annotation> componentAnnotation, Provider<ASTType> astProvider) {
049        TransactionProcessorBuilder<Provider<ASTType>, ?> builder = generatorRepository.getComponentBuilder(componentAnnotation);
050        if (builder == null) {
051            throw new TransfuseAnalysisException("Builder for type " + componentAnnotation.getName() + " not found");
052        }
053
054        builder.submit(astProvider);
055    }
056
057    public void execute() {
058        generatorRepository.getProcessor().execute();
059    }
060
061    public void checkForErrors() {
062        boolean errored = false;
063        ImmutableSet.Builder<Exception> exceptions = ImmutableSet.builder();
064
065        for (TransactionProcessorBuilder transactionProcessor : generatorRepository.getComponentBuilders().values()) {
066            if (!transactionProcessor.getTransactionProcessor().isComplete()) {
067                errored = true;
068                exceptions.addAll(transactionProcessor.getTransactionProcessor().getErrors());
069            }
070        }
071
072        if (errored) {
073            throw new TransfuseAnalysisException("Code generation did not complete successfully.", exceptions.build());
074        }
075    }
076}