Remove System.out.println from RevWalkFilterTest
[egit/imyousuf.git] / org.spearce.jgit / src / org / spearce / jgit / treewalk / filter / AndTreeFilter.java
blob7f1e39ace5aa3de72f9c1c3e307f99dc10e732d5
1 /*
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.treewalk.filter;
41 import java.io.IOException;
42 import java.util.Collection;
44 import org.spearce.jgit.errors.IncorrectObjectTypeException;
45 import org.spearce.jgit.errors.MissingObjectException;
46 import org.spearce.jgit.treewalk.TreeWalk;
48 /**
49 * Includes a tree entry only if all subfilters include the same tree entry.
50 * <p>
51 * Classic shortcut behavior is used, so evaluation of the
52 * {@link TreeFilter#include(TreeWalk)} method stops as soon as a false result
53 * is obtained. Applications can improve filtering performance by placing faster
54 * filters that are more likely to reject a result earlier in the list.
56 public abstract class AndTreeFilter extends TreeFilter {
57 /**
58 * Create a filter with two filters, both of which must match.
60 * @param a
61 * first filter to test.
62 * @param b
63 * second filter to test.
64 * @return a filter that must match both input filters.
66 public static TreeFilter create(final TreeFilter a, final TreeFilter b) {
67 if (a == ALL)
68 return b;
69 if (b == ALL)
70 return a;
71 return new Binary(a, b);
74 /**
75 * Create a filter around many filters, all of which must match.
77 * @param list
78 * list of filters to match against. Must contain at least 2
79 * filters.
80 * @return a filter that must match all input filters.
82 public static TreeFilter create(final TreeFilter[] list) {
83 if (list.length == 2)
84 return create(list[0], list[1]);
85 if (list.length < 2)
86 throw new IllegalArgumentException("At least two filters needed.");
87 final TreeFilter[] subfilters = new TreeFilter[list.length];
88 System.arraycopy(list, 0, subfilters, 0, list.length);
89 return new List(subfilters);
92 /**
93 * Create a filter around many filters, all of which must match.
95 * @param list
96 * list of filters to match against. Must contain at least 2
97 * filters.
98 * @return a filter that must match all input filters.
100 public static TreeFilter create(final Collection<TreeFilter> list) {
101 if (list.size() < 2)
102 throw new IllegalArgumentException("At least two filters needed.");
103 final TreeFilter[] subfilters = new TreeFilter[list.size()];
104 list.toArray(subfilters);
105 if (subfilters.length == 2)
106 return create(subfilters[0], subfilters[1]);
107 return new List(subfilters);
110 private static class Binary extends AndTreeFilter {
111 private final TreeFilter a;
113 private final TreeFilter b;
115 Binary(final TreeFilter one, final TreeFilter two) {
116 a = one;
117 b = two;
120 @Override
121 public boolean include(final TreeWalk walker)
122 throws MissingObjectException, IncorrectObjectTypeException,
123 IOException {
124 return a.include(walker) && b.include(walker);
127 @Override
128 public boolean shouldBeRecursive() {
129 return a.shouldBeRecursive() || b.shouldBeRecursive();
132 @Override
133 public TreeFilter clone() {
134 return new Binary(a.clone(), b.clone());
137 @Override
138 public String toString() {
139 return "(" + a.toString() + " AND " + b.toString() + ")";
143 private static class List extends AndTreeFilter {
144 private final TreeFilter[] subfilters;
146 List(final TreeFilter[] list) {
147 subfilters = list;
150 @Override
151 public boolean include(final TreeWalk walker)
152 throws MissingObjectException, IncorrectObjectTypeException,
153 IOException {
154 for (final TreeFilter f : subfilters) {
155 if (!f.include(walker))
156 return false;
158 return true;
161 @Override
162 public boolean shouldBeRecursive() {
163 for (final TreeFilter f : subfilters)
164 if (f.shouldBeRecursive())
165 return true;
166 return false;
169 @Override
170 public TreeFilter clone() {
171 final TreeFilter[] s = new TreeFilter[subfilters.length];
172 for (int i = 0; i < s.length; i++)
173 s[i] = subfilters[i].clone();
174 return new List(s);
177 @Override
178 public String toString() {
179 final StringBuffer r = new StringBuffer();
180 r.append("(");
181 for (int i = 0; i < subfilters.length; i++) {
182 if (i > 0)
183 r.append(" AND ");
184 r.append(subfilters[i].toString());
186 r.append(")");
187 return r.toString();