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    @Override
044    public boolean traverseDependency(Dependency dependency) {
045        requireNonNull(dependency, "dependency cannot be null");
046        return traverse;
047    }
048
049    @Override
050    public DependencyTraverser deriveChildTraverser(DependencyCollectionContext context) {
051        requireNonNull(context, "context cannot be null");
052        return this;
053    }
054
055    @Override
056    public boolean equals(Object obj) {
057        if (this == obj) {
058            return true;
059        } else if (null == obj || !getClass().equals(obj.getClass())) {
060            return false;
061        }
062
063        StaticDependencyTraverser that = (StaticDependencyTraverser) obj;
064        return traverse == that.traverse;
065    }
066
067    @Override
068    public int hashCode() {
069        int hash = getClass().hashCode();
070        hash = hash * 31 + (traverse ? 1 : 0);
071        return hash;
072    }
073}