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.filter; 020 021import java.util.Collection; 022import java.util.HashSet; 023import java.util.List; 024import java.util.Set; 025 026import org.eclipse.aether.graph.Dependency; 027import org.eclipse.aether.graph.DependencyFilter; 028import org.eclipse.aether.graph.DependencyNode; 029 030import static java.util.Objects.requireNonNull; 031 032/** 033 * A simple filter to exclude artifacts based on either artifact id or group id and artifact id. 034 */ 035public final class ExclusionsDependencyFilter implements DependencyFilter { 036 037 private final Set<String> excludes = new HashSet<>(); 038 039 /** 040 * Creates a new filter using the specified exclude patterns. A pattern can either be of the form 041 * {@code groupId:artifactId} (recommended) or just {@code artifactId} (deprecated). 042 * 043 * @param excludes The exclude patterns, may be {@code null} or empty to exclude no artifacts. 044 */ 045 public ExclusionsDependencyFilter(Collection<String> excludes) { 046 if (excludes != null) { 047 this.excludes.addAll(excludes); 048 } 049 } 050 051 public boolean accept(DependencyNode node, List<DependencyNode> parents) { 052 requireNonNull(node, "node cannot be null"); 053 requireNonNull(parents, "parents cannot be null"); 054 Dependency dependency = node.getDependency(); 055 056 if (dependency == null) { 057 return true; 058 } 059 060 String id = dependency.getArtifact().getArtifactId(); 061 062 if (excludes.contains(id)) { 063 return false; 064 } 065 066 id = dependency.getArtifact().getGroupId() + ':' + id; 067 068 return !(excludes.contains(id)); 069 } 070 071 @Override 072 public boolean equals(Object obj) { 073 if (this == obj) { 074 return true; 075 } 076 077 if (obj == null || !getClass().equals(obj.getClass())) { 078 return false; 079 } 080 081 ExclusionsDependencyFilter that = (ExclusionsDependencyFilter) obj; 082 083 return this.excludes.equals(that.excludes); 084 } 085 086 @Override 087 public int hashCode() { 088 int hash = 17; 089 hash = hash * 31 + excludes.hashCode(); 090 return hash; 091 } 092}