2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
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
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
;
49 * Includes a tree entry only if all subfilters include the same tree entry.
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
{
58 * Create a filter with two filters, both of which must match.
61 * first filter to test.
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
) {
71 return new Binary(a
, b
);
75 * Create a filter around many filters, all of which must match.
78 * list of filters to match against. Must contain at least 2
80 * @return a filter that must match all input filters.
82 public static TreeFilter
create(final TreeFilter
[] list
) {
84 return create(list
[0], list
[1]);
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
);
93 * Create a filter around many filters, all of which must match.
96 * list of filters to match against. Must contain at least 2
98 * @return a filter that must match all input filters.
100 public static TreeFilter
create(final Collection
<TreeFilter
> list
) {
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
) {
121 public boolean include(final TreeWalk walker
)
122 throws MissingObjectException
, IncorrectObjectTypeException
,
124 return a
.include(walker
) && b
.include(walker
);
128 public boolean shouldBeRecursive() {
129 return a
.shouldBeRecursive() || b
.shouldBeRecursive();
133 public TreeFilter
clone() {
134 return new Binary(a
.clone(), b
.clone());
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
) {
151 public boolean include(final TreeWalk walker
)
152 throws MissingObjectException
, IncorrectObjectTypeException
,
154 for (final TreeFilter f
: subfilters
) {
155 if (!f
.include(walker
))
162 public boolean shouldBeRecursive() {
163 for (final TreeFilter f
: subfilters
)
164 if (f
.shouldBeRecursive())
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();
178 public String
toString() {
179 final StringBuffer r
= new StringBuffer();
181 for (int i
= 0; i
< subfilters
.length
; i
++) {
184 r
.append(subfilters
[i
].toString());