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
.revwalk
.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
.revwalk
.RevCommit
;
47 import org
.spearce
.jgit
.revwalk
.RevWalk
;
50 * Includes a commit only if all subfilters include the same commit.
52 * Classic shortcut behavior is used, so evaluation of the
53 * {@link RevFilter#include(RevWalk, RevCommit)} method stops as soon as a false
54 * result is obtained. Applications can improve filtering performance by placing
55 * faster filters that are more likely to reject a result earlier in the list.
57 public abstract class AndRevFilter
extends RevFilter
{
59 * Create a filter with two filters, both of which must match.
62 * first filter to test.
64 * second filter to test.
65 * @return a filter that must match both input filters.
67 public static RevFilter
create(final RevFilter a
, final RevFilter b
) {
72 return new Binary(a
, b
);
76 * Create a filter around many filters, all of which must match.
79 * list of filters to match against. Must contain at least 2
81 * @return a filter that must match all input filters.
83 public static RevFilter
create(final RevFilter
[] list
) {
85 return create(list
[0], list
[1]);
87 throw new IllegalArgumentException("At least two filters needed.");
88 final RevFilter
[] subfilters
= new RevFilter
[list
.length
];
89 System
.arraycopy(list
, 0, subfilters
, 0, list
.length
);
90 return new List(subfilters
);
94 * Create a filter around many filters, all of which must match.
97 * list of filters to match against. Must contain at least 2
99 * @return a filter that must match all input filters.
101 public static RevFilter
create(final Collection
<RevFilter
> list
) {
103 throw new IllegalArgumentException("At least two filters needed.");
104 final RevFilter
[] subfilters
= new RevFilter
[list
.size()];
105 list
.toArray(subfilters
);
106 if (subfilters
.length
== 2)
107 return create(subfilters
[0], subfilters
[1]);
108 return new List(subfilters
);
111 private static class Binary
extends AndRevFilter
{
112 private final RevFilter a
;
114 private final RevFilter b
;
116 Binary(final RevFilter one
, final RevFilter two
) {
122 public boolean include(final RevWalk walker
, final RevCommit c
)
123 throws MissingObjectException
, IncorrectObjectTypeException
,
125 return a
.include(walker
, c
) && b
.include(walker
, c
);
129 public RevFilter
clone() {
130 return new Binary(a
.clone(), b
.clone());
134 public String
toString() {
135 return "(" + a
.toString() + " AND " + b
.toString() + ")";
139 private static class List
extends AndRevFilter
{
140 private final RevFilter
[] subfilters
;
142 List(final RevFilter
[] list
) {
147 public boolean include(final RevWalk walker
, final RevCommit c
)
148 throws MissingObjectException
, IncorrectObjectTypeException
,
150 for (final RevFilter f
: subfilters
) {
151 if (!f
.include(walker
, c
))
158 public RevFilter
clone() {
159 final RevFilter
[] s
= new RevFilter
[subfilters
.length
];
160 for (int i
= 0; i
< s
.length
; i
++)
161 s
[i
] = subfilters
[i
].clone();
166 public String
toString() {
167 final StringBuffer r
= new StringBuffer();
169 for (int i
= 0; i
< subfilters
.length
; i
++) {
172 r
.append(subfilters
[i
].toString());