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.treewalk.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.treewalk.TreeWalk;
54  
55  /**
56   * Includes a tree entry if any subfilters include the same tree entry.
57   * <p>
58   * Classic shortcut behavior is used, so evaluation of the
59   * {@link org.eclipse.jgit.treewalk.filter.TreeFilter#include(TreeWalk)} method
60   * stops as soon as a true result is obtained. Applications can improve
61   * filtering performance by placing faster filters that are more likely to
62   * accept a result earlier in the list.
63   */
64  public abstract class OrTreeFilter extends TreeFilter {
65  	/**
66  	 * Create a filter with two filters, one of which must match.
67  	 *
68  	 * @param a
69  	 *            first filter to test.
70  	 * @param b
71  	 *            second filter to test.
72  	 * @return a filter that must match at least one input filter.
73  	 */
74  	public static TreeFilter create(TreeFilter a, TreeFilter b) {
75  		if (a == ALL || b == ALL)
76  			return ALL;
77  		return new Binary(a, b);
78  	}
79  
80  	/**
81  	 * Create a filter around many filters, one of which must match.
82  	 *
83  	 * @param list
84  	 *            list of filters to match against. Must contain at least 2
85  	 *            filters.
86  	 * @return a filter that must match at least one input filter.
87  	 */
88  	public static TreeFilter create(TreeFilter[] list) {
89  		if (list.length == 2)
90  			return create(list[0], list[1]);
91  		if (list.length < 2)
92  			throw new IllegalArgumentException(JGitText.get().atLeastTwoFiltersNeeded);
93  		final TreeFilter[] subfilters = new TreeFilter[list.length];
94  		System.arraycopy(list, 0, subfilters, 0, list.length);
95  		return new List(subfilters);
96  	}
97  
98  	/**
99  	 * Create a filter around many filters, one of which must match.
100 	 *
101 	 * @param list
102 	 *            list of filters to match against. Must contain at least 2
103 	 *            filters.
104 	 * @return a filter that must match at least one input filter.
105 	 */
106 	public static TreeFilter create(Collection<TreeFilter> list) {
107 		if (list.size() < 2)
108 			throw new IllegalArgumentException(JGitText.get().atLeastTwoFiltersNeeded);
109 		final TreeFilter[] subfilters = new TreeFilter[list.size()];
110 		list.toArray(subfilters);
111 		if (subfilters.length == 2)
112 			return create(subfilters[0], subfilters[1]);
113 		return new List(subfilters);
114 	}
115 
116 	private static class Binary extends OrTreeFilter {
117 		private final TreeFilter a;
118 
119 		private final TreeFilter b;
120 
121 		Binary(TreeFilter one, TreeFilter two) {
122 			a = one;
123 			b = two;
124 		}
125 
126 		@Override
127 		public boolean include(TreeWalk walker)
128 				throws MissingObjectException, IncorrectObjectTypeException,
129 				IOException {
130 			return matchFilter(walker) <= 0;
131 		}
132 
133 		@Override
134 		public int matchFilter(TreeWalk walker)
135 				throws MissingObjectException, IncorrectObjectTypeException,
136 				IOException {
137 			final int ra = a.matchFilter(walker);
138 			if (ra == 0) {
139 				return 0;
140 			}
141 			final int rb = b.matchFilter(walker);
142 			if (rb == 0) {
143 				return 0;
144 			}
145 			if (ra == -1 || rb == -1) {
146 				return -1;
147 			}
148 			return 1;
149 		}
150 
151 		@Override
152 		public boolean shouldBeRecursive() {
153 			return a.shouldBeRecursive() || b.shouldBeRecursive();
154 		}
155 
156 		@Override
157 		public TreeFilter clone() {
158 			return new Binary(a.clone(), b.clone());
159 		}
160 
161 		@SuppressWarnings("nls")
162 		@Override
163 		public String toString() {
164 			return "(" + a.toString() + " OR " + b.toString() + ")";
165 		}
166 	}
167 
168 	private static class List extends OrTreeFilter {
169 		private final TreeFilter[] subfilters;
170 
171 		List(TreeFilter[] list) {
172 			subfilters = list;
173 		}
174 
175 		@Override
176 		public boolean include(TreeWalk walker)
177 				throws MissingObjectException, IncorrectObjectTypeException,
178 				IOException {
179 			return matchFilter(walker) <= 0;
180 		}
181 
182 		@Override
183 		public int matchFilter(TreeWalk walker)
184 				throws MissingObjectException, IncorrectObjectTypeException,
185 				IOException {
186 			int m = 1;
187 			for (TreeFilter f : subfilters) {
188 				int r = f.matchFilter(walker);
189 				if (r == 0) {
190 					return 0;
191 				}
192 				if (r == -1) {
193 					m = -1;
194 				}
195 			}
196 			return m;
197 		}
198 
199 		@Override
200 		public boolean shouldBeRecursive() {
201 			for (TreeFilter f : subfilters)
202 				if (f.shouldBeRecursive())
203 					return true;
204 			return false;
205 		}
206 
207 		@Override
208 		public TreeFilter clone() {
209 			final TreeFilter[] s = new TreeFilter[subfilters.length];
210 			for (int i = 0; i < s.length; i++)
211 				s[i] = subfilters[i].clone();
212 			return new List(s);
213 		}
214 
215 		@Override
216 		public String toString() {
217 			final StringBuilder r = new StringBuilder();
218 			r.append("("); //$NON-NLS-1$
219 			for (int i = 0; i < subfilters.length; i++) {
220 				if (i > 0)
221 					r.append(" OR "); //$NON-NLS-1$
222 				r.append(subfilters[i].toString());
223 			}
224 			r.append(")"); //$NON-NLS-1$
225 			return r.toString();
226 		}
227 	}
228 }