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.

OrTreeFilter.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.treewalk.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.treewalk.TreeWalk;
  50. /**
  51. * Includes a tree entry if any subfilters include the same tree entry.
  52. * <p>
  53. * Classic shortcut behavior is used, so evaluation of the
  54. * {@link TreeFilter#include(TreeWalk)} method stops as soon as a true result is
  55. * obtained. Applications can improve filtering performance by placing faster
  56. * filters that are more likely to accept a result earlier in the list.
  57. */
  58. public abstract class OrTreeFilter extends TreeFilter {
  59. /**
  60. * Create a filter with two filters, one of which must match.
  61. *
  62. * @param a
  63. * first filter to test.
  64. * @param b
  65. * second filter to test.
  66. * @return a filter that must match at least one input filter.
  67. */
  68. public static TreeFilter create(final TreeFilter a, final TreeFilter b) {
  69. if (a == ALL || b == ALL)
  70. return ALL;
  71. return new Binary(a, b);
  72. }
  73. /**
  74. * Create a filter around many filters, one of which must match.
  75. *
  76. * @param list
  77. * list of filters to match against. Must contain at least 2
  78. * filters.
  79. * @return a filter that must match at least one input filter.
  80. */
  81. public static TreeFilter create(final TreeFilter[] list) {
  82. if (list.length == 2)
  83. return create(list[0], list[1]);
  84. if (list.length < 2)
  85. throw new IllegalArgumentException("At least two filters needed.");
  86. final TreeFilter[] subfilters = new TreeFilter[list.length];
  87. System.arraycopy(list, 0, subfilters, 0, list.length);
  88. return new List(subfilters);
  89. }
  90. /**
  91. * Create a filter around many filters, one of which must match.
  92. *
  93. * @param list
  94. * list of filters to match against. Must contain at least 2
  95. * filters.
  96. * @return a filter that must match at least one input filter.
  97. */
  98. public static TreeFilter create(final Collection<TreeFilter> list) {
  99. if (list.size() < 2)
  100. throw new IllegalArgumentException("At least two filters needed.");
  101. final TreeFilter[] subfilters = new TreeFilter[list.size()];
  102. list.toArray(subfilters);
  103. if (subfilters.length == 2)
  104. return create(subfilters[0], subfilters[1]);
  105. return new List(subfilters);
  106. }
  107. private static class Binary extends OrTreeFilter {
  108. private final TreeFilter a;
  109. private final TreeFilter b;
  110. Binary(final TreeFilter one, final TreeFilter two) {
  111. a = one;
  112. b = two;
  113. }
  114. @Override
  115. public boolean include(final TreeWalk walker)
  116. throws MissingObjectException, IncorrectObjectTypeException,
  117. IOException {
  118. return a.include(walker) || b.include(walker);
  119. }
  120. @Override
  121. public boolean shouldBeRecursive() {
  122. return a.shouldBeRecursive() || b.shouldBeRecursive();
  123. }
  124. @Override
  125. public TreeFilter clone() {
  126. return new Binary(a.clone(), b.clone());
  127. }
  128. @Override
  129. public String toString() {
  130. return "(" + a.toString() + " OR " + b.toString() + ")";
  131. }
  132. }
  133. private static class List extends OrTreeFilter {
  134. private final TreeFilter[] subfilters;
  135. List(final TreeFilter[] list) {
  136. subfilters = list;
  137. }
  138. @Override
  139. public boolean include(final TreeWalk walker)
  140. throws MissingObjectException, IncorrectObjectTypeException,
  141. IOException {
  142. for (final TreeFilter f : subfilters) {
  143. if (f.include(walker))
  144. return true;
  145. }
  146. return false;
  147. }
  148. @Override
  149. public boolean shouldBeRecursive() {
  150. for (final TreeFilter f : subfilters)
  151. if (f.shouldBeRecursive())
  152. return true;
  153. return false;
  154. }
  155. @Override
  156. public TreeFilter clone() {
  157. final TreeFilter[] s = new TreeFilter[subfilters.length];
  158. for (int i = 0; i < s.length; i++)
  159. s[i] = subfilters[i].clone();
  160. return new List(s);
  161. }
  162. @Override
  163. public String toString() {
  164. final StringBuilder r = new StringBuilder();
  165. r.append("(");
  166. for (int i = 0; i < subfilters.length; i++) {
  167. if (i > 0)
  168. r.append(" OR ");
  169. r.append(subfilters[i].toString());
  170. }
  171. r.append(")");
  172. return r.toString();
  173. }
  174. }
  175. }