You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OrRevFilter.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.revwalk.filter;
  45. import java.io.IOException;
  46. import java.util.Collection;
  47. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  48. import org.eclipse.jgit.errors.MissingObjectException;
  49. import org.eclipse.jgit.internal.JGitText;
  50. import org.eclipse.jgit.revwalk.RevCommit;
  51. import org.eclipse.jgit.revwalk.RevWalk;
  52. /**
  53. * Includes a commit if any subfilters include the same commit.
  54. * <p>
  55. * Classic shortcut behavior is used, so evaluation of the
  56. * {@link org.eclipse.jgit.revwalk.filter.RevFilter#include(RevWalk, RevCommit)}
  57. * method stops as soon as a true result is obtained. Applications can improve
  58. * filtering performance by placing faster filters that are more likely to
  59. * accept a result earlier in the list.
  60. */
  61. public abstract class OrRevFilter extends RevFilter {
  62. /**
  63. * Create a filter with two filters, one of which must match.
  64. *
  65. * @param a
  66. * first filter to test.
  67. * @param b
  68. * second filter to test.
  69. * @return a filter that must match at least one input filter.
  70. */
  71. public static RevFilter create(final RevFilter a, final RevFilter b) {
  72. if (a == ALL || b == ALL)
  73. return ALL;
  74. return new Binary(a, b);
  75. }
  76. /**
  77. * Create a filter around many filters, one of which must match.
  78. *
  79. * @param list
  80. * list of filters to match against. Must contain at least 2
  81. * filters.
  82. * @return a filter that must match at least one input filter.
  83. */
  84. public static RevFilter create(final RevFilter[] list) {
  85. if (list.length == 2)
  86. return create(list[0], list[1]);
  87. if (list.length < 2)
  88. throw new IllegalArgumentException(JGitText.get().atLeastTwoFiltersNeeded);
  89. final RevFilter[] subfilters = new RevFilter[list.length];
  90. System.arraycopy(list, 0, subfilters, 0, list.length);
  91. return new List(subfilters);
  92. }
  93. /**
  94. * Create a filter around many filters, one of which must match.
  95. *
  96. * @param list
  97. * list of filters to match against. Must contain at least 2
  98. * filters.
  99. * @return a filter that must match at least one input filter.
  100. */
  101. public static RevFilter create(final Collection<RevFilter> list) {
  102. if (list.size() < 2)
  103. throw new IllegalArgumentException(JGitText.get().atLeastTwoFiltersNeeded);
  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);
  109. }
  110. private static class Binary extends OrRevFilter {
  111. private final RevFilter a;
  112. private final RevFilter b;
  113. private final boolean requiresCommitBody;
  114. Binary(final RevFilter one, final RevFilter two) {
  115. a = one;
  116. b = two;
  117. requiresCommitBody = a.requiresCommitBody()
  118. || b.requiresCommitBody();
  119. }
  120. @Override
  121. public boolean include(final RevWalk walker, final RevCommit c)
  122. throws MissingObjectException, IncorrectObjectTypeException,
  123. IOException {
  124. return a.include(walker, c) || b.include(walker, c);
  125. }
  126. @Override
  127. public boolean requiresCommitBody() {
  128. return requiresCommitBody;
  129. }
  130. @Override
  131. public RevFilter clone() {
  132. return new Binary(a.clone(), b.clone());
  133. }
  134. @SuppressWarnings("nls")
  135. @Override
  136. public String toString() {
  137. return "(" + a.toString() + " OR " + b.toString() + ")";
  138. }
  139. }
  140. private static class List extends OrRevFilter {
  141. private final RevFilter[] subfilters;
  142. private final boolean requiresCommitBody;
  143. List(final RevFilter[] list) {
  144. subfilters = list;
  145. boolean rcb = false;
  146. for (RevFilter filter : subfilters)
  147. rcb |= filter.requiresCommitBody();
  148. requiresCommitBody = rcb;
  149. }
  150. @Override
  151. public boolean include(final RevWalk walker, final RevCommit c)
  152. throws MissingObjectException, IncorrectObjectTypeException,
  153. IOException {
  154. for (final RevFilter f : subfilters) {
  155. if (f.include(walker, c))
  156. return true;
  157. }
  158. return false;
  159. }
  160. @Override
  161. public boolean requiresCommitBody() {
  162. return requiresCommitBody;
  163. }
  164. @Override
  165. public RevFilter clone() {
  166. final RevFilter[] s = new RevFilter[subfilters.length];
  167. for (int i = 0; i < s.length; i++)
  168. s[i] = subfilters[i].clone();
  169. return new List(s);
  170. }
  171. @Override
  172. public String toString() {
  173. final StringBuilder r = new StringBuilder();
  174. r.append("("); //$NON-NLS-1$
  175. for (int i = 0; i < subfilters.length; i++) {
  176. if (i > 0)
  177. r.append(" OR "); //$NON-NLS-1$
  178. r.append(subfilters[i].toString());
  179. }
  180. r.append(")"); //$NON-NLS-1$
  181. return r.toString();
  182. }
  183. }
  184. }