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

com.commercetools.sunrise.ctp.project.ProjectContext Maven / Gradle / Ivy

The newest version!
package com.commercetools.sunrise.ctp.project;

import com.commercetools.sunrise.framework.localization.NoCountryFoundException;
import com.commercetools.sunrise.framework.localization.NoCurrencyFoundException;
import com.commercetools.sunrise.framework.localization.NoLocaleFoundException;
import com.google.inject.ImplementedBy;
import com.neovisionaries.i18n.CountryCode;
import io.sphere.sdk.projects.Project;
import play.Configuration;

import javax.money.CurrencyUnit;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

/**
 * A container for all information related to the project, such as supported countries, languages or currencies.
 */
@ImplementedBy(ProjectContextImpl.class)
public interface ProjectContext {

    /**
     * Locales associated to the project.
     * @return the list of locales
     */
    List locales();

    /**
     * Countries associated to the project.
     * @return the list of country codes
     */
    List countries();

    /**
     * Currencies associated to the project.
     * @return the list of currency units
     */
    List currencies();

    default Locale defaultLocale() {
        return locales().stream()
                .filter(Objects::nonNull)
                .findFirst()
                .orElseThrow(() -> new NoLocaleFoundException("Project does not have any valid locale associated"));
    }

    default CountryCode defaultCountry() {
        return countries().stream()
                .filter(Objects::nonNull)
                .findFirst()
                .orElseThrow(() -> new NoCountryFoundException("Project does not have any valid country code associated"));
    }

    default CurrencyUnit defaultCurrency() {
        return currencies().stream()
                .filter(Objects::nonNull)
                .findFirst()
                .orElseThrow(() -> new NoCurrencyFoundException("Project does not have any valid currency unit associated"));
    }

    default boolean isLocaleSupported(final Locale locale) {
        return locales().contains(locale);
    }

    default boolean isCountrySupported(final CountryCode countryCode) {
        return countries().contains(countryCode);
    }

    default boolean isCurrencySupported(final CurrencyUnit currency) {
        return currencies().contains(currency);
    }

    static ProjectContext of(final Configuration globalConfig, final String configPath, final Project project) {
        return new ProjectContextImpl(globalConfig, configPath, project);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy