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

org.sonarsource.sonarlint.core.analysis.api.AnalysisConfiguration Maven / Gradle / Ivy

There is a newer version: 10.2.0.78029
Show newest version
/*
 * SonarLint Core - Analysis Engine
 * Copyright (C) 2016-2022 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonarsource.sonarlint.core.analysis.api;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.concurrent.Immutable;

@Immutable
public class AnalysisConfiguration {

  private final Iterable inputFiles;
  private final Map extraProperties;
  private final Path baseDir;
  private final Collection activeRules;
  private final String toString;

  private AnalysisConfiguration(Builder builder) {
    this.baseDir = builder.baseDir;
    this.inputFiles = builder.inputFiles;
    this.extraProperties = builder.extraProperties;
    this.activeRules = builder.activeRules;
    this.toString = generateToString();
  }

  public static Builder builder() {
    return new Builder();
  }

  public Map extraProperties() {
    return extraProperties;
  }

  public Path baseDir() {
    return baseDir;
  }

  public Iterable inputFiles() {
    return inputFiles;
  }

  public Collection activeRules() {
    return activeRules;
  }

  @Override
  public String toString() {
    return toString;
  }

  private String generateToString() {
    var sb = new StringBuilder();
    sb.append("[\n");
    generateToStringCommon(sb);
    sb.append("  activeRules: ").append(activeRules).append("\n");
    generateToStringInputFiles(sb);
    sb.append("]\n");
    return sb.toString();
  }

  protected void generateToStringCommon(StringBuilder sb) {
    sb.append("  baseDir: ").append(baseDir()).append("\n");
    sb.append("  extraProperties: ").append(extraProperties()).append("\n");
  }

  protected void generateToStringInputFiles(StringBuilder sb) {
    sb.append("  inputFiles: [\n");
    for (ClientInputFile inputFile : inputFiles()) {
      sb.append("    ").append(inputFile.uri());
      sb.append(" (").append(getCharsetLabel(inputFile)).append(")");
      if (inputFile.isTest()) {
        sb.append(" [test]");
      }
      var language = inputFile.language();
      if (language != null) {
        sb.append(" [" + language.getLanguageKey() + "]");
      }
      sb.append("\n");
    }
    sb.append("  ]\n");
  }

  private static String getCharsetLabel(ClientInputFile inputFile) {
    var charset = inputFile.getCharset();
    return charset != null ? charset.displayName() : "default";
  }

  public static final class Builder {
    private final List inputFiles = new ArrayList<>();
    private final Map extraProperties = new HashMap<>();
    private Path baseDir;
    private final Collection activeRules = new ArrayList<>();

    private Builder() {
    }

    public Builder addInputFiles(ClientInputFile... inputFiles) {
      Collections.addAll(this.inputFiles, inputFiles);
      return this;
    }

    public Builder addInputFiles(Collection inputFiles) {
      this.inputFiles.addAll(inputFiles);
      return this;
    }

    public Builder addInputFile(ClientInputFile inputFile) {
      this.inputFiles.add(inputFile);
      return this;
    }

    public Builder putAllExtraProperties(Map extraProperties) {
      this.extraProperties.putAll(extraProperties);
      return this;
    }

    public Builder putExtraProperty(String key, String value) {
      this.extraProperties.put(key, value);
      return this;
    }

    public Builder setBaseDir(Path baseDir) {
      this.baseDir = baseDir;
      return this;
    }

    public Builder addActiveRules(ActiveRule... activeRules) {
      Collections.addAll(this.activeRules, activeRules);
      return this;
    }

    public Builder addActiveRules(Collection activeRules) {
      this.activeRules.addAll(activeRules);
      return this;
    }

    public Builder addActiveRule(ActiveRule activeRules) {
      this.activeRules.add(activeRules);
      return this;
    }

    public AnalysisConfiguration build() {
      return new AnalysisConfiguration(this);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy