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.traverser;
020
021import org.eclipse.aether.collection.DependencyCollectionContext;
022import org.eclipse.aether.collection.DependencyTraverser;
023import org.eclipse.aether.graph.Dependency;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * A dependency traverser which always or never traverses children.
029 */
030public final class StaticDependencyTraverser implements DependencyTraverser {
031
032    private final boolean traverse;
033
034    /**
035     * Creates a new traverser with the specified traversal behavior.
036     *
037     * @param traverse {@code true} to traverse all dependencies, {@code false} to never traverse.
038     */
039    public StaticDependencyTraverser(boolean traverse) {
040        this.traverse = traverse;
041    }
042
043    public boolean traverseDependency(Dependency dependency) {
044        requireNonNull(dependency, "dependency cannot be null");
045        return traverse;
046    }
047
048    public DependencyTraverser deriveChildTraverser(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        StaticDependencyTraverser that = (StaticDependencyTraverser) obj;
062        return traverse == that.traverse;
063    }
064
065    @Override
066    public int hashCode() {
067        int hash = getClass().hashCode();
068        hash = hash * 31 + (traverse ? 1 : 0);
069        return hash;
070    }
071}