001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util.graph.transformer;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.HashSet;
024import java.util.Iterator;
025
026import org.eclipse.aether.RepositoryException;
027import org.eclipse.aether.collection.UnsolvableVersionConflictException;
028import org.eclipse.aether.graph.DependencyFilter;
029import org.eclipse.aether.graph.DependencyNode;
030import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictContext;
031import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictItem;
032import org.eclipse.aether.util.graph.transformer.ConflictResolver.VersionSelector;
033import org.eclipse.aether.util.graph.visitor.PathRecordingDependencyVisitor;
034import org.eclipse.aether.util.graph.visitor.TreeDependencyVisitor;
035import org.eclipse.aether.version.Version;
036import org.eclipse.aether.version.VersionConstraint;
037
038import static java.util.Objects.requireNonNull;
039
040/**
041 * A version selector for use with {@link ConflictResolver} that resolves version conflicts using a nearest-wins
042 * strategy. If there is no single node that satisfies all encountered version ranges, the selector will fail.
043 *
044 * @deprecated Use {@link ConfigurableVersionSelector} instead.
045 */
046@Deprecated
047public final class NearestVersionSelector extends VersionSelector {
048
049    /**
050     * Creates a new instance of this version selector.
051     */
052    public NearestVersionSelector() {}
053
054    @Override
055    public void selectVersion(ConflictContext context) throws RepositoryException {
056        ConflictGroup group = new ConflictGroup();
057        for (ConflictItem item : context.getItems()) {
058            DependencyNode node = item.getNode();
059            VersionConstraint constraint = node.getVersionConstraint();
060
061            boolean backtrack = false;
062            boolean hardConstraint = constraint.getRange() != null;
063
064            if (hardConstraint) {
065                if (group.constraints.add(constraint)) {
066                    if (group.winner != null
067                            && !constraint.containsVersion(
068                                    group.winner.getNode().getVersion())) {
069                        backtrack = true;
070                    }
071                }
072            }
073
074            if (isAcceptable(group, node.getVersion())) {
075                group.candidates.add(item);
076
077                if (backtrack) {
078                    backtrack(group, context);
079                } else if (group.winner == null || isNearer(item, group.winner)) {
080                    group.winner = item;
081                }
082            } else if (backtrack) {
083                backtrack(group, context);
084            }
085        }
086        context.setWinner(group.winner);
087    }
088
089    private void backtrack(ConflictGroup group, ConflictContext context) throws UnsolvableVersionConflictException {
090        group.winner = null;
091
092        for (Iterator<ConflictItem> it = group.candidates.iterator(); it.hasNext(); ) {
093            ConflictItem candidate = it.next();
094
095            if (!isAcceptable(group, candidate.getNode().getVersion())) {
096                it.remove();
097            } else if (group.winner == null || isNearer(candidate, group.winner)) {
098                group.winner = candidate;
099            }
100        }
101
102        if (group.winner == null) {
103            throw newFailure(context);
104        }
105    }
106
107    private boolean isAcceptable(ConflictGroup group, Version version) {
108        for (VersionConstraint constraint : group.constraints) {
109            if (!constraint.containsVersion(version)) {
110                return false;
111            }
112        }
113        return true;
114    }
115
116    private boolean isNearer(ConflictItem item1, ConflictItem item2) {
117        if (item1.isSibling(item2)) {
118            return item1.getNode().getVersion().compareTo(item2.getNode().getVersion()) > 0;
119        } else {
120            return item1.getDepth() < item2.getDepth();
121        }
122    }
123
124    private UnsolvableVersionConflictException newFailure(final ConflictContext context) {
125        DependencyFilter filter = (node, parents) -> {
126            requireNonNull(node, "node cannot be null");
127            requireNonNull(parents, "parents cannot be null");
128            return context.isIncluded(node);
129        };
130        PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(filter);
131        context.getRoot().accept(new TreeDependencyVisitor(visitor));
132        return new UnsolvableVersionConflictException(visitor.getPaths());
133    }
134
135    static final class ConflictGroup {
136
137        final Collection<VersionConstraint> constraints;
138
139        final Collection<ConflictItem> candidates;
140
141        ConflictItem winner;
142
143        ConflictGroup() {
144            constraints = new HashSet<>();
145            candidates = new ArrayList<>(64);
146        }
147
148        @Override
149        public String toString() {
150            return String.valueOf(winner);
151        }
152    }
153}