1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.util.graph.version;
20
21 import java.util.Iterator;
22
23 import org.eclipse.aether.collection.DependencyCollectionContext;
24 import org.eclipse.aether.collection.VersionFilter;
25 import org.eclipse.aether.version.Version;
26
27 /**
28 * A version filter that excludes any version except the lowest one.
29 *
30 * @since 2.0.0
31 */
32 public final class LowestVersionFilter implements VersionFilter {
33 private final int count;
34
35 /**
36 * Creates a new instance of this version filter.
37 */
38 public LowestVersionFilter() {
39 this.count = 1;
40 }
41
42 /**
43 * Creates a new instance of this version filter.
44 */
45 public LowestVersionFilter(int count) {
46 if (count < 1) {
47 throw new IllegalArgumentException("Count should be greater or equal to 1");
48 }
49 this.count = count;
50 }
51
52 @Override
53 public void filterVersions(VersionFilterContext context) {
54 if (context.getCount() <= count) {
55 return;
56 }
57 // iterator comes in ascending order, basically we "step over" (leave) first few
58 int stepOver = count;
59 Iterator<Version> it = context.iterator();
60 while (it.hasNext()) {
61 it.next();
62 stepOver--;
63 if (stepOver < 0) {
64 it.remove();
65 }
66 }
67 }
68
69 @Override
70 public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
71 return this;
72 }
73
74 @Override
75 public boolean equals(Object obj) {
76 if (this == obj) {
77 return true;
78 } else if (null == obj || !getClass().equals(obj.getClass())) {
79 return false;
80 }
81 return true;
82 }
83
84 @Override
85 public int hashCode() {
86 return getClass().hashCode();
87 }
88 }