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

io.sphere.sdk.products.SuggestionResultImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0-M26
Show newest version
package io.sphere.sdk.products;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import io.sphere.sdk.models.Base;

import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static java.util.Collections.emptyList;

final class SuggestionResultImpl extends Base implements SuggestionResult {
    private final Map> suggestionMap;

    @JsonCreator
    private SuggestionResultImpl(final Map> suggestionMap) {
        this.suggestionMap = suggestionMap;
    }

    @Override
    @Nonnull
    public List getSuggestionsForLocale(final Locale locale) {
        return suggestionMap.getOrDefault("searchKeywords." + locale.toLanguageTag(), emptyList());
    }

    @Override
    public Map> getSuggestionsMap() {
        final Map> suggestions = new HashMap<>();
        suggestionMap.entrySet().forEach(entry -> {
            final Locale locale = Locale.forLanguageTag(entry.getKey().replace("searchKeywords.", ""));
            suggestions.put(locale, entry.getValue());
        });
        return suggestions;
    }

    @SuppressWarnings("unused")//used by Jackson JSON mapper
    @JsonAnySetter
    private void set(final String searchKeywordWithLang, final List suggestions) {
        suggestionMap.put(searchKeywordWithLang, suggestions);
    }

    @JsonIgnore
    public static SuggestionResult of(final Map> suggestions) {
        final Map> suggestionMap = new HashMap<>();
        suggestions.entrySet().forEach(entry -> suggestionMap.put(entry.getKey().toLanguageTag(), entry.getValue()));
        return new SuggestionResultImpl(suggestionMap);
    }

    public static TypeReference typeReference() {
        return new TypeReference() {
            @Override
            public String toString() {
                return "TypeReference";
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy