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.Arrays;
023import java.util.Collection;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Set;
027
028import org.eclipse.aether.graph.Dependency;
029import org.eclipse.aether.graph.DependencyFilter;
030import org.eclipse.aether.graph.DependencyNode;
031
032/**
033 * A dependency filter based on dependency scopes. <em>Note:</em> This filter does not assume any relationships between
034 * the scopes. In particular, the filter is not aware of scopes that logically include other scopes.
035 * 
036 * @see Dependency#getScope()
037 */
038public final class ScopeDependencyFilter
039    implements DependencyFilter
040{
041
042    private final Set<String> included = new HashSet<String>();
043
044    private final Set<String> excluded = new HashSet<String>();
045
046    /**
047     * Creates a new filter using the specified includes and excludes.
048     * 
049     * @param included The set of scopes to include, may be {@code null} or empty to include any scope.
050     * @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
051     */
052    public ScopeDependencyFilter( Collection<String> included, Collection<String> excluded )
053    {
054        if ( included != null )
055        {
056            this.included.addAll( included );
057        }
058        if ( excluded != null )
059        {
060            this.excluded.addAll( excluded );
061        }
062    }
063
064    /**
065     * Creates a new filter using the specified excludes.
066     * 
067     * @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
068     */
069    public ScopeDependencyFilter( String... excluded )
070    {
071        if ( excluded != null )
072        {
073            this.excluded.addAll( Arrays.asList( excluded ) );
074        }
075    }
076
077    public boolean accept( DependencyNode node, List<DependencyNode> parents )
078    {
079        Dependency dependency = node.getDependency();
080
081        if ( dependency == null )
082        {
083            return true;
084        }
085
086        String scope = node.getDependency().getScope();
087        return ( included.isEmpty() || included.contains( scope ) )
088            && ( excluded.isEmpty() || !excluded.contains( scope ) );
089    }
090
091    @Override
092    public boolean equals( Object obj )
093    {
094        if ( this == obj )
095        {
096            return true;
097        }
098
099        if ( obj == null || !getClass().equals( obj.getClass() ) )
100        {
101            return false;
102        }
103
104        ScopeDependencyFilter that = (ScopeDependencyFilter) obj;
105
106        return this.included.equals( that.included ) && this.excluded.equals( that.excluded );
107    }
108
109    @Override
110    public int hashCode()
111    {
112        int hash = 17;
113        hash = hash * 31 + included.hashCode();
114        hash = hash * 31 + excluded.hashCode();
115        return hash;
116    }
117
118}