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 031import static java.util.Objects.requireNonNull; 032 033/** 034 * A dependency filter that combines zero or more other filters using a logical {@code AND}. The resulting filter 035 * accepts a given dependency node if and only if all constituent filters accept it. 036 */ 037public final class AndDependencyFilter 038 implements DependencyFilter 039{ 040 041 private final Set<DependencyFilter> filters = new LinkedHashSet<>(); 042 043 /** 044 * Creates a new filter from the specified filters. Prefer {@link #newInstance(DependencyFilter, DependencyFilter)} 045 * if any of the input filters might be {@code null}. 046 * 047 * @param filters The filters to combine, may be {@code null} but must not contain {@code null} elements. 048 */ 049 public AndDependencyFilter( DependencyFilter... filters ) 050 { 051 if ( filters != null ) 052 { 053 Collections.addAll( this.filters, filters ); 054 } 055 } 056 057 /** 058 * Creates a new filter from the specified filters. 059 * 060 * @param filters The filters to combine, may be {@code null} but must not contain {@code null} elements. 061 */ 062 public AndDependencyFilter( Collection<DependencyFilter> filters ) 063 { 064 if ( filters != null ) 065 { 066 this.filters.addAll( filters ); 067 } 068 } 069 070 /** 071 * Creates a new filter from the specified filters. 072 * 073 * @param filter1 The first filter to combine, may be {@code null}. 074 * @param filter2 The second filter to combine, may be {@code null}. 075 * @return The combined filter or {@code null} if both filter were {@code null}. 076 */ 077 public static DependencyFilter newInstance( DependencyFilter filter1, DependencyFilter filter2 ) 078 { 079 if ( filter1 == null ) 080 { 081 return filter2; 082 } 083 else if ( filter2 == null ) 084 { 085 return filter1; 086 } 087 return new AndDependencyFilter( filter1, filter2 ); 088 } 089 090 public boolean accept( DependencyNode node, List<DependencyNode> parents ) 091 { 092 requireNonNull( node, "node cannot be null" ); 093 requireNonNull( parents, "parents cannot be null" ); 094 for ( DependencyFilter filter : filters ) 095 { 096 if ( !filter.accept( node, parents ) ) 097 { 098 return false; 099 } 100 } 101 return true; 102 } 103 104 @Override 105 public boolean equals( Object obj ) 106 { 107 if ( this == obj ) 108 { 109 return true; 110 } 111 112 if ( obj == null || !getClass().equals( obj.getClass() ) ) 113 { 114 return false; 115 } 116 117 AndDependencyFilter that = (AndDependencyFilter) obj; 118 119 return this.filters.equals( that.filters ); 120 } 121 122 @Override 123 public int hashCode() 124 { 125 int hash = getClass().hashCode(); 126 hash = hash * 31 + filters.hashCode(); 127 return hash; 128 } 129 130}