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.Iterator; 022 023import org.eclipse.aether.collection.DependencyCollectionContext; 024import org.eclipse.aether.collection.VersionFilter; 025import org.eclipse.aether.version.Version; 026 027/** 028 * A version filter that excludes any version except the lowest one. 029 * 030 * @since 2.0.0 031 */ 032public final class LowestVersionFilter implements VersionFilter { 033 private final int count; 034 035 /** 036 * Creates a new instance of this version filter. 037 */ 038 public LowestVersionFilter() { 039 this.count = 1; 040 } 041 042 /** 043 * Creates a new instance of this version filter. 044 */ 045 public LowestVersionFilter(int count) { 046 if (count < 1) { 047 throw new IllegalArgumentException("Count should be greater or equal to 1"); 048 } 049 this.count = count; 050 } 051 052 @Override 053 public void filterVersions(VersionFilterContext context) { 054 if (context.getCount() <= count) { 055 return; 056 } 057 // iterator comes in ascending order, basically we "step over" (leave) first few 058 int stepOver = count; 059 Iterator<Version> it = context.iterator(); 060 while (it.hasNext()) { 061 it.next(); 062 stepOver--; 063 if (stepOver < 0) { 064 it.remove(); 065 } 066 } 067 } 068 069 @Override 070 public VersionFilter deriveChildFilter(DependencyCollectionContext context) { 071 return this; 072 } 073 074 @Override 075 public boolean equals(Object obj) { 076 if (this == obj) { 077 return true; 078 } else if (null == obj || !getClass().equals(obj.getClass())) { 079 return false; 080 } 081 return true; 082 } 083 084 @Override 085 public int hashCode() { 086 return getClass().hashCode(); 087 } 088}