2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
5
This program and the accompanying materials are made available
6
under the terms of the Eclipse Distribution License v1.0 which
7
accompanies this distribution, is reproduced below, and is
8
available at http://www.eclipse.org/org/documents/edl-v10.php
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
19
- Redistributions in binary form must reproduce the above
20
copyright notice, this list of conditions and the following
21
disclaimer in the documentation and/or other materials provided
22
with the distribution.
24
- Neither the name of the Eclipse Foundation, Inc. nor the
25
names of its contributors may be used to endorse or promote
26
products derived from this software without specific prior
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
using System.Collections.Generic;
49
using NGit.Treewalk.Filter;
52
namespace NGit.Treewalk.Filter
54
/// <summary>Includes a tree entry only if all subfilters include the same tree entry.
57
/// Includes a tree entry only if all subfilters include the same tree entry.
59
/// Classic shortcut behavior is used, so evaluation of the
60
/// <see cref="TreeFilter.Include(NGit.Treewalk.TreeWalk)">TreeFilter.Include(NGit.Treewalk.TreeWalk)
62
/// method stops as soon as a false result
63
/// is obtained. Applications can improve filtering performance by placing faster
64
/// filters that are more likely to reject a result earlier in the list.
66
public abstract class AndTreeFilter : TreeFilter
68
/// <summary>Create a filter with two filters, both of which must match.</summary>
69
/// <remarks>Create a filter with two filters, both of which must match.</remarks>
70
/// <param name="a">first filter to test.</param>
71
/// <param name="b">second filter to test.</param>
72
/// <returns>a filter that must match both input filters.</returns>
73
public static TreeFilter Create(TreeFilter a, TreeFilter b)
83
return new AndTreeFilter.Binary(a, b);
86
/// <summary>Create a filter around many filters, all of which must match.</summary>
87
/// <remarks>Create a filter around many filters, all of which must match.</remarks>
88
/// <param name="list">
89
/// list of filters to match against. Must contain at least 2
92
/// <returns>a filter that must match all input filters.</returns>
93
public static TreeFilter Create(TreeFilter[] list)
97
return Create(list[0], list[1]);
101
throw new ArgumentException(JGitText.Get().atLeastTwoFiltersNeeded);
103
TreeFilter[] subfilters = new TreeFilter[list.Length];
104
System.Array.Copy(list, 0, subfilters, 0, list.Length);
105
return new AndTreeFilter.List(subfilters);
108
/// <summary>Create a filter around many filters, all of which must match.</summary>
109
/// <remarks>Create a filter around many filters, all of which must match.</remarks>
110
/// <param name="list">
111
/// list of filters to match against. Must contain at least 2
114
/// <returns>a filter that must match all input filters.</returns>
115
public static TreeFilter Create(ICollection<TreeFilter> list)
119
throw new ArgumentException(JGitText.Get().atLeastTwoFiltersNeeded);
121
TreeFilter[] subfilters = new TreeFilter[list.Count];
122
Sharpen.Collections.ToArray(list, subfilters);
123
if (subfilters.Length == 2)
125
return Create(subfilters[0], subfilters[1]);
127
return new AndTreeFilter.List(subfilters);
130
private class Binary : AndTreeFilter
132
private readonly TreeFilter a;
134
private readonly TreeFilter b;
136
internal Binary(TreeFilter one, TreeFilter two)
142
/// <exception cref="NGit.Errors.MissingObjectException"></exception>
143
/// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
144
/// <exception cref="System.IO.IOException"></exception>
145
public override bool Include(TreeWalk walker)
147
return a.Include(walker) && b.Include(walker);
150
public override bool ShouldBeRecursive()
152
return a.ShouldBeRecursive() || b.ShouldBeRecursive();
155
public override TreeFilter Clone()
157
return new AndTreeFilter.Binary(a.Clone(), b.Clone());
160
public override string ToString()
162
return "(" + a.ToString() + " AND " + b.ToString() + ")";
166
private class List : AndTreeFilter
168
private readonly TreeFilter[] subfilters;
170
internal List(TreeFilter[] list)
175
/// <exception cref="NGit.Errors.MissingObjectException"></exception>
176
/// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
177
/// <exception cref="System.IO.IOException"></exception>
178
public override bool Include(TreeWalk walker)
180
foreach (TreeFilter f in subfilters)
182
if (!f.Include(walker))
190
public override bool ShouldBeRecursive()
192
foreach (TreeFilter f in subfilters)
194
if (f.ShouldBeRecursive())
202
public override TreeFilter Clone()
204
TreeFilter[] s = new TreeFilter[subfilters.Length];
205
for (int i = 0; i < s.Length; i++)
207
s[i] = subfilters[i].Clone();
209
return new AndTreeFilter.List(s);
212
public override string ToString()
214
StringBuilder r = new StringBuilder();
216
for (int i = 0; i < subfilters.Length; i++)
222
r.Append(subfilters[i].ToString());