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    public boolean accept(DependencyNode node, List<DependencyNode> parents) {
070        Dependency dependency = node.getDependency();
071
072        if (dependency == null) {
073            return true;
074        }
075
076        String scope = node.getDependency().getScope();
077        return (included.isEmpty() || included.contains(scope)) && (excluded.isEmpty() || !excluded.contains(scope));
078    }
079
080    @Override
081    public boolean equals(Object obj) {
082        if (this == obj) {
083            return true;
084        }
085
086        if (obj == null || !getClass().equals(obj.getClass())) {
087            return false;
088        }
089
090        ScopeDependencyFilter that = (ScopeDependencyFilter) obj;
091
092        return this.included.equals(that.included) && this.excluded.equals(that.excluded);
093    }
094
095    @Override
096    public int hashCode() {
097        int hash = 17;
098        hash = hash * 31 + included.hashCode();
099        hash = hash * 31 + excluded.hashCode();
100        return hash;
101    }
102}