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.Collection;
023    import java.util.HashSet;
024    import java.util.Set;
025    
026    /**
027     * Filter to only retain objects in the given scope or better. This implementation allows the accumulation of multiple
028     * scopes and their associated implied scopes, so that the user can filter apply a series of implication rules in a
029     * single step. This should be a more efficient implementation of multiple standard {@link ScopeArtifactFilter}
030     * instances ORed together.
031     * 
032     * @author <a href="mailto:brett@apache.org">Brett Porter</a>
033     * @author jdcasey
034     */
035    public class CumulativeScopeArtifactFilter
036        extends AbstractScopeArtifactFilter
037    {
038    
039        private Set<String> scopes;
040    
041        /**
042         * Create a new filter with the specified scopes and their implied scopes enabled.
043         * 
044         * @param scopes The scopes to enable, along with all implied scopes, may be {@code null}.
045         */
046        public CumulativeScopeArtifactFilter( Collection<String> scopes )
047        {
048            this.scopes = new HashSet<String>();
049    
050            addScopes( scopes );
051        }
052    
053        /**
054         * Creates a new filter that combines the specified filters.
055         * 
056         * @param filters The filters to combine, may be {@code null}.
057         */
058        public CumulativeScopeArtifactFilter( CumulativeScopeArtifactFilter... filters )
059        {
060            this.scopes = new HashSet<String>();
061    
062            if ( filters != null )
063            {
064                for ( CumulativeScopeArtifactFilter filter : filters )
065                {
066                    addScopes( filter.getScopes() );
067                }
068            }
069        }
070    
071        private void addScopes( Collection<String> scopes )
072        {
073            if ( scopes != null )
074            {
075                for ( String scope : scopes )
076                {
077                    addScope( scope );
078                }
079            }
080        }
081    
082        private void addScope( String scope )
083        {
084            this.scopes.add( scope );
085    
086            addScopeInternal( scope );
087        }
088    
089        public Set<String> getScopes()
090        {
091            return scopes;
092        }
093    
094        @Override
095        public int hashCode()
096        {
097            int hash = 17;
098    
099            hash = hash * 31 + scopes.hashCode();
100    
101            return hash;
102        }
103    
104        @Override
105        public boolean equals( Object obj )
106        {
107            if ( this == obj )
108            {
109                return true;
110            }
111    
112            if ( !( obj instanceof CumulativeScopeArtifactFilter ) )
113            {
114                return false;
115            }
116    
117            CumulativeScopeArtifactFilter that = (CumulativeScopeArtifactFilter) obj;
118    
119            return scopes.equals( that.scopes );
120        }
121    
122    }