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
026/**
027 * A dependency selector that excludes optional dependencies which occur beyond level one of the dependency graph.
028 * 
029 * @see Dependency#isOptional()
030 */
031public final class OptionalDependencySelector
032    implements DependencySelector
033{
034
035    private final int depth;
036
037    /**
038     * Creates a new selector to exclude optional transitive dependencies.
039     */
040    public OptionalDependencySelector()
041    {
042        depth = 0;
043    }
044
045    private OptionalDependencySelector( int depth )
046    {
047        this.depth = depth;
048    }
049
050    public boolean selectDependency( Dependency dependency )
051    {
052        return depth < 2 || !dependency.isOptional();
053    }
054
055    public DependencySelector deriveChildSelector( DependencyCollectionContext context )
056    {
057        if ( depth >= 2 )
058        {
059            return this;
060        }
061
062        return new OptionalDependencySelector( depth + 1 );
063    }
064
065    @Override
066    public boolean equals( Object obj )
067    {
068        if ( this == obj )
069        {
070            return true;
071        }
072        else if ( null == obj || !getClass().equals( obj.getClass() ) )
073        {
074            return false;
075        }
076
077        OptionalDependencySelector that = (OptionalDependencySelector) obj;
078        return depth == that.depth;
079    }
080
081    @Override
082    public int hashCode()
083    {
084        int hash = getClass().hashCode();
085        hash = hash * 31 + depth;
086        return hash;
087    }
088
089}