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 if any subfilters include the same tree entry.</summary>
56
/// Includes a tree entry if any subfilters include the same tree entry.
58
/// Classic shortcut behavior is used, so evaluation of the
59
/// <see cref="TreeFilter.Include(NGit.Treewalk.TreeWalk)">TreeFilter.Include(NGit.Treewalk.TreeWalk)
61
/// method stops as soon as a true result is
62
/// obtained. Applications can improve filtering performance by placing faster
63
/// filters that are more likely to accept a result earlier in the list.
65
public abstract class OrTreeFilter : TreeFilter
67
/// <summary>Create a filter with two filters, one of which must match.</summary>
68
/// <remarks>Create a filter with two filters, one of which must match.</remarks>
69
/// <param name="a">first filter to test.</param>
70
/// <param name="b">second filter to test.</param>
71
/// <returns>a filter that must match at least one input filter.</returns>
72
public static TreeFilter Create(TreeFilter a, TreeFilter b)
74
if (a == ALL || b == ALL)
78
return new OrTreeFilter.Binary(a, b);
81
/// <summary>Create a filter around many filters, one of which must match.</summary>
82
/// <remarks>Create a filter around many filters, one of which must match.</remarks>
83
/// <param name="list">
84
/// list of filters to match against. Must contain at least 2
87
/// <returns>a filter that must match at least one input filter.</returns>
88
public static TreeFilter Create(TreeFilter[] list)
92
return Create(list[0], list[1]);
96
throw new ArgumentException(JGitText.Get().atLeastTwoFiltersNeeded);
98
TreeFilter[] subfilters = new TreeFilter[list.Length];
99
System.Array.Copy(list, 0, subfilters, 0, list.Length);
100
return new OrTreeFilter.List(subfilters);
103
/// <summary>Create a filter around many filters, one of which must match.</summary>
104
/// <remarks>Create a filter around many filters, one of which must match.</remarks>
105
/// <param name="list">
106
/// list of filters to match against. Must contain at least 2
109
/// <returns>a filter that must match at least one input filter.</returns>
110
public static TreeFilter Create(ICollection<TreeFilter> list)
114
throw new ArgumentException(JGitText.Get().atLeastTwoFiltersNeeded);
116
TreeFilter[] subfilters = new TreeFilter[list.Count];
117
Sharpen.Collections.ToArray(list, subfilters);
118
if (subfilters.Length == 2)
120
return Create(subfilters[0], subfilters[1]);
122
return new OrTreeFilter.List(subfilters);
125
private class Binary : OrTreeFilter
127
private readonly TreeFilter a;
129
private readonly TreeFilter b;
131
internal Binary(TreeFilter one, TreeFilter two)
137
/// <exception cref="NGit.Errors.MissingObjectException"></exception>
138
/// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
139
/// <exception cref="System.IO.IOException"></exception>
140
public override bool Include(TreeWalk walker)
142
return a.Include(walker) || b.Include(walker);
145
public override bool ShouldBeRecursive()
147
return a.ShouldBeRecursive() || b.ShouldBeRecursive();
150
public override TreeFilter Clone()
152
return new OrTreeFilter.Binary(a.Clone(), b.Clone());
155
public override string ToString()
157
return "(" + a.ToString() + " OR " + b.ToString() + ")";
161
private class List : OrTreeFilter
163
private readonly TreeFilter[] subfilters;
165
internal List(TreeFilter[] list)
170
/// <exception cref="NGit.Errors.MissingObjectException"></exception>
171
/// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
172
/// <exception cref="System.IO.IOException"></exception>
173
public override bool Include(TreeWalk walker)
175
foreach (TreeFilter f in subfilters)
177
if (f.Include(walker))
185
public override bool ShouldBeRecursive()
187
foreach (TreeFilter f in subfilters)
189
if (f.ShouldBeRecursive())
197
public override TreeFilter Clone()
199
TreeFilter[] s = new TreeFilter[subfilters.Length];
200
for (int i = 0; i < s.Length; i++)
202
s[i] = subfilters[i].Clone();
204
return new OrTreeFilter.List(s);
207
public override string ToString()
209
StringBuilder r = new StringBuilder();
211
for (int i = 0; i < subfilters.Length; i++)
217
r.Append(subfilters[i].ToString());