001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.dvc.strategy;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static java.util.function.Function.identity;
019
020import java.util.List;
021import java.util.Locale;
022import java.util.Map;
023import javax.inject.Inject;
024import javax.inject.Named;
025import javax.inject.Singleton;
026
027import com.google.common.collect.ImmutableList;
028import com.google.common.collect.ImmutableMap;
029
030/**
031 * Default implementation for {@link StrategyProvider}.
032 */
033@Named("default")
034@Singleton
035public class DefaultStrategyProvider
036        implements StrategyProvider {
037
038    @Inject
039    protected List<Strategy> resolverDefinitions = ImmutableList.of();
040
041    @Override
042    public ImmutableMap<String, Strategy> getStrategies() {
043        return resolverDefinitions.stream().collect(ImmutableMap.toImmutableMap(r -> r.getName().toLowerCase(Locale.ENGLISH), identity()));
044    }
045
046    @Override
047    public Strategy forName(final String name) {
048        checkNotNull(name, "name is null");
049
050        final Map<String, Strategy> strategies = getStrategies();
051        return strategies.get(name.toLowerCase(Locale.ENGLISH));
052    }
053}
054