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.

AndTreeFilter.java 6.7KB

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