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.

RevFlagFilter.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.revwalk.filter;
  44. import java.io.IOException;
  45. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  46. import org.eclipse.jgit.errors.MissingObjectException;
  47. import org.eclipse.jgit.revwalk.RevCommit;
  48. import org.eclipse.jgit.revwalk.RevFlag;
  49. import org.eclipse.jgit.revwalk.RevFlagSet;
  50. import org.eclipse.jgit.revwalk.RevWalk;
  51. /** Matches only commits with some/all RevFlags already set. */
  52. public abstract class RevFlagFilter extends RevFilter {
  53. /**
  54. * Create a new filter that tests for a single flag.
  55. *
  56. * @param a
  57. * the flag to test.
  58. * @return filter that selects only commits with flag <code>a</code>.
  59. */
  60. public static RevFilter has(final RevFlag a) {
  61. final RevFlagSet s = new RevFlagSet();
  62. s.add(a);
  63. return new HasAll(s);
  64. }
  65. /**
  66. * Create a new filter that tests all flags in a set.
  67. *
  68. * @param a
  69. * set of flags to test.
  70. * @return filter that selects only commits with all flags in <code>a</code>.
  71. */
  72. public static RevFilter hasAll(final RevFlag... a) {
  73. final RevFlagSet set = new RevFlagSet();
  74. for (final RevFlag flag : a)
  75. set.add(flag);
  76. return new HasAll(set);
  77. }
  78. /**
  79. * Create a new filter that tests all flags in a set.
  80. *
  81. * @param a
  82. * set of flags to test.
  83. * @return filter that selects only commits with all flags in <code>a</code>.
  84. */
  85. public static RevFilter hasAll(final RevFlagSet a) {
  86. return new HasAll(new RevFlagSet(a));
  87. }
  88. /**
  89. * Create a new filter that tests for any flag in a set.
  90. *
  91. * @param a
  92. * set of flags to test.
  93. * @return filter that selects only commits with any flag in <code>a</code>.
  94. */
  95. public static RevFilter hasAny(final RevFlag... a) {
  96. final RevFlagSet set = new RevFlagSet();
  97. for (final RevFlag flag : a)
  98. set.add(flag);
  99. return new HasAny(set);
  100. }
  101. /**
  102. * Create a new filter that tests for any flag in a set.
  103. *
  104. * @param a
  105. * set of flags to test.
  106. * @return filter that selects only commits with any flag in <code>a</code>.
  107. */
  108. public static RevFilter hasAny(final RevFlagSet a) {
  109. return new HasAny(new RevFlagSet(a));
  110. }
  111. final RevFlagSet flags;
  112. RevFlagFilter(final RevFlagSet m) {
  113. flags = m;
  114. }
  115. @Override
  116. public RevFilter clone() {
  117. return this;
  118. }
  119. @Override
  120. public String toString() {
  121. return super.toString() + flags;
  122. }
  123. private static class HasAll extends RevFlagFilter {
  124. HasAll(final RevFlagSet m) {
  125. super(m);
  126. }
  127. @Override
  128. public boolean include(final RevWalk walker, final RevCommit c)
  129. throws MissingObjectException, IncorrectObjectTypeException,
  130. IOException {
  131. return c.hasAll(flags);
  132. }
  133. @Override
  134. public boolean requiresCommitBody() {
  135. return false;
  136. }
  137. }
  138. private static class HasAny extends RevFlagFilter {
  139. HasAny(final RevFlagSet m) {
  140. super(m);
  141. }
  142. @Override
  143. public boolean include(final RevWalk walker, final RevCommit c)
  144. throws MissingObjectException, IncorrectObjectTypeException,
  145. IOException {
  146. return c.hasAny(flags);
  147. }
  148. @Override
  149. public boolean requiresCommitBody() {
  150. return false;
  151. }
  152. }
  153. }