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