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