001package org.apache.maven.artifact.resolver.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.ArrayList;
023import java.util.Iterator;
024import java.util.LinkedHashSet;
025import java.util.List;
026import java.util.Set;
027
028import org.apache.maven.artifact.Artifact;
029
030/**
031 * Apply multiple filters.
032 *
033 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
034 */
035public class AndArtifactFilter
036    implements ArtifactFilter
037{
038    private Set<ArtifactFilter> filters; 
039
040    public AndArtifactFilter()
041    {
042        this.filters = new LinkedHashSet<ArtifactFilter>();
043    }
044
045    public AndArtifactFilter( List<ArtifactFilter> filters )
046    {
047        this.filters = new LinkedHashSet<ArtifactFilter>( filters );
048    }
049    
050    public boolean include( Artifact artifact )
051    {
052        boolean include = true;
053        for ( Iterator<ArtifactFilter> i = filters.iterator(); i.hasNext() && include; )
054        {
055            ArtifactFilter filter = i.next();
056            if ( !filter.include( artifact ) )
057            {
058                include = false;
059            }
060        }
061        return include;
062    }
063
064    public void add( ArtifactFilter artifactFilter )
065    {
066        filters.add( artifactFilter );
067    }
068
069    public List<ArtifactFilter> getFilters()
070    {
071        return new ArrayList<ArtifactFilter>( filters );
072    }
073
074    @Override
075    public int hashCode()
076    {
077        int hash = 17;
078        hash = hash * 31 + filters.hashCode();
079        return hash;
080    }
081
082    @Override
083    public boolean equals( Object obj )
084    {
085        if ( this == obj )
086        {
087            return true;
088        }
089        
090        if ( !( obj instanceof AndArtifactFilter ) )
091        {
092            return false;
093        }
094        
095        AndArtifactFilter other = (AndArtifactFilter) obj;
096        
097        return filters.equals( other.filters );
098    }
099}