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

io.sphere.internal.CategoryCache Maven / Gradle / Ivy

There is a newer version: 0.72.1
Show newest version
package io.sphere.internal;

import com.google.common.collect.*;
import io.sphere.client.shop.model.Category;

import java.util.*;

public class CategoryCache {
    private final ImmutableList roots;
    private final ImmutableList all;
    private final ImmutableMap byIdMap;
    private final ImmutableMap categoriesByLocaleAndSlug;
    private final Locale defaultLocale;


    private CategoryCache(
            final ImmutableList roots,
            final ImmutableList all,
            final ImmutableMap categoriesById,
            final ImmutableMap categoriesByLocaleAndSlug,
            final Locale defaultLocale) {
        this.roots = roots;
        this.byIdMap = categoriesById;
        this.all = all;
        this.categoriesByLocaleAndSlug = categoriesByLocaleAndSlug;
        this.defaultLocale = defaultLocale;
    }

    /** Caches category tree in multiple different ways for fast lookup. */
    public static CategoryCache create(final Iterable roots, final Locale locale) {
        List all = getAllRecursive(roots);
        return new CategoryCache(ImmutableList.copyOf(roots), sortByName(all, locale), buildByIdMap(all), buildBySlugMap(all), locale);
    }

    public List getRoots() { return roots; }
    public Category getById(String id) { return byIdMap.get(id); }
    public Category getBySlug(String slug) {
        return getBySlug(slug, defaultLocale);
    }
    public Category getBySlug(final String slug, final Locale locale) {
        return categoriesByLocaleAndSlug.get(new LocaleSlugPair(locale, slug));
    }
    public List getAsFlatList() { return all; }

    // --------------------------------------------------
    // Helpers for create()
    // --------------------------------------------------

    private static List getAllRecursive(Iterable categories) {
        List result = new ArrayList();
        for (Category c: categories) {
            result.add(c);
            for (Category child: getAllRecursive(c.getChildren())) {
                result.add(child);
            }
        }
        return result;
    }

    private static ImmutableMap buildByIdMap(Collection categories) {
        Map map = new HashMap(categories.size());
        for (Category c: categories) {
            map.put(c.getId(), c);
        }
        return ImmutableMap.copyOf(map);
    }

    private static ImmutableMap buildBySlugMap(final Collection categories) {
        final Map map = Maps.newHashMap();
        for (final Category category : categories) {
            for (final Locale locale : category.getLocalizedSlug().getLocales()) {
                map.put(new LocaleSlugPair(locale, category.getSlug(locale)), category);
            }
        }
        return ImmutableMap.copyOf(map);
    }


    private static ImmutableList sortByName(Collection categories, Locale locale) {
        List categoriesCopy = new ArrayList(categories);
        Collections.sort(categoriesCopy, new ByNameComparator(locale));
        return ImmutableList.copyOf(categoriesCopy);
    }

    private static class LocaleSlugPair extends Pair {
        public LocaleSlugPair(final Locale x, final String y) {
            super(x, y);
        }
    }

    private static class ByNameComparator implements Comparator {
        private final Locale locale;

        private ByNameComparator(Locale locale) { this.locale = locale; }

        @Override public int compare(Category category, Category other) {
            if (category == null && other == null) return 0;
            if (category == null && other != null) return -1;
            if (category != null && other == null) return 1;
            return category.getName(locale).compareTo(other.getName(locale));
        }

        /** Indicates whether some other object is equal to this comparator. */
        @Override public boolean equals(Object other) {
            return this == other;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy