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;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static com.google.common.base.Preconditions.checkState;
019
020import org.basepom.mojo.dvc.model.ResolverDefinition;
021import org.basepom.mojo.dvc.strategy.Strategy;
022import org.basepom.mojo.dvc.strategy.StrategyProvider;
023
024import java.util.Arrays;
025import java.util.Map;
026import java.util.concurrent.ExecutionException;
027
028import com.google.common.cache.Cache;
029import com.google.common.cache.CacheBuilder;
030import com.google.common.collect.ImmutableMap;
031
032public final class StrategyCache {
033
034    /**
035     * name of an artifact to  version resolution strategy.
036     */
037    private final Cache<QualifiedName, Strategy> resolverCache = CacheBuilder.newBuilder().concurrencyLevel(10).build();
038
039    private final ImmutableMap<QualifiedNameMatcher, Strategy> resolverPatterns;
040    private final Strategy defaultStrategy;
041
042    StrategyCache(final StrategyProvider strategyProvider, final ResolverDefinition[] resolvers, final String defaultStrategyName) {
043        checkNotNull(strategyProvider, "strategyProvider is null");
044        checkNotNull(resolvers, "resolvers is null");
045        checkNotNull(defaultStrategyName, "defaultStrategyName is null");
046
047        this.defaultStrategy = strategyProvider.forName(defaultStrategyName);
048        checkState(defaultStrategy != null, "Could not locate default version strategy '%s'", defaultStrategyName);
049
050        final ImmutableMap.Builder<QualifiedNameMatcher, Strategy> builder = ImmutableMap.builder();
051        Arrays.stream(resolvers).forEach(r -> {
052            final Strategy strategy = strategyProvider.forName(r.getStrategy());
053            checkState(strategy != null, "Could not locate version strategy %s! Check for typos!", r.getStrategy());
054            r.getIncludes().forEach(include -> builder.put(include, strategy));
055        });
056        this.resolverPatterns = builder.build();
057    }
058
059    public Strategy forQualifiedName(final QualifiedName name) {
060        checkNotNull(name, "name is null");
061        try {
062            return resolverCache.get(name, () -> {
063                for (final Map.Entry<QualifiedNameMatcher, Strategy> entry : resolverPatterns.entrySet()) {
064                    if (entry.getKey().matches(name)) {
065                        return entry.getValue();
066                    }
067                }
068                return defaultStrategy;
069            });
070        } catch (ExecutionException e) {
071            // ignore, never happens.
072            return defaultStrategy;
073        }
074    }
075}