001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.util.graph.version; 020 021import java.util.Objects; 022import java.util.function.Predicate; 023 024import org.eclipse.aether.RepositoryException; 025import org.eclipse.aether.collection.DependencyCollectionContext; 026import org.eclipse.aether.collection.VersionFilter; 027 028import static java.util.Objects.requireNonNull; 029 030/** 031 * A version filter that applies delegate version filter if context predicate applies. 032 * 033 * @since 2.0.17 034 */ 035public class ContextPredicateDelegatingVersionFilter implements VersionFilter { 036 private final Predicate<VersionFilterContext> contextPredicate; 037 private final VersionFilter delegate; 038 039 /** 040 * Creates a new instance of this version filter. 041 */ 042 public ContextPredicateDelegatingVersionFilter( 043 Predicate<VersionFilterContext> contextPredicate, VersionFilter delegate) { 044 this.contextPredicate = requireNonNull(contextPredicate); 045 this.delegate = requireNonNull(delegate); 046 } 047 048 @Override 049 public void filterVersions(VersionFilterContext context) throws RepositoryException { 050 if (contextPredicate.test(context)) { 051 delegate.filterVersions(context); 052 } 053 } 054 055 @Override 056 public VersionFilter deriveChildFilter(DependencyCollectionContext context) { 057 VersionFilter derived = delegate.deriveChildFilter(context); 058 if (derived == delegate) { 059 return this; 060 } else { 061 return new ContextPredicateDelegatingVersionFilter(contextPredicate, derived); 062 } 063 } 064 065 @Override 066 public boolean equals(Object o) { 067 if (this == o) { 068 return true; 069 } 070 if (o == null || getClass() != o.getClass()) { 071 return false; 072 } 073 ContextPredicateDelegatingVersionFilter that = (ContextPredicateDelegatingVersionFilter) o; 074 return Objects.equals(contextPredicate, that.contextPredicate) && Objects.equals(delegate, that.delegate); 075 } 076 077 @Override 078 public int hashCode() { 079 return Objects.hash(contextPredicate, delegate); 080 } 081}