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 java.util.ArrayList; 022import java.util.Arrays; 023import java.util.Collection; 024import java.util.HashSet; 025import java.util.Objects; 026import java.util.TreeSet; 027 028import org.eclipse.aether.collection.DependencyCollectionContext; 029import org.eclipse.aether.collection.DependencySelector; 030import org.eclipse.aether.graph.Dependency; 031 032import static java.util.Objects.requireNonNull; 033 034/** 035 * A dependency selector that filters transitive dependencies based on their scope. Direct dependencies are always 036 * included regardless of their scope. <em>Note:</em> This filter does not assume any relationships between the scopes. 037 * In particular, the filter is not aware of scopes that logically include other scopes. 038 * 039 * @see Dependency#getScope() 040 * @deprecated Scope semantics belong to the consumer project since Resolver 2.0.0. Obtain the selector from 041 * {@link org.eclipse.aether.scope.ScopeManager#getDependencySelector(org.eclipse.aether.RepositorySystemSession, 042 * org.eclipse.aether.scope.ResolutionScope)}, or use the session created by the {@code SessionBuilderSupplier} of 043 * the {@code maven-resolver-supplier-mvn3} or {@code maven-resolver-supplier-mvn4} module, which already carries the 044 * Maven scope-aware selectors. 045 */ 046@Deprecated 047public final class ScopeDependencySelector implements DependencySelector { 048 049 private final boolean transitive; 050 051 private final Collection<String> included; 052 053 private final Collection<String> excluded; 054 055 /** 056 * Creates a new selector using the specified includes and excludes. 057 * 058 * @param included the set of scopes to include, may be {@code null} or empty to include any scope 059 * @param excluded the set of scopes to exclude, may be {@code null} or empty to exclude no scope 060 */ 061 public ScopeDependencySelector(Collection<String> included, Collection<String> excluded) { 062 transitive = false; 063 this.included = clone(included); 064 this.excluded = clone(excluded); 065 } 066 067 private static Collection<String> clone(Collection<String> scopes) { 068 Collection<String> copy; 069 if (scopes == null || scopes.isEmpty()) { 070 // checking for null is faster than isEmpty() 071 copy = null; 072 } else { 073 copy = new HashSet<>(scopes); 074 if (copy.size() <= 2) { 075 // contains() is faster for smallish array (sorted for equals()!) 076 copy = new ArrayList<>(new TreeSet<>(copy)); 077 } 078 } 079 return copy; 080 } 081 082 /** 083 * Creates a new selector using the specified excludes. 084 * 085 * @param excluded the set of scopes to exclude, may be {@code null} or empty to exclude no scope 086 */ 087 public ScopeDependencySelector(String... excluded) { 088 this(null, (excluded != null) ? Arrays.asList(excluded) : null); 089 } 090 091 private ScopeDependencySelector(boolean transitive, Collection<String> included, Collection<String> excluded) { 092 this.transitive = transitive; 093 this.included = included; 094 this.excluded = excluded; 095 } 096 097 public boolean selectDependency(Dependency dependency) { 098 requireNonNull(dependency, "dependency cannot be null"); 099 if (!transitive) { 100 return true; 101 } 102 103 String scope = dependency.getScope(); 104 return (included == null || included.contains(scope)) && (excluded == null || !excluded.contains(scope)); 105 } 106 107 public DependencySelector deriveChildSelector(DependencyCollectionContext context) { 108 requireNonNull(context, "context cannot be null"); 109 if (this.transitive || context.getDependency() == null) { 110 return this; 111 } 112 113 return new ScopeDependencySelector(true, included, excluded); 114 } 115 116 @Override 117 public boolean equals(Object obj) { 118 if (this == obj) { 119 return true; 120 } else if (null == obj || !getClass().equals(obj.getClass())) { 121 return false; 122 } 123 124 ScopeDependencySelector that = (ScopeDependencySelector) obj; 125 return transitive == that.transitive 126 && Objects.equals(included, that.included) 127 && Objects.equals(excluded, that.excluded); 128 } 129 130 @Override 131 public int hashCode() { 132 int hash = 17; 133 hash = hash * 31 + (transitive ? 1 : 0); 134 hash = hash * 31 + (included != null ? included.hashCode() : 0); 135 hash = hash * 31 + (excluded != null ? excluded.hashCode() : 0); 136 return hash; 137 } 138 139 @Override 140 public String toString() { 141 return String.format( 142 "%s(included: %s, excluded: %s, transitive: %s)", 143 getClass().getSimpleName(), included, excluded, transitive); 144 } 145}