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.selector;
020
021import org.eclipse.aether.collection.DependencyCollectionContext;
022import org.eclipse.aether.collection.DependencySelector;
023import org.eclipse.aether.graph.Dependency;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * A dependency selector that excludes optional dependencies which occur beyond level one of the dependency graph.
029 *
030 * @see Dependency#isOptional()
031 * @deprecated Scope semantics belong to the consumer project since Resolver 2.0.0. Obtain the selector from
032 * {@link org.eclipse.aether.scope.ScopeManager#getDependencySelector(org.eclipse.aether.RepositorySystemSession,
033 * org.eclipse.aether.scope.ResolutionScope)}, or use the session created by the {@code SessionBuilderSupplier} of
034 * the {@code maven-resolver-supplier-mvn3} or {@code maven-resolver-supplier-mvn4} module, which already carries the
035 * Maven scope-aware selectors.
036 */
037@Deprecated
038public final class OptionalDependencySelector implements DependencySelector {
039
040    private final int depth;
041
042    /**
043     * Creates a new selector to exclude optional transitive dependencies.
044     */
045    public OptionalDependencySelector() {
046        depth = 0;
047    }
048
049    private OptionalDependencySelector(int depth) {
050        this.depth = depth;
051    }
052
053    public boolean selectDependency(Dependency dependency) {
054        requireNonNull(dependency, "dependency cannot be null");
055        return depth < 2 || !dependency.isOptional();
056    }
057
058    public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
059        requireNonNull(context, "context cannot be null");
060        if (depth >= 2) {
061            return this;
062        }
063
064        return new OptionalDependencySelector(depth + 1);
065    }
066
067    @Override
068    public boolean equals(Object obj) {
069        if (this == obj) {
070            return true;
071        } else if (null == obj || !getClass().equals(obj.getClass())) {
072            return false;
073        }
074
075        OptionalDependencySelector that = (OptionalDependencySelector) obj;
076        return depth == that.depth;
077    }
078
079    @Override
080    public int hashCode() {
081        int hash = getClass().hashCode();
082        hash = hash * 31 + depth;
083        return hash;
084    }
085
086    @Override
087    public String toString() {
088        return String.format("%s(depth: %d)", this.getClass().getSimpleName(), this.depth);
089    }
090}