View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.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.revwalk.filter;
12  
13  import java.io.IOException;
14  import java.util.Arrays;
15  
16  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
17  import org.eclipse.jgit.errors.MissingObjectException;
18  import org.eclipse.jgit.revwalk.RevCommit;
19  import org.eclipse.jgit.revwalk.RevFlag;
20  import org.eclipse.jgit.revwalk.RevFlagSet;
21  import org.eclipse.jgit.revwalk.RevWalk;
22  
23  /**
24   * Matches only commits with some/all RevFlags already set.
25   */
26  public abstract class RevFlagFilter extends RevFilter {
27  	/**
28  	 * Create a new filter that tests for a single flag.
29  	 *
30  	 * @param a
31  	 *            the flag to test.
32  	 * @return filter that selects only commits with flag <code>a</code>.
33  	 */
34  	public static RevFilter has(RevFlag a) {
35  		final RevFlagSet/RevFlagSet.html#RevFlagSet">RevFlagSet s = new RevFlagSet();
36  		s.add(a);
37  		return new HasAll(s);
38  	}
39  
40  	/**
41  	 * Create a new filter that tests all flags in a set.
42  	 *
43  	 * @param a
44  	 *            set of flags to test.
45  	 * @return filter that selects only commits with all flags in <code>a</code>.
46  	 */
47  	public static RevFilter hasAll(RevFlag... a) {
48  		final RevFlagSetevFlagSet.html#RevFlagSet">RevFlagSet set = new RevFlagSet();
49  		set.addAll(Arrays.asList(a));
50  		return new HasAll(set);
51  	}
52  
53  	/**
54  	 * Create a new filter that tests all flags in a set.
55  	 *
56  	 * @param a
57  	 *            set of flags to test.
58  	 * @return filter that selects only commits with all flags in <code>a</code>.
59  	 */
60  	public static RevFilter hasAll(RevFlagSet a) {
61  		return new HasAll(new RevFlagSet(a));
62  	}
63  
64  	/**
65  	 * Create a new filter that tests for any flag in a set.
66  	 *
67  	 * @param a
68  	 *            set of flags to test.
69  	 * @return filter that selects only commits with any flag in <code>a</code>.
70  	 */
71  	public static RevFilter hasAny(RevFlag... a) {
72  		final RevFlagSetevFlagSet.html#RevFlagSet">RevFlagSet set = new RevFlagSet();
73  		set.addAll(Arrays.asList(a));
74  		return new HasAny(set);
75  	}
76  
77  	/**
78  	 * Create a new filter that tests for any flag in a set.
79  	 *
80  	 * @param a
81  	 *            set of flags to test.
82  	 * @return filter that selects only commits with any flag in <code>a</code>.
83  	 */
84  	public static RevFilter hasAny(RevFlagSet a) {
85  		return new HasAny(new RevFlagSet(a));
86  	}
87  
88  	final RevFlagSet flags;
89  
90  	RevFlagFilter(RevFlagSet m) {
91  		flags = m;
92  	}
93  
94  	/** {@inheritDoc} */
95  	@Override
96  	public RevFilter clone() {
97  		return this;
98  	}
99  
100 	/** {@inheritDoc} */
101 	@Override
102 	public String toString() {
103 		return super.toString() + flags;
104 	}
105 
106 	private static class HasAll extends RevFlagFilter {
107 		HasAll(RevFlagSet m) {
108 			super(m);
109 		}
110 
111 		@Override
112 		public boolean include(RevWalk walker, RevCommit c)
113 				throws MissingObjectException, IncorrectObjectTypeException,
114 				IOException {
115 			return c.hasAll(flags);
116 		}
117 
118 		@Override
119 		public boolean requiresCommitBody() {
120 			return false;
121 		}
122 	}
123 
124 	private static class HasAny extends RevFlagFilter {
125 		HasAny(RevFlagSet m) {
126 			super(m);
127 		}
128 
129 		@Override
130 		public boolean include(RevWalk walker, RevCommit c)
131 				throws MissingObjectException, IncorrectObjectTypeException,
132 				IOException {
133 			return c.hasAny(flags);
134 		}
135 
136 		@Override
137 		public boolean requiresCommitBody() {
138 			return false;
139 		}
140 	}
141 }