001package org.eclipse.aether.util.filter;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Collection;
023import java.util.HashSet;
024import java.util.List;
025import java.util.Set;
026
027import org.eclipse.aether.graph.Dependency;
028import org.eclipse.aether.graph.DependencyFilter;
029import org.eclipse.aether.graph.DependencyNode;
030
031import static java.util.Objects.requireNonNull;
032
033/**
034 * A simple filter to exclude artifacts based on either artifact id or group id and artifact id.
035 */
036public final class ExclusionsDependencyFilter
037    implements DependencyFilter
038{
039
040    private final Set<String> excludes = new HashSet<>();
041
042    /**
043     * Creates a new filter using the specified exclude patterns. A pattern can either be of the form
044     * {@code groupId:artifactId} (recommended) or just {@code artifactId} (deprecated).
045     * 
046     * @param excludes The exclude patterns, may be {@code null} or empty to exclude no artifacts.
047     */
048    public ExclusionsDependencyFilter( Collection<String> excludes )
049    {
050        if ( excludes != null )
051        {
052            this.excludes.addAll( excludes );
053        }
054    }
055
056    public boolean accept( DependencyNode node, List<DependencyNode> parents )
057    {
058        requireNonNull( node, "node cannot be null" );
059        requireNonNull( parents, "parents cannot be null" );
060        Dependency dependency = node.getDependency();
061
062        if ( dependency == null )
063        {
064            return true;
065        }
066
067        String id = dependency.getArtifact().getArtifactId();
068
069        if ( excludes.contains( id ) )
070        {
071            return false;
072        }
073
074        id = dependency.getArtifact().getGroupId() + ':' + id;
075
076        if ( excludes.contains( id ) )
077        {
078            return false;
079        }
080
081        return true;
082    }
083
084    @Override
085    public boolean equals( Object obj )
086    {
087        if ( this == obj )
088        {
089            return true;
090        }
091
092        if ( obj == null || !getClass().equals( obj.getClass() ) )
093        {
094            return false;
095        }
096
097        ExclusionsDependencyFilter that = (ExclusionsDependencyFilter) obj;
098
099        return this.excludes.equals( that.excludes );
100    }
101
102    @Override
103    public int hashCode()
104    {
105        int hash = 17;
106        hash = hash * 31 + excludes.hashCode();
107        return hash;
108    }
109
110}