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 if any 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 true result is obtained. Applications can improve
62   * filtering performance by placing faster filters that are more likely to
63   * accept a result earlier in the list.
64   */
65  public abstract class OrRevFilter extends RevFilter {
66  	/**
67  	 * Create a filter with two filters, one 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 at least one input filter.
74  	 */
75  	public static RevFilter create(RevFilter a, RevFilter b) {
76  		if (a == ALL || b == ALL)
77  			return ALL;
78  		return new Binary(a, b);
79  	}
80  
81  	/**
82  	 * Create a filter around many filters, one of which must match.
83  	 *
84  	 * @param list
85  	 *            list of filters to match against. Must contain at least 2
86  	 *            filters.
87  	 * @return a filter that must match at least one input filter.
88  	 */
89  	public static RevFilter create(RevFilter[] list) {
90  		if (list.length == 2)
91  			return create(list[0], list[1]);
92  		if (list.length < 2)
93  			throw new IllegalArgumentException(JGitText.get().atLeastTwoFiltersNeeded);
94  		final RevFilter[] subfilters = new RevFilter[list.length];
95  		System.arraycopy(list, 0, subfilters, 0, list.length);
96  		return new List(subfilters);
97  	}
98  
99  	/**
100 	 * Create a filter around many filters, one of which must match.
101 	 *
102 	 * @param list
103 	 *            list of filters to match against. Must contain at least 2
104 	 *            filters.
105 	 * @return a filter that must match at least one input filter.
106 	 */
107 	public static RevFilter create(Collection<RevFilter> list) {
108 		if (list.size() < 2)
109 			throw new IllegalArgumentException(JGitText.get().atLeastTwoFiltersNeeded);
110 		final RevFilter[] subfilters = new RevFilter[list.size()];
111 		list.toArray(subfilters);
112 		if (subfilters.length == 2)
113 			return create(subfilters[0], subfilters[1]);
114 		return new List(subfilters);
115 	}
116 
117 	private static class Binary extends OrRevFilter {
118 		private final RevFilter a;
119 
120 		private final RevFilter b;
121 
122 		private final boolean requiresCommitBody;
123 
124 		Binary(RevFilter one, RevFilter two) {
125 			a = one;
126 			b = two;
127 			requiresCommitBody = a.requiresCommitBody()
128 					|| b.requiresCommitBody();
129 		}
130 
131 		@Override
132 		public boolean include(RevWalk walker, RevCommit c)
133 				throws MissingObjectException, IncorrectObjectTypeException,
134 				IOException {
135 			return a.include(walker, c) || b.include(walker, c);
136 		}
137 
138 		@Override
139 		public boolean requiresCommitBody() {
140 			return requiresCommitBody;
141 		}
142 
143 		@Override
144 		public RevFilter clone() {
145 			return new Binary(a.clone(), b.clone());
146 		}
147 
148 		@SuppressWarnings("nls")
149 		@Override
150 		public String toString() {
151 			return "(" + a.toString() + " OR " + b.toString() + ")";
152 		}
153 	}
154 
155 	private static class List extends OrRevFilter {
156 		private final RevFilter[] subfilters;
157 
158 		private final boolean requiresCommitBody;
159 
160 		List(RevFilter[] list) {
161 			subfilters = list;
162 
163 			boolean rcb = false;
164 			for (RevFilter filter : subfilters)
165 				rcb |= filter.requiresCommitBody();
166 			requiresCommitBody = rcb;
167 		}
168 
169 		@Override
170 		public boolean include(RevWalk walker, RevCommit c)
171 				throws MissingObjectException, IncorrectObjectTypeException,
172 				IOException {
173 			for (RevFilter f : subfilters) {
174 				if (f.include(walker, c))
175 					return true;
176 			}
177 			return false;
178 		}
179 
180 		@Override
181 		public boolean requiresCommitBody() {
182 			return requiresCommitBody;
183 		}
184 
185 		@Override
186 		public RevFilter clone() {
187 			final RevFilter[] s = new RevFilter[subfilters.length];
188 			for (int i = 0; i < s.length; i++)
189 				s[i] = subfilters[i].clone();
190 			return new List(s);
191 		}
192 
193 		@Override
194 		public String toString() {
195 			final StringBuilder r = new StringBuilder();
196 			r.append("("); //$NON-NLS-1$
197 			for (int i = 0; i < subfilters.length; i++) {
198 				if (i > 0)
199 					r.append(" OR "); //$NON-NLS-1$
200 				r.append(subfilters[i].toString());
201 			}
202 			r.append(")"); //$NON-NLS-1$
203 			return r.toString();
204 		}
205 	}
206 }