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

org.sonar.plugins.python.api.SonarLintCache Maven / Gradle / Ivy

The newest version!
/*
 * SonarQube Python Plugin
 * Copyright (C) 2011-2024 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 Sonar Source-Available License Version 1, as published by SonarSource SA.
 *
 * 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 Sonar Source-Available License for more details.
 *
 * You should have received a copy of the Sonar Source-Available License
 * along with this program; if not, see https://sonarsource.com/license/ssal/
 */
package org.sonar.plugins.python.api;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.sonar.api.Beta;
import org.sonar.api.batch.sensor.cache.ReadCache;
import org.sonar.api.batch.sensor.cache.WriteCache;
import org.sonarsource.api.sonarlint.SonarLintSide;

/**
 * Component used in SonarLint to transfer data in memory between plugins.
 * At the time of writing, this is used only by the DBD plugin, to consume IRs produced by DBD custom rules in SonarLint context.
 */
@SonarLintSide()
@Beta
public class SonarLintCache implements ReadCache, WriteCache {

  private final Map cache = new HashMap<>();


  @Override
  public InputStream read(String s) {
    if (!contains(s)) {
      throw new IllegalArgumentException(String.format("SonarLintCache does not contain key \"%s\"", s));
    }
    return new ByteArrayInputStream(cache.get(s));
  }

  @Override
  public boolean contains(String s) {
    return cache.containsKey(s);
  }

  @Override
  public void write(String s, InputStream inputStream) {
    try {
      write(s, inputStream.readAllBytes());
    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
  }

  @Override
  public void write(String s, byte[] bytes) {
    if (contains(s)) {
      throw new IllegalArgumentException(String.format("Same key cannot be written to multiple times (%s)", s));
    }
    cache.put(s, bytes);
  }

  @Override
  public void copyFromPrevious(String s) {
    throw new UnsupportedOperationException("SonarLintCache does not allow to copy from previous.");
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy