<commit_before>/**
 *
 */
package org.synyx.minos.core.web.menu;

import org.springframework.beans.factory.InitializingBean;

import org.synyx.minos.util.Assert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * {@link MenuProvider} implementation that uses {@link MenuItem}s provided by {@link MenuItemProvider}s defined in the
 * context, filteres them using {@link MenuItemFilter}s defined in the context and assembles the {@link MenuItems} using
 * a {@link MenuAssembler}-Implementation.
 *
 * @author <NAME> - <EMAIL>
 * @author <NAME>
 */
public class MenuManager implements MenuProvider, InitializingBean {

    private static final MenuAssembler DEFAULT_MENUASSEMBLER = new SimpleMenuAssembler();

    private final List<MenuItemProvider> menuItemProviders;
    private final List<MenuItemFilter> menuItemFilters;
    private MenuAssembler menuAssembler = DEFAULT_MENUASSEMBLER;

    private List<MenuItem> menuItems = new ArrayList<MenuItem>();

    /**
     * Creates a new {@link MenuManager}.
     *
     * @param menuItemProviders
     * @param menuItemFilters
     */
    public MenuManager(List<MenuItemProvider> menuItemProviders, List<MenuItemFilter> menuItemFilters) {

        Assert.notNull(menuItemProviders);
        Assert.notNull(menuItemFilters);

        this.menuItemProviders = menuItemProviders;
        this.menuItemFilters = menuItemFilters;
    }

    /**
     * Configures a {@link MenuAssembler}. Defaults to {@value #DEFAULT_MENUASSEMBLER}.
     *
     * @param menuAssembler the menuAssembler to set
     */
    public void setMenuAssembler(MenuAssembler menuAssembler) {

        this.menuAssembler = menuAssembler == null ? DEFAULT_MENUASSEMBLER : menuAssembler;
    }


    /*
     * (non-Javadoc)
     *
     * @see org.synyx.minos.core.web.menu.MenuProvider#getMenu(java.lang.String)
     */
    @Override
    public MenuItems getMenu(String id) {

        MenuItems items = buildMenu(menuItems);
        MenuItems menuTree = items.filter(menuItemFilters);

        Map<String, MenuItems> menues = buildMenues(menuTree);

        MenuItems menu = menues.get(id);

        if (menu == null) {
            menu = MenuItems.EMPTY;
        }

        return menu;
    }


    /**
     * Converts the flat hierarchy of {@link MenuItems} into a Tree of {@link MenuItems} by resolving the parent-child
     * relation.
     *
     * @param menuItems
     * @return
     */
    private MenuItems buildMenu(List<MenuItem> menuItems) {

        Map<String, List<Menu>> childMap = new HashMap<String, List<Menu>>();

        for (MenuItem item : menuItems) {
            List<Menu> children = childMap.get(item.getPath());

            if (children == null) {
                children = new ArrayList<Menu>();
            } else {
                Collections.sort(children);
            }

            Menu menu = Menu.create(item, new MenuItems(children));

            List<Menu> items = childMap.get(item.getParentPath());

            if (items == null) {
                items = new ArrayList<Menu>();
                childMap.put(item.getParentPath(), items);
            }

            items.add(menu);
        }

        List<Menu> root = childMap.get("");

        if (root == null) {
            root = new ArrayList<Menu>();
        } else {
            Collections.sort(root);
        }

        return new MenuItems(root);
    }


    /**
     * Build the actual {@link MenuItems}es from the given {@link MenuItems}. Default implmentation will delegate this
     * to configured {@link MenuAssembler}.
     * <p>
     * TODO: should we wrap the Map into a Menues class to avoid null checks for non existent id lookups?
     * </p>
     *
     * @param items
     * @return
     */
    protected Map<String, MenuItems> buildMenues(MenuItems items) {

        return menuAssembler.assembleMenues(items);
    }


    @Override
    public void afterPropertiesSet() throws Exception {

        for (MenuItemProvider provider : menuItemProviders) {
            menuItems.addAll(provider.getMenuItems());
        }

        Collections.sort(menuItems, new Comparator<MenuItem>() {

                @Override
                public int compare(MenuItem o1, MenuItem o2) {

                    int value = o2.getPath().length() - o1.getPath().length();

                    if (value == 0) {
                        return o1.compareTo(o2);
                    }

                    return value;
                }
            });

        // Collections.sort(menuItems);
    }
}
<commit_msg>Refactored anonymous class in MenuManager into a named static inner class MenuItemComparator (SIC_INNER_SHOULD_BE_STATIC_ANON), refs #460<commit_after>package org.synyx.minos.core.web.menu;

