001package org.eclipse.aether.util.filter;
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 java.util.Collection;
023import java.util.Collections;
024import java.util.LinkedHashSet;
025import java.util.List;
026import java.util.Set;
027
028import org.eclipse.aether.graph.DependencyFilter;
029import org.eclipse.aether.graph.DependencyNode;
030
031/**
032 * A dependency filter that combines zero or more other filters using a logical {@code OR}.
033 */
034public final class OrDependencyFilter
035    implements DependencyFilter
036{
037
038    private final Set<DependencyFilter> filters = new LinkedHashSet<DependencyFilter>();
039
040    /**
041     * Creates a new filter from the specified filters.
042     * 
043     * @param filters The filters to combine, may be {@code null}.
044     */
045    public OrDependencyFilter( DependencyFilter... filters )
046    {
047        if ( filters != null )
048        {
049            Collections.addAll( this.filters, filters );
050        }
051    }
052
053    /**
054     * Creates a new filter from the specified filters.
055     * 
056     * @param filters The filters to combine, may be {@code null}.
057     */
058    public OrDependencyFilter( Collection<DependencyFilter> filters )
059    {
060        if ( filters != null )
061        {
062            this.filters.addAll( filters );
063        }
064    }
065
066    /**
067     * Creates a new filter from the specified filters.
068     * 
069     * @param filter1 The first filter to combine, may be {@code null}.
070     * @param filter2 The first filter to combine, may be {@code null}.
071     * @return The combined filter or {@code null} if both filter were {@code null}.
072     */
073    public static DependencyFilter newInstance( DependencyFilter filter1, DependencyFilter filter2 )
074    {
075        if ( filter1 == null )
076        {
077            return filter2;
078        }
079        else if ( filter2 == null )
080        {
081            return filter1;
082        }
083        return new OrDependencyFilter( filter1, filter2 );
084    }
085
086    public boolean accept( DependencyNode node, List<DependencyNode> parents )
087    {
088        for ( DependencyFilter filter : filters )
089        {
090            if ( filter.accept( node, parents ) )
091            {
092                return true;
093            }
094        }
095        return false;
096    }
097
098    @Override
099    public boolean equals( Object obj )
100    {
101        if ( this == obj )
102        {
103            return true;
104        }
105
106        if ( obj == null || !getClass().equals( obj.getClass() ) )
107        {
108            return false;
109        }
110
111        OrDependencyFilter that = (OrDependencyFilter) obj;
112
113        return this.filters.equals( that.filters );
114    }
115
116    @Override
117    public int hashCode()
118    {
119        int hash = getClass().hashCode();
120        hash = hash * 31 + filters.hashCode();
121        return hash;
122    }
123
124}