001    package 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    
022    import java.util.ArrayList;
023    import java.util.Iterator;
024    import java.util.LinkedHashSet;
025    import java.util.List;
026    import java.util.Set;
027    
028    import org.apache.maven.artifact.Artifact;
029    
030    /**
031     * Filter to include from a list of artifact patterns.
032     *
033     * @author <a href="mailto:brett@apache.org">Brett Porter</a>
034     */
035    public class IncludesArtifactFilter
036        implements ArtifactFilter
037    {
038        private final Set<String> patterns;
039    
040        public IncludesArtifactFilter( List<String> patterns )
041        {
042            this.patterns = new LinkedHashSet<String>( patterns );
043        }
044    
045        public boolean include( Artifact artifact )
046        {
047            String id = artifact.getGroupId() + ":" + artifact.getArtifactId();
048    
049            boolean matched = false;
050            for ( Iterator<String> i = patterns.iterator(); i.hasNext() & !matched; )
051            {
052                // TODO: what about wildcards? Just specifying groups? versions?
053                if ( id.equals( i.next() ) )
054                {
055                    matched = true;
056                }
057            }
058            return matched;
059        }
060    
061        public List<String> getPatterns()
062        {
063            return new ArrayList<String>( patterns );
064        }
065    
066        @Override
067        public int hashCode()
068        {
069            int hash = 17;
070            hash = hash * 31 + patterns.hashCode();
071            
072            return hash;
073        }
074    
075        @Override
076        public boolean equals( Object obj )
077        {
078            if ( this == obj ) 
079            {
080                return true;
081            }
082    
083            // make sure IncludesArtifactFilter is not equal ExcludesArtifactFilter! 
084            if ( obj == null || getClass() != obj.getClass() )
085            {
086                return false;
087            }
088    
089            IncludesArtifactFilter other = (IncludesArtifactFilter) obj;
090    
091            return patterns.equals( other.patterns );
092        }
093    }