001package org.eclipse.aether.util.graph.selector;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.eclipse.aether.collection.DependencyCollectionContext;
023import org.eclipse.aether.collection.DependencySelector;
024import org.eclipse.aether.graph.Dependency;
025
026import static java.util.Objects.requireNonNull;
027
028/**
029 * A dependency selector that excludes optional dependencies which occur beyond level one of the dependency graph.
030 * 
031 * @see Dependency#isOptional()
032 */
033public final class OptionalDependencySelector
034    implements DependencySelector
035{
036
037    private final int depth;
038
039    /**
040     * Creates a new selector to exclude optional transitive dependencies.
041     */
042    public OptionalDependencySelector()
043    {
044        depth = 0;
045    }
046
047    private OptionalDependencySelector( int depth )
048    {
049        this.depth = depth;
050    }
051
052    public boolean selectDependency( Dependency dependency )
053    {
054        requireNonNull( dependency, "dependency cannot be null" );
055        return depth < 2 || !dependency.isOptional();
056    }
057
058    public DependencySelector deriveChildSelector( DependencyCollectionContext context )
059    {
060        requireNonNull( context, "context cannot be null" );
061        if ( depth >= 2 )
062        {
063            return this;
064        }
065
066        return new OptionalDependencySelector( depth + 1 );
067    }
068
069    @Override
070    public boolean equals( Object obj )
071    {
072        if ( this == obj )
073        {
074            return true;
075        }
076        else if ( null == obj || !getClass().equals( obj.getClass() ) )
077        {
078            return false;
079        }
080
081        OptionalDependencySelector that = (OptionalDependencySelector) obj;
082        return depth == that.depth;
083    }
084
085    @Override
086    public int hashCode()
087    {
088        int hash = getClass().hashCode();
089        hash = hash * 31 + depth;
090        return hash;
091    }
092
093    @Override
094    public String toString()
095    {
096        return String.format( "%s(depth: %d)", this.getClass().getSimpleName(), this.depth );
097    }
098
099}