~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Treewalk.Filter/OrTreeFilter.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
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
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
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.
 
23
 
 
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
 
27
  written permission.
 
28
 
 
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.
 
42
*/
 
43
 
 
44
using System;
 
45
using System.Collections.Generic;
 
46
using System.Text;
 
47
using NGit;
 
48
using NGit.Treewalk;
 
49
using NGit.Treewalk.Filter;
 
50
using Sharpen;
 
51
 
 
52
namespace NGit.Treewalk.Filter
 
53
{
 
54
        /// <summary>Includes a tree entry if any subfilters include the same tree entry.</summary>
 
55
        /// <remarks>
 
56
        /// Includes a tree entry if any subfilters include the same tree entry.
 
57
        /// <p>
 
58
        /// Classic shortcut behavior is used, so evaluation of the
 
59
        /// <see cref="TreeFilter.Include(NGit.Treewalk.TreeWalk)">TreeFilter.Include(NGit.Treewalk.TreeWalk)
 
60
        ///     </see>
 
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.
 
64
        /// </remarks>
 
65
        public abstract class OrTreeFilter : TreeFilter
 
66
        {
 
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)
 
73
                {
 
74
                        if (a == ALL || b == ALL)
 
75
                        {
 
76
                                return ALL;
 
77
                        }
 
78
                        return new OrTreeFilter.Binary(a, b);
 
79
                }
 
80
 
 
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
 
85
                /// filters.
 
86
                /// </param>
 
87
                /// <returns>a filter that must match at least one input filter.</returns>
 
88
                public static TreeFilter Create(TreeFilter[] list)
 
89
                {
 
90
                        if (list.Length == 2)
 
91
                        {
 
92
                                return Create(list[0], list[1]);
 
93
                        }
 
94
                        if (list.Length < 2)
 
95
                        {
 
96
                                throw new ArgumentException(JGitText.Get().atLeastTwoFiltersNeeded);
 
97
                        }
 
98
                        TreeFilter[] subfilters = new TreeFilter[list.Length];
 
99
                        System.Array.Copy(list, 0, subfilters, 0, list.Length);
 
100
                        return new OrTreeFilter.List(subfilters);
 
101
                }
 
102
 
 
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
 
107
                /// filters.
 
108
                /// </param>
 
109
                /// <returns>a filter that must match at least one input filter.</returns>
 
110
                public static TreeFilter Create(ICollection<TreeFilter> list)
 
111
                {
 
112
                        if (list.Count < 2)
 
113
                        {
 
114
                                throw new ArgumentException(JGitText.Get().atLeastTwoFiltersNeeded);
 
115
                        }
 
116
                        TreeFilter[] subfilters = new TreeFilter[list.Count];
 
117
                        Sharpen.Collections.ToArray(list, subfilters);
 
118
                        if (subfilters.Length == 2)
 
119
                        {
 
120
                                return Create(subfilters[0], subfilters[1]);
 
121
                        }
 
122
                        return new OrTreeFilter.List(subfilters);
 
123
                }
 
124
 
 
125
                private class Binary : OrTreeFilter
 
126
                {
 
127
                        private readonly TreeFilter a;
 
128
 
 
129
                        private readonly TreeFilter b;
 
130
 
 
131
                        internal Binary(TreeFilter one, TreeFilter two)
 
132
                        {
 
133
                                a = one;
 
134
                                b = two;
 
135
                        }
 
136
 
 
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)
 
141
                        {
 
142
                                return a.Include(walker) || b.Include(walker);
 
143
                        }
 
144
 
 
145
                        public override bool ShouldBeRecursive()
 
146
                        {
 
147
                                return a.ShouldBeRecursive() || b.ShouldBeRecursive();
 
148
                        }
 
149
 
 
150
                        public override TreeFilter Clone()
 
151
                        {
 
152
                                return new OrTreeFilter.Binary(a.Clone(), b.Clone());
 
153
                        }
 
154
 
 
155
                        public override string ToString()
 
156
                        {
 
157
                                return "(" + a.ToString() + " OR " + b.ToString() + ")";
 
158
                        }
 
159
                }
 
160
 
 
161
                private class List : OrTreeFilter
 
162
                {
 
163
                        private readonly TreeFilter[] subfilters;
 
164
 
 
165
                        internal List(TreeFilter[] list)
 
166
                        {
 
167
                                subfilters = list;
 
168
                        }
 
169
 
 
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)
 
174
                        {
 
175
                                foreach (TreeFilter f in subfilters)
 
176
                                {
 
177
                                        if (f.Include(walker))
 
178
                                        {
 
179
                                                return true;
 
180
                                        }
 
181
                                }
 
182
                                return false;
 
183
                        }
 
184
 
 
185
                        public override bool ShouldBeRecursive()
 
186
                        {
 
187
                                foreach (TreeFilter f in subfilters)
 
188
                                {
 
189
                                        if (f.ShouldBeRecursive())
 
190
                                        {
 
191
                                                return true;
 
192
                                        }
 
193
                                }
 
194
                                return false;
 
195
                        }
 
196
 
 
197
                        public override TreeFilter Clone()
 
198
                        {
 
199
                                TreeFilter[] s = new TreeFilter[subfilters.Length];
 
200
                                for (int i = 0; i < s.Length; i++)
 
201
                                {
 
202
                                        s[i] = subfilters[i].Clone();
 
203
                                }
 
204
                                return new OrTreeFilter.List(s);
 
205
                        }
 
206
 
 
207
                        public override string ToString()
 
208
                        {
 
209
                                StringBuilder r = new StringBuilder();
 
210
                                r.Append("(");
 
211
                                for (int i = 0; i < subfilters.Length; i++)
 
212
                                {
 
213
                                        if (i > 0)
 
214
                                        {
 
215
                                                r.Append(" OR ");
 
216
                                        }
 
217
                                        r.Append(subfilters[i].ToString());
 
218
                                }
 
219
                                r.Append(")");
 
220
                                return r.ToString();
 
221
                        }
 
222
                }
 
223
        }
 
224
}