Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / revwalk / filter / AndRevFilter.java
blob487cdbd3b0370fb51b064729be0637a3e98cf3e1
1 /*
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
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
22 * written permission.
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;
49 /**
50 * Includes a commit only if all subfilters include the same commit.
51 * <p>
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 {
58 /**
59 * Create a filter with two filters, both of which must match.
61 * @param a
62 * first filter to test.
63 * @param b
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) {
68 if (a == ALL)
69 return b;
70 if (b == ALL)
71 return a;
72 return new Binary(a, b);
75 /**
76 * Create a filter around many filters, all of which must match.
78 * @param list
79 * list of filters to match against. Must contain at least 2
80 * filters.
81 * @return a filter that must match all input filters.
83 public static RevFilter create(final RevFilter[] list) {
84 if (list.length == 2)
85 return create(list[0], list[1]);
86 if (list.length < 2)
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);
93 /**
94 * Create a filter around many filters, all of which must match.
96 * @param list
97 * list of filters to match against. Must contain at least 2
98 * filters.
99 * @return a filter that must match all input filters.
101 public static RevFilter create(final Collection<RevFilter> list) {
102 if (list.size() < 2)
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) {
117 a = one;
118 b = two;
121 @Override
122 public boolean include(final RevWalk walker, final RevCommit c)
123 throws MissingObjectException, IncorrectObjectTypeException,
124 IOException {
125 return a.include(walker, c) && b.include(walker, c);
128 @Override
129 public RevFilter clone() {
130 return new Binary(a.clone(), b.clone());
133 @Override
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) {
143 subfilters = list;
146 @Override
147 public boolean include(final RevWalk walker, final RevCommit c)
148 throws MissingObjectException, IncorrectObjectTypeException,
149 IOException {
150 for (final RevFilter f : subfilters) {
151 if (!f.include(walker, c))
152 return false;
154 return true;
157 @Override
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();
162 return new List(s);
165 @Override
166 public String toString() {
167 final StringBuffer r = new StringBuffer();
168 r.append("(");
169 for (int i = 0; i < subfilters.length; i++) {
170 if (i > 0)
171 r.append(" AND ");
172 r.append(subfilters[i].toString());
174 r.append(")");
175 return r.toString();