/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server;

import com.google.common.annotations.VisibleForTesting;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

import java.util.Optional;
import java.util.function.Supplier;

import static com.google.common.base.Strings.emptyToNull;
import static io.spine.server.DeploymentType.APPENGINE_CLOUD;
import static io.spine.server.DeploymentType.APPENGINE_EMULATOR;
import static io.spine.server.DeploymentType.STANDALONE;
import static java.util.Optional.ofNullable;

/**
 * The Default implementation of {@linkplain io.spine.server.ServerEnvironment.Provider
 * server environment provider}.
 */
class SystemEnvironmentSupplier implements Supplier<DeploymentType> {

    @VisibleForTesting
    static final String APP_ENGINE_ENVIRONMENT_PATH =
            "com.google.appengine.runtime.environment";
    @VisibleForTesting
    static final String APP_ENGINE_ENVIRONMENT_PRODUCTION_VALUE = "Production";
    @VisibleForTesting
    static final String APP_ENGINE_ENVIRONMENT_DEVELOPMENT_VALUE = "Development";

    private @MonotonicNonNull DeploymentType deploymentType = null;

    /** Prevent instantiation from outside. */
    private SystemEnvironmentSupplier() {
    }

    public static Supplier<DeploymentType> newInstance() {
        return new SystemEnvironmentSupplier();
    }

    @Override
    public DeploymentType get() {
        if (deploymentType == null) {
            deploymentType = readDeploymentType();
        }
        return deploymentType;
    }

    private static DeploymentType readDeploymentType() {
        Optional<String> gaeEnvironment = getProperty(APP_ENGINE_ENVIRONMENT_PATH);
        if (gaeEnvironment.isPresent()) {
            if (APP_ENGINE_ENVIRONMENT_DEVELOPMENT_VALUE.equals(gaeEnvironment.get())) {
                return APPENGINE_EMULATOR;
            }
            if (APP_ENGINE_ENVIRONMENT_PRODUCTION_VALUE.equals(gaeEnvironment.get())) {
                return APPENGINE_CLOUD;
            }
        }
        return STANDALONE;
    }

    @SuppressWarnings("AccessOfSystemProperties") /*  Based on system property. */
    private static Optional<String> getProperty(String path) {
        return ofNullable(emptyToNull(System.getProperty(path)));
    }
}

@@ -1091,76 +1091,8 @@
 ing;
-%0Aimport org.checkerframework.checker.nullness.qual.MonotonicNonNull;
 %0A%0Aim
@@ -1494,79 +1494,71 @@
 ain 
-io.spine.server.ServerEnvironment.Provider%0A * server environment provid
+DeploymentType deployment type%7D %0A * %7B@linkplain Supplier suppli
 er%7D.
@@ -1709,28 +1709,16 @@
 T_PATH =
-%0A           
  %22com.go
@@ -1967,76 +1967,8 @@
 %22;%0A%0A
-    private @MonotonicNonNull DeploymentType deploymentType = null;%0A
 %0A   
@@ -2226,202 +2226,8 @@
 ) %7B%0A
-        if (deploymentType == null) %7B%0A            deploymentType = readDeploymentType();%0A        %7D%0A        return deploymentType;%0A    %7D%0A%0A    private static DeploymentType readDeploymentType() %7B%0A
     

Remove SystemEnvironmentSupplier#readDeploymentType().