import org.springframework.beans.factory.InitializingBean;

import org.synyx.minos.util.Assert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * {@link MenuProvider} implementation that uses {@link MenuItem}s provided by {@link MenuItemProvider}s defined in the
 * context, filters them using {@link MenuItemFilter}s defined in the context and assembles the {@link MenuItems} using
 * a {@link MenuAssembler}-Implementation.
 *
 * @author <NAME> - <EMAIL>
 * @author <NAME>
 */
public class MenuManager implements MenuProvider, InitializingBean {

    private static final MenuAssembler DEFAULT_MENUASSEMBLER = new SimpleMenuAssembler();

    private final List<MenuItemProvider> menuItemProviders;
    private final List<MenuItemFilter> menuItemFilters;
    private MenuAssembler menuAssembler = DEFAULT_MENUASSEMBLER;

    private List<MenuItem> menuItems = new ArrayList<MenuItem>();

    /**
     * Creates a new {@link MenuManager}.
     *
     * @param menuItemProviders
     * @param menuItemFilters
     */
    public MenuManager(List<MenuItemProvider> menuItemProviders, List<MenuItemFilter> menuItemFilters) {

        Assert.notNull(menuItemProviders);
        Assert.notNull(menuItemFilters);

        this.menuItemProviders = menuItemProviders;
        this.menuItemFilters = menuItemFilters;
    }

    /**
     * Configures a {@link MenuAssembler}. Defaults to {@value #DEFAULT_MENUASSEMBLER}.
     *
     * @param menuAssembler the menuAssembler to set
     */
    public void setMenuAssembler(MenuAssembler menuAssembler) {

        this.menuAssembler = menuAssembler == null ? DEFAULT_MENUASSEMBLER : menuAssembler;
    }


    /*
     * (non-Javadoc)
     *
     * @see org.synyx.minos.core.web.menu.MenuProvider#getMenu(java.lang.String)
     */
    @Override
    public MenuItems getMenu(String id) {

        MenuItems items = buildMenu(menuItems);
        MenuItems menuTree = items.filter(menuItemFilters);

        Map<String, MenuItems> menues = buildMenues(menuTree);

        MenuItems menu = menues.get(id);

        if (menu == null) {
            menu = MenuItems.EMPTY;
        }

        return menu;
    }


    /**
     * Converts the flat hierarchy of {@link MenuItems} into a Tree of {@link MenuItems} by resolving the parent-child
     * relation.
     *
     * @param menuItems
     * @return
     */
    private MenuItems buildMenu(List<MenuItem> menuItems) {

        Map<String, List<Menu>> childMap = new HashMap<String, List<Menu>>();

        for (MenuItem item : menuItems) {
            List<Menu> children = childMap.get(item.getPath());

            if (children == null) {
                children = new ArrayList<Menu>();
            } else {
                Collections.sort(children);
            }

            Menu menu = Menu.create(item, new MenuItems(children));

            List<Menu> items = childMap.get(item.getParentPath());

            if (items == null) {
                items = new ArrayList<Menu>();
                childMap.put(item.getParentPath(), items);
            }

            items.add(menu);
        }

        List<Menu> root = childMap.get("");

        if (root == null) {
            root = new ArrayList<Menu>();
        } else {
            Collections.sort(root);
        }

        return new MenuItems(root);
    }


    /**
     * Build the actual {@link MenuItems}es from the given {@link MenuItems}. Default implmentation will delegate this
     * to configured {@link MenuAssembler}.
     * <p>
     * TODO: should we wrap the Map into a Menues class to avoid null checks for non existent id lookups?
     * </p>
     *
     * @param items
     * @return
     */
    protected Map<String, MenuItems> buildMenues(MenuItems items) {

        return menuAssembler.assembleMenues(items);
    }


    @Override
    public void afterPropertiesSet() throws Exception {

        for (MenuItemProvider provider : menuItemProviders) {
            menuItems.addAll(provider.getMenuItems());
        }

        Collections.sort(menuItems, new MenuItemComparator());
    }

    private static class MenuItemComparator implements Comparator<MenuItem> {
        @Override
        public int compare(MenuItem o1, MenuItem o2) {
            int value = o2.getPath().length() - o1.getPath().length();

            if (value == 0) {
                return o1.compareTo(o2);
            }

            return value;
        }
    }
}

{'fraction_non_alphanumeric': 0.10316104228961982, 'fraction_numerical': 0.0018154634771465185, 'mean_word_length': 2.4442809856564915, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6080655', 'n_tokens_mistral': 2702, 'n_tokens_neox': 2595, 'n_words': 654}