001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.List;
023import java.util.Set;
024import java.util.stream.Collectors;
025
026import org.springframework.boot.context.annotation.Configurations;
027import org.springframework.core.Ordered;
028import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
029import org.springframework.util.ClassUtils;
030
031/**
032 * {@link Configurations} representing auto-configuration {@code @Configuration} classes.
033 *
034 * @author Phillip Webb
035 * @since 2.0.0
036 */
037public class AutoConfigurations extends Configurations implements Ordered {
038
039        private static final AutoConfigurationSorter SORTER = new AutoConfigurationSorter(
040                        new SimpleMetadataReaderFactory(), null);
041
042        private static final Ordered ORDER = new AutoConfigurationImportSelector();
043
044        protected AutoConfigurations(Collection<Class<?>> classes) {
045                super(classes);
046        }
047
048        @Override
049        protected Collection<Class<?>> sort(Collection<Class<?>> classes) {
050                List<String> names = classes.stream().map(Class::getName)
051                                .collect(Collectors.toList());
052                List<String> sorted = SORTER.getInPriorityOrder(names);
053                return sorted.stream()
054                                .map((className) -> ClassUtils.resolveClassName(className, null))
055                                .collect(Collectors.toCollection(ArrayList::new));
056        }
057
058        @Override
059        public int getOrder() {
060                return ORDER.getOrder();
061        }
062
063        @Override
064        protected AutoConfigurations merge(Set<Class<?>> mergedClasses) {
065                return new AutoConfigurations(mergedClasses);
066        }
067
068        public static AutoConfigurations of(Class<?>... classes) {
069                return new AutoConfigurations(Arrays.asList(classes));
070        }
071
072}