AbstractRevQueue.java

  1. /*
  2.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.revwalk;

  11. abstract class AbstractRevQueue extends Generator {
  12.     static final AbstractRevQueue EMPTY_QUEUE = new AlwaysEmptyQueue();

  13.     /** Current output flags set for this generator instance. */
  14.     int outputType;

  15.     AbstractRevQueue(boolean firstParent) {
  16.         super(firstParent);
  17.     }

  18.     /**
  19.      * Add a commit to the queue.
  20.      * <p>
  21.      * This method always adds the commit, even if it is already in the queue or
  22.      * previously was in the queue but has already been removed. To control
  23.      * queue admission use {@link #add(RevCommit, RevFlag)}.
  24.      *
  25.      * @param c
  26.      *            commit to add.
  27.      */
  28.     public abstract void add(RevCommit c);

  29.     /**
  30.      * Add a commit if it does not have a flag set yet, then set the flag.
  31.      * <p>
  32.      * This method permits the application to test if the commit has the given
  33.      * flag; if it does not already have the flag than the commit is added to
  34.      * the queue and the flag is set. This later will prevent the commit from
  35.      * being added twice.
  36.      *
  37.      * @param c
  38.      *            commit to add.
  39.      * @param queueControl
  40.      *            flag that controls admission to the queue.
  41.      */
  42.     public final void add(RevCommit c, RevFlag queueControl) {
  43.         if (!c.has(queueControl)) {
  44.             c.add(queueControl);
  45.             add(c);
  46.         }
  47.     }

  48.     /**
  49.      * Add a commit's parents if one does not have a flag set yet.
  50.      * <p>
  51.      * This method permits the application to test if the commit has the given
  52.      * flag; if it does not already have the flag than the commit is added to
  53.      * the queue and the flag is set. This later will prevent the commit from
  54.      * being added twice.
  55.      *
  56.      * @param c
  57.      *            commit whose parents should be added.
  58.      * @param queueControl
  59.      *            flag that controls admission to the queue.
  60.      */
  61.     public final void addParents(RevCommit c, RevFlag queueControl) {
  62.         final RevCommit[] pList = c.parents;
  63.         if (pList == null) {
  64.             return;
  65.         }
  66.         for (int i = 0; i < pList.length; i++) {
  67.             if (firstParent && i > 0) {
  68.                 break;
  69.             }
  70.             add(pList[i], queueControl);
  71.         }
  72.     }

  73.     /**
  74.      * {@inheritDoc}
  75.      * <p>
  76.      * Remove the first commit from the queue.
  77.      */
  78.     @Override
  79.     public abstract RevCommit next();

  80.     /**
  81.      * Remove all entries from this queue.
  82.      */
  83.     public abstract void clear();

  84.     abstract boolean everbodyHasFlag(int f);

  85.     abstract boolean anybodyHasFlag(int f);

  86.     @Override
  87.     int outputType() {
  88.         return outputType;
  89.     }

  90.     /**
  91.      * Describe this queue
  92.      *
  93.      * @param s
  94.      *            a StringBuilder
  95.      * @param c
  96.      *            a {@link org.eclipse.jgit.revwalk.RevCommit}
  97.      */
  98.     protected static void describe(StringBuilder s, RevCommit c) {
  99.         s.append(c.toString());
  100.         s.append('\n');
  101.     }

  102.     private static class AlwaysEmptyQueue extends AbstractRevQueue {
  103.         private AlwaysEmptyQueue() {
  104.             super(false);
  105.         }

  106.         @Override
  107.         public void add(RevCommit c) {
  108.             throw new UnsupportedOperationException();
  109.         }

  110.         @Override
  111.         public RevCommit next() {
  112.             return null;
  113.         }

  114.         @Override
  115.         boolean anybodyHasFlag(int f) {
  116.             return false;
  117.         }

  118.         @Override
  119.         boolean everbodyHasFlag(int f) {
  120.             return true;
  121.         }

  122.         @Override
  123.         public void clear() {
  124.             // Nothing to clear, we have no state.
  125.         }

  126.     }
  127. }