View Javadoc
1   /*
2    * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.revwalk.filter;
46  
47  import java.io.IOException;
48  import java.util.Collection;
49  
50  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
51  import org.eclipse.jgit.errors.MissingObjectException;
52  import org.eclipse.jgit.internal.JGitText;
53  import org.eclipse.jgit.revwalk.RevCommit;
54  import org.eclipse.jgit.revwalk.RevWalk;
55  
56  /**
57   * Includes a commit only if all subfilters include the same commit.
58   * <p>
59   * Classic shortcut behavior is used, so evaluation of the
60   * {@link org.eclipse.jgit.revwalk.filter.RevFilter#include(RevWalk, RevCommit)}
61   * method stops as soon as a false result is obtained. Applications can improve
62   * filtering performance by placing faster filters that are more likely to
63   * reject a result earlier in the list.
64   */
65  public abstract class AndRevFilter extends RevFilter {
66  	/**
67  	 * Create a filter with two filters, both of which must match.
68  	 *
69  	 * @param a
70  	 *            first filter to test.
71  	 * @param b
72  	 *            second filter to test.
73  	 * @return a filter that must match both input filters.
74  	 */
75  	public static RevFilter create(RevFilter a, RevFilter b) {
76  		if (a == ALL)
77  			return b;
78  		if (b == ALL)
79  			return a;
80  		return new Binary(a, b);
81  	}
82  
83  	/**
84  	 * Create a filter around many filters, all of which must match.
85  	 *
86  	 * @param list
87  	 *            list of filters to match against. Must contain at least 2
88  	 *            filters.
89  	 * @return a filter that must match all input filters.
90  	 */
91  	public static RevFilter create(RevFilter[] list) {
92  		if (list.length == 2)
93  			return create(list[0], list[1]);
94  		if (list.length < 2)
95  			throw new IllegalArgumentException(JGitText.get().atLeastTwoFiltersNeeded);
96  		final RevFilter[] subfilters = new RevFilter[list.length];
97  		System.arraycopy(list, 0, subfilters, 0, list.length);
98  		return new List(subfilters);
99  	}
100 
101 	/**
102 	 * Create a filter around many filters, all of which must match.
103 	 *
104 	 * @param list
105 	 *            list of filters to match against. Must contain at least 2
106 	 *            filters.
107 	 * @return a filter that must match all input filters.
108 	 */
109 	public static RevFilter create(Collection<RevFilter> list) {
110 		if (list.size() < 2)
111 			throw new IllegalArgumentException(JGitText.get().atLeastTwoFiltersNeeded);
112 		final RevFilter[] subfilters = new RevFilter[list.size()];
113 		list.toArray(subfilters);
114 		if (subfilters.length == 2)
115 			return create(subfilters[0], subfilters[1]);
116 		return new List(subfilters);
117 	}
118 
119 	private static class Binary extends AndRevFilter {
120 		private final RevFilter a;
121 
122 		private final RevFilter b;
123 
124 		private final boolean requiresCommitBody;
125 
126 		Binary(RevFilter one, RevFilter two) {
127 			a = one;
128 			b = two;
129 			requiresCommitBody = a.requiresCommitBody()
130 					|| b.requiresCommitBody();
131 		}
132 
133 		@Override
134 		public boolean include(RevWalk walker, RevCommit c)
135 				throws MissingObjectException, IncorrectObjectTypeException,
136 				IOException {
137 			return a.include(walker, c) && b.include(walker, c);
138 		}
139 
140 		@Override
141 		public boolean requiresCommitBody() {
142 			return requiresCommitBody;
143 		}
144 
145 		@Override
146 		public RevFilter clone() {
147 			return new Binary(a.clone(), b.clone());
148 		}
149 
150 		@SuppressWarnings("nls")
151 		@Override
152 		public String toString() {
153 			return "(" + a.toString() + " AND " + b.toString() + ")"; //$NON-NLS-1$
154 		}
155 	}
156 
157 	private static class List extends AndRevFilter {
158 		private final RevFilter[] subfilters;
159 
160 		private final boolean requiresCommitBody;
161 
162 		List(RevFilter[] list) {
163 			subfilters = list;
164 
165 			boolean rcb = false;
166 			for (RevFilter filter : subfilters)
167 				rcb |= filter.requiresCommitBody();
168 			requiresCommitBody = rcb;
169 		}
170 
171 		@Override
172 		public boolean include(RevWalk walker, RevCommit c)
173 				throws MissingObjectException, IncorrectObjectTypeException,
174 				IOException {
175 			for (RevFilter f : subfilters) {
176 				if (!f.include(walker, c))
177 					return false;
178 			}
179 			return true;
180 		}
181 
182 		@Override
183 		public boolean requiresCommitBody() {
184 			return requiresCommitBody;
185 		}
186 
187 		@Override
188 		public RevFilter clone() {
189 			final RevFilter[] s = new RevFilter[subfilters.length];
190 			for (int i = 0; i < s.length; i++)
191 				s[i] = subfilters[i].clone();
192 			return new List(s);
193 		}
194 
195 		@SuppressWarnings("nls")
196 		@Override
197 		public String toString() {
198 			final StringBuilder r = new StringBuilder();
199 			r.append("(");
200 			for (int i = 0; i < subfilters.length; i++) {
201 				if (i > 0)
202 					r.append(" AND ");
203 				r.append(subfilters[i].toString());
204 			}
205 			r.append(")");
206 			return r.toString();
207 		}
208 	}
209 }