View Javadoc
1   /*
2    * Copyright (C) 2011, Tomasz Zarna <Tomasz.Zarna@pl.ibm.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.revwalk.filter;
11  
12  import java.io.IOException;
13  
14  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
15  import org.eclipse.jgit.errors.MissingObjectException;
16  import org.eclipse.jgit.errors.StopWalkException;
17  import org.eclipse.jgit.internal.JGitText;
18  import org.eclipse.jgit.revwalk.RevCommit;
19  import org.eclipse.jgit.revwalk.RevWalk;
20  
21  /**
22   * Limits the number of commits output.
23   */
24  public class MaxCountRevFilter extends RevFilter {
25  
26  	private int maxCount;
27  
28  	private int count;
29  
30  	/**
31  	 * Create a new max count filter.
32  	 *
33  	 * @param maxCount
34  	 *            the limit
35  	 * @return a new filter
36  	 */
37  	public static RevFilter create(int maxCount) {
38  		if (maxCount < 0)
39  			throw new IllegalArgumentException(
40  					JGitText.get().maxCountMustBeNonNegative);
41  		return new MaxCountRevFilter(maxCount);
42  	}
43  
44  	private MaxCountRevFilter(int maxCount) {
45  		this.count = 0;
46  		this.maxCount = maxCount;
47  	}
48  
49  	/** {@inheritDoc} */
50  	@Override
51  	public boolean include(RevWalk walker, RevCommit cmit)
52  			throws StopWalkException, MissingObjectException,
53  			IncorrectObjectTypeException, IOException {
54  		count++;
55  		if (count > maxCount)
56  			throw StopWalkException.INSTANCE;
57  		return true;
58  	}
59  
60  	/** {@inheritDoc} */
61  	@Override
62  	public RevFilter clone() {
63  		return new MaxCountRevFilter(maxCount);
64  	}
65  }