All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.code.testing.Compilation Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014-2020 Objectos Software LTDA.
 *
 * 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 br.com.objectos.code.testing;

import static br.com.objectos.comuns.lang.Preconditions.checkNotNull;

import java.util.Map;

public class Compilation {

  private final boolean successful;
  private final String message;
  private final Map generatedClassFiles;
  private final Map generatedJavaFiles;
  private final Map generatedResources;

  private Compilation(Builder builder) {
    successful = builder.successful();
    message = builder.message();
    generatedClassFiles = builder.generatedClassFiles();
    generatedJavaFiles = builder.generatedJavaFiles();
    generatedResources = builder.generatedResources();
  }

  public static CompilationTaskDsl newTask() {
    return CompilationTaskDsl.withSystemJavaCompiler();
  }

  public final boolean containsClassFile(String qualifiedName) {
    return generatedClassFiles.containsKey(qualifiedName);
  }

  public final boolean containsJavaFile(String qualifiedName) {
    return generatedJavaFiles.containsKey(qualifiedName);
  }

  public final boolean containsResource(String resourceName) {
    return generatedResources.containsKey(resourceName);
  }
  
  public final GeneratedClassFile getClassFile(String qualifiedName) {
    checkNotNull(qualifiedName, "qualifiedName == null");
    checkKey(generatedClassFiles, qualifiedName);
    return generatedClassFiles.get(qualifiedName);
  }

  public final GeneratedJavaFile getJavaFile(String qualifiedName) {
    checkNotNull(qualifiedName, "qualifiedName == null");
    checkKey(generatedJavaFiles, qualifiedName);
    return generatedJavaFiles.get(qualifiedName);
  }
  
  public final GeneratedResource getResource(String resourceName) {
    checkNotNull(resourceName, "resourceName == null");
    checkKey(generatedResources, resourceName);
    return generatedResources.get(resourceName);
  }

  public final String message() {
    return message;
  }

  public final boolean wasSuccessful() {
    return successful;
  }

  private void checkKey(Map map, String key) {
    if (!map.containsKey(key)) {
      throw new AssertionError("Compilation did not generate [" + key + "]");
    }
  }

  abstract static class Builder
      implements br.com.objectos.comuns.lang.Builder {

    @Override
    public final Compilation build() {
      return new Compilation(this);
    }

    abstract boolean successful();
    abstract String message();
    abstract Map generatedClassFiles();
    abstract Map generatedJavaFiles();
    abstract Map generatedResources();

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy