001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.internal;
021
022import javax.annotation.Nullable;
023import javax.annotation.concurrent.Immutable;
024import org.sonar.api.SonarProduct;
025import org.sonar.api.SonarQubeSide;
026import org.sonar.api.SonarRuntime;
027import org.sonar.api.utils.Version;
028
029import static com.google.common.base.Preconditions.checkArgument;
030import static java.util.Objects.requireNonNull;
031
032/**
033 * @since 6.0
034 */
035@Immutable
036public class SonarRuntimeImpl implements SonarRuntime {
037
038  private final Version version;
039  private final SonarProduct product;
040  private final SonarQubeSide sonarQubeSide;
041
042  private SonarRuntimeImpl(Version version, SonarProduct product, @Nullable SonarQubeSide sonarQubeSide) {
043    requireNonNull(product);
044    checkArgument((product == SonarProduct.SONARQUBE) == (sonarQubeSide != null), "sonarQubeSide should be provided only for SonarQube product");
045    this.version = requireNonNull(version);
046    this.product = product;
047    this.sonarQubeSide = sonarQubeSide;
048  }
049
050  @Override
051  public Version getApiVersion() {
052    return version;
053  }
054
055  @Override
056  public SonarProduct getProduct() {
057    return product;
058  }
059
060  @Override
061  public SonarQubeSide getSonarQubeSide() {
062    if (sonarQubeSide == null) {
063      throw new UnsupportedOperationException("Can only be called in SonarQube");
064    }
065    return sonarQubeSide;
066  }
067
068  /**
069   * Create an instance for SonarQube runtime environment.
070   */
071  public static SonarRuntime forSonarQube(Version version, SonarQubeSide side) {
072    return new SonarRuntimeImpl(version, SonarProduct.SONARQUBE, side);
073  }
074
075  /**
076   * Create an instance for SonarLint runtime environment.
077   */
078  public static SonarRuntime forSonarLint(Version version) {
079    return new SonarRuntimeImpl(version, SonarProduct.SONARLINT, null);
080  }
081
082}