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.graph.selector;
020
021import org.eclipse.aether.collection.DependencyCollectionContext;
022import org.eclipse.aether.collection.DependencySelector;
023import org.eclipse.aether.graph.Dependency;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * A dependency selector that always includes or excludes dependencies.
029 */
030public final class StaticDependencySelector implements DependencySelector {
031
032    private final boolean select;
033
034    /**
035     * Creates a new selector with the specified selection behavior.
036     *
037     * @param select {@code true} to select all dependencies, {@code false} to exclude all dependencies.
038     */
039    public StaticDependencySelector(boolean select) {
040        this.select = select;
041    }
042
043    public boolean selectDependency(Dependency dependency) {
044        requireNonNull(dependency, "dependency cannot be null");
045        return select;
046    }
047
048    public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
049        requireNonNull(context, "context cannot be null");
050        return this;
051    }
052
053    @Override
054    public boolean equals(Object obj) {
055        if (this == obj) {
056            return true;
057        } else if (null == obj || !getClass().equals(obj.getClass())) {
058            return false;
059        }
060
061        StaticDependencySelector that = (StaticDependencySelector) obj;
062        return select == that.select;
063    }
064
065    @Override
066    public int hashCode() {
067        int hash = getClass().hashCode();
068        hash = hash * 31 + (select ? 1 : 0);
069        return hash;
070    }
071
072    @Override
073    public String toString() {
074        return String.format("%s(%s)", this.getClass().getSimpleName(), this.select ? "Select all" : "Exclude all");
075    }
076}