View Javadoc
1   /*
2    * Copyright (C) 2013, Robin Stocker <robin@nibor.org> 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  
11  package org.eclipse.jgit.treewalk.filter;
12  
13  import java.io.IOException;
14  import java.text.MessageFormat;
15  
16  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
17  import org.eclipse.jgit.errors.MissingObjectException;
18  import org.eclipse.jgit.errors.StopWalkException;
19  import org.eclipse.jgit.internal.JGitText;
20  import org.eclipse.jgit.treewalk.TreeWalk;
21  
22  /**
23   * For testing an array of {@link org.eclipse.jgit.treewalk.filter.TreeFilter}
24   * during a {@link org.eclipse.jgit.treewalk.TreeWalk} for each entry and
25   * returning the result as a bitmask.
26   *
27   * @since 2.3
28   */
29  public class TreeFilterMarker {
30  
31  	private final TreeFilter[] filters;
32  
33  	/**
34  	 * Construct a TreeFilterMarker. Note that it is stateful and can only be
35  	 * used for one walk loop.
36  	 *
37  	 * @param markTreeFilters
38  	 *            the filters to use for marking, must not have more elements
39  	 *            than {@link java.lang.Integer#SIZE}.
40  	 * @throws java.lang.IllegalArgumentException
41  	 *             if more tree filters are passed than possible
42  	 */
43  	public TreeFilterMarker(TreeFilter[] markTreeFilters) {
44  		if (markTreeFilters.length > Integer.SIZE) {
45  			throw new IllegalArgumentException(MessageFormat.format(
46  					JGitText.get().treeFilterMarkerTooManyFilters,
47  					Integer.valueOf(Integer.SIZE),
48  					Integer.valueOf(markTreeFilters.length)));
49  		}
50  		filters = new TreeFilter[markTreeFilters.length];
51  		System.arraycopy(markTreeFilters, 0, filters, 0, markTreeFilters.length);
52  	}
53  
54  	/**
55  	 * Test the filters against the walk. Returns a bitmask where each bit
56  	 * represents the result of a call to
57  	 * {@link org.eclipse.jgit.treewalk.filter.TreeFilter#include(TreeWalk)},
58  	 * ordered by the index for which the tree filters were passed in the
59  	 * constructor.
60  	 *
61  	 * @param walk
62  	 *            the walk from which to test the current entry
63  	 * @return the marks bitmask
64  	 * @throws org.eclipse.jgit.errors.MissingObjectException
65  	 *             as thrown by
66  	 *             {@link org.eclipse.jgit.treewalk.filter.TreeFilter#include(TreeWalk)}
67  	 * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
68  	 *             as thrown by
69  	 *             {@link org.eclipse.jgit.treewalk.filter.TreeFilter#include(TreeWalk)}
70  	 * @throws java.io.IOException
71  	 *             as thrown by
72  	 *             {@link org.eclipse.jgit.treewalk.filter.TreeFilter#include(TreeWalk)}
73  	 */
74  	public int getMarks(TreeWalk walk) throws MissingObjectException,
75  			IncorrectObjectTypeException, IOException {
76  		int marks = 0;
77  		for (int index = 0; index < filters.length; index++) {
78  			TreeFilter filter = filters[index];
79  			if (filter != null) {
80  				try {
81  					boolean marked = filter.include(walk);
82  					if (marked)
83  						marks |= (1 << index);
84  				} catch (StopWalkException e) {
85  					// Don't check tree filter anymore, it will no longer
86  					// match
87  					filters[index] = null;
88  				}
89  			}
90  		}
91  		return marks;
92  	}
93  
94  }