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