001/* 002 * Copyright 2012-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.maven; 018 019import java.util.HashSet; 020import java.util.List; 021import java.util.Set; 022 023import org.apache.maven.artifact.Artifact; 024import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter; 025import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; 026import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; 027 028/** 029 * Base class for {@link ArtifactsFilter} based on a {@link FilterableDependency} list. 030 * 031 * @author Stephane Nicoll 032 * @author David Turanski 033 * @since 1.2 034 */ 035public abstract class DependencyFilter extends AbstractArtifactsFilter { 036 037 private final List<? extends FilterableDependency> filters; 038 039 /** 040 * Create a new instance with the list of {@link FilterableDependency} instance(s) to 041 * use. 042 * @param dependencies the source dependencies 043 */ 044 public DependencyFilter(List<? extends FilterableDependency> dependencies) { 045 this.filters = dependencies; 046 } 047 048 @Override 049 @SuppressWarnings({ "rawtypes", "unchecked" }) 050 public Set filter(Set artifacts) throws ArtifactFilterException { 051 Set result = new HashSet(); 052 for (Object artifact : artifacts) { 053 if (!filter((Artifact) artifact)) { 054 result.add(artifact); 055 } 056 } 057 return result; 058 } 059 060 protected abstract boolean filter(Artifact artifact); 061 062 /** 063 * Check if the specified {@link org.apache.maven.artifact.Artifact} matches the 064 * specified {@link org.springframework.boot.maven.FilterableDependency}. Returns 065 * {@code true} if it should be excluded 066 * @param artifact the Maven {@link Artifact} 067 * @param dependency the {@link FilterableDependency} 068 * @return {@code true} if the artifact matches the dependency 069 */ 070 protected final boolean equals(Artifact artifact, FilterableDependency dependency) { 071 if (!dependency.getGroupId().equals(artifact.getGroupId())) { 072 return false; 073 } 074 if (!dependency.getArtifactId().equals(artifact.getArtifactId())) { 075 return false; 076 } 077 return (dependency.getClassifier() == null || artifact.getClassifier() != null 078 && dependency.getClassifier().equals(artifact.getClassifier())); 079 } 080 081 protected final List<? extends FilterableDependency> getFilters() { 082 return this.filters; 083 } 084 085}