001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact 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(version);
044    requireNonNull(product);
045    checkArgument((product == SonarProduct.SONARQUBE) == (sonarQubeSide != null), "sonarQubeSide should be provided only for SonarQube product");
046    this.version = version;
047    this.product = product;
048    this.sonarQubeSide = sonarQubeSide;
049  }
050
051  @Override
052  public Version getApiVersion() {
053    return this.version;
054  }
055
056  @Override
057  public SonarProduct getProduct() {
058    return product;
059  }
060
061  @Override
062  public SonarQubeSide getSonarQubeSide() {
063    if (sonarQubeSide == null) {
064      throw new UnsupportedOperationException("Can only be called in SonarQube");
065    }
066    return sonarQubeSide;
067  }
068
069  /**
070   * Create an instance for SonarQube runtime environment.
071   */
072  public static SonarRuntime forSonarQube(Version version, SonarQubeSide side) {
073    return new SonarRuntimeImpl(version, SonarProduct.SONARQUBE, side);
074  }
075
076  /**
077   * Create an instance for SonarLint runtime environment.
078   */
079  public static SonarRuntime forSonarLint(Version version) {
080    return new SonarRuntimeImpl(version, SonarProduct.SONARLINT, null);
081  }
082
083}