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

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Treewalk.Filter/AndTreeFilter.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 only if all subfilters include the same tree entry.
 
55
        ///     </summary>
 
56
        /// <remarks>
 
57
        /// Includes a tree entry only if all subfilters include the same tree entry.
 
58
        /// <p>
 
59
        /// Classic shortcut behavior is used, so evaluation of the
 
60
        /// <see cref="TreeFilter.Include(NGit.Treewalk.TreeWalk)">TreeFilter.Include(NGit.Treewalk.TreeWalk)
 
61
        ///     </see>
 
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.
 
65
        /// </remarks>
 
66
        public abstract class AndTreeFilter : TreeFilter
 
67
        {
 
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)
 
74
                {
 
75
                        if (a == ALL)
 
76
                        {
 
77
                                return b;
 
78
                        }
 
79
                        if (b == ALL)
 
80
                        {
 
81
                                return a;
 
82
                        }
 
83
                        return new AndTreeFilter.Binary(a, b);
 
84
                }
 
85
 
 
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
 
90
                /// filters.
 
91
                /// </param>
 
92
                /// <returns>a filter that must match all input filters.</returns>
 
93
                public static TreeFilter Create(TreeFilter[] list)
 
94
                {
 
95
                        if (list.Length == 2)
 
96
                        {
 
97
                                return Create(list[0], list[1]);
 
98
                        }
 
99
                        if (list.Length < 2)
 
100
                        {
 
101
                                throw new ArgumentException(JGitText.Get().atLeastTwoFiltersNeeded);
 
102
                        }
 
103
                        TreeFilter[] subfilters = new TreeFilter[list.Length];
 
104
                        System.Array.Copy(list, 0, subfilters, 0, list.Length);
 
105
                        return new AndTreeFilter.List(subfilters);
 
106
                }
 
107
 
 
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
 
112
                /// filters.
 
113
                /// </param>
 
114
                /// <returns>a filter that must match all input filters.</returns>
 
115
                public static TreeFilter Create(ICollection<TreeFilter> list)
 
116
                {
 
117
                        if (list.Count < 2)
 
118
                        {
 
119
                                throw new ArgumentException(JGitText.Get().atLeastTwoFiltersNeeded);
 
120
                        }
 
121
                        TreeFilter[] subfilters = new TreeFilter[list.Count];
 
122
                        Sharpen.Collections.ToArray(list, subfilters);
 
123
                        if (subfilters.Length == 2)
 
124
                        {
 
125
                                return Create(subfilters[0], subfilters[1]);
 
126
                        }
 
127
                        return new AndTreeFilter.List(subfilters);
 
128
                }
 
129
 
 
130
                private class Binary : AndTreeFilter
 
131
                {
 
132
                        private readonly TreeFilter a;
 
133
 
 
134
                        private readonly TreeFilter b;
 
135
 
 
136
                        internal Binary(TreeFilter one, TreeFilter two)
 
137
                        {
 
138
                                a = one;
 
139
                                b = two;
 
140
                        }
 
141
 
 
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)
 
146
                        {
 
147
                                return a.Include(walker) && b.Include(walker);
 
148
                        }
 
149
 
 
150
                        public override bool ShouldBeRecursive()
 
151
                        {
 
152
                                return a.ShouldBeRecursive() || b.ShouldBeRecursive();
 
153
                        }
 
154
 
 
155
                        public override TreeFilter Clone()
 
156
                        {
 
157
                                return new AndTreeFilter.Binary(a.Clone(), b.Clone());
 
158
                        }
 
159
 
 
160
                        public override string ToString()
 
161
                        {
 
162
                                return "(" + a.ToString() + " AND " + b.ToString() + ")";
 
163
                        }
 
164
                }
 
165
 
 
166
                private class List : AndTreeFilter
 
167
                {
 
168
                        private readonly TreeFilter[] subfilters;
 
169
 
 
170
                        internal List(TreeFilter[] list)
 
171
                        {
 
172
                                subfilters = list;
 
173
                        }
 
174
 
 
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)
 
179
                        {
 
180
                                foreach (TreeFilter f in subfilters)
 
181
                                {
 
182
                                        if (!f.Include(walker))
 
183
                                        {
 
184
                                                return false;
 
185
                                        }
 
186
                                }
 
187
                                return true;
 
188
                        }
 
189
 
 
190
                        public override bool ShouldBeRecursive()
 
191
                        {
 
192
                                foreach (TreeFilter f in subfilters)
 
193
                                {
 
194
                                        if (f.ShouldBeRecursive())
 
195
                                        {
 
196
                                                return true;
 
197
                                        }
 
198
                                }
 
199
                                return false;
 
200
                        }
 
201
 
 
202
                        public override TreeFilter Clone()
 
203
                        {
 
204
                                TreeFilter[] s = new TreeFilter[subfilters.Length];
 
205
                                for (int i = 0; i < s.Length; i++)
 
206
                                {
 
207
                                        s[i] = subfilters[i].Clone();
 
208
                                }
 
209
                                return new AndTreeFilter.List(s);
 
210
                        }
 
211
 
 
212
                        public override string ToString()
 
213
                        {
 
214
                                StringBuilder r = new StringBuilder();
 
215
                                r.Append("(");
 
216
                                for (int i = 0; i < subfilters.Length; i++)
 
217
                                {
 
218
                                        if (i > 0)
 
219
                                        {
 
220
                                                r.Append(" AND ");
 
221
                                        }
 
222
                                        r.Append(subfilters[i].ToString());
 
223
                                }
 
224
                                r.Append(")");
 
225
                                return r.ToString();
 
226
                        }
 
227
                }
 
228
        }
 
229
}