<commit_before>/*
 * Copyright 2017
 * Ubiquitous Knowledge Processing (UKP) Lab and FG Language Technology
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.tudarmstadt.ukp.clarin.webanno.ui.annotation.sidebar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.ClassUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.stereotype.Component;

@Component
public class AnnotationSidebarRegistryImpl
    implements AnnotationSidebarRegistry
{
    private final Logger log = LoggerFactory.getLogger(getClass());

    private final List<AnnotationSidebarFactory> extensionsProxy;

    private List<AnnotationSidebarFactory> extensions;

    public AnnotationSidebarRegistryImpl(
            @Lazy @Autowired(required = false) List<AnnotationSidebarFactory> aExtensions)
    {
        extensionsProxy = aExtensions;
    }
    
    @EventListener
    public void onContextRefreshedEvent(ContextRefreshedEvent aEvent)
    {
        init();
    }
    
    /* package private */ void init()
    {
        List<AnnotationSidebarFactory> exts = new ArrayList<>();

        if (extensionsProxy != null) {
            exts.addAll(extensionsProxy);
            AnnotationAwareOrderComparator.sort(exts);
        
            for (AnnotationSidebarFactory fs : exts) {
                log.info("Found annotation sidebar extension: {}",
                        ClassUtils.getAbbreviatedName(fs.getClass(), 20));
            }
        }
        
        extensions = Collections.unmodifiableList(exts);
    }
    
    @Override
    public List<AnnotationSidebarFactory> getSidebarFactories()
    {
        return extensions;
    }
    
    @Override
    public AnnotationSidebarFactory getSidebarFactory(String aId)
    {
        if (aId == null) {
            return null;
        }
        else {
            return extensions.stream().filter(f -> aId.equals(f.getBeanName())).findFirst()
                    .orElse(null);
        }
    }
    
    @Override
    public AnnotationSidebarFactory getDefaultSidebarFactory()
    {
        return getSidebarFactories().get(0);
    }
}<commit_msg>#881 - Use stable order in left side bar (via PR #882)<commit_after>/*
 * Copyright 2017
 * Ubiquitous Knowledge Processing (UKP) Lab and FG Language Technology
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.tudarmstadt.ukp.clarin.webanno.ui.annotation.sidebar;

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

import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.builder.CompareToBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.stereotype.Component;

@Component
public class AnnotationSidebarRegistryImpl
    implements AnnotationSidebarRegistry
{
    private final Logger log = LoggerFactory.getLogger(getClass());

    private final List<AnnotationSidebarFactory> extensionsProxy;

    private List<AnnotationSidebarFactory> extensions;

    public AnnotationSidebarRegistryImpl(
            @Lazy @Autowired(required = false) List<AnnotationSidebarFactory> aExtensions)
    {
        extensionsProxy = aExtensions;
    }

    @EventListener
    public void onContextRefreshedEvent(ContextRefreshedEvent aEvent)
    {
        init();
    }
    
    /* package private */ void init()
    {
        List<AnnotationSidebarFactory> exts = new ArrayList<>();

        if (extensionsProxy != null) {
            exts.addAll(extensionsProxy);
            exts.sort(buildComparator());

            for (AnnotationSidebarFactory fs : exts) {
                log.info("Found annotation sidebar extension: {}",
                        ClassUtils.getAbbreviatedName(fs.getClass(), 20));
            }
        }
        
        extensions = Collections.unmodifiableList(exts);
    }
    
    @Override
    public List<AnnotationSidebarFactory> getSidebarFactories()
    {
        return extensions;
    }
    
    @Override
    public AnnotationSidebarFactory getSidebarFactory(String aId)
    {
        if (aId == null) {
            return null;
        }
        else {
            return extensions.stream().filter(f -> aId.equals(f.getBeanName())).findFirst()
                    .orElse(null);
        }
    }
    
    @Override
    public AnnotationSidebarFactory getDefaultSidebarFactory()
    {
        return getSidebarFactories().get(0);
    }

    /**
     * Builds a comparator that sorts first by the order, if specified, then by the display
     * name to break ties. It is assumed that the compared elements are all non-null
     * @return The comparator
     */
    private Comparator<AnnotationSidebarFactory> buildComparator()
    {
        return (asf1, asf2) -> new CompareToBuilder()
                .appendSuper(AnnotationAwareOrderComparator.INSTANCE.compare(asf1, asf2))
                .append(asf1.getDisplayName(), asf2.getDisplayName())
                .toComparison();
    }
}
{'fraction_non_alphanumeric': 0.08482871125611746, 'fraction_numerical': 0.006080379652973454, 'mean_word_length': 3.0872727272727274, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3443127', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1762, 'n_words': 467}