FIFORevQueue.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. import java.io.IOException;

  12. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  13. import org.eclipse.jgit.errors.MissingObjectException;

  14. /**
  15.  * A queue of commits in FIFO order.
  16.  */
  17. public class FIFORevQueue extends BlockRevQueue {
  18.     private Block head;

  19.     private Block tail;

  20.     /** Create an empty FIFO queue. */
  21.     public FIFORevQueue() {
  22.         super(false);
  23.     }

  24.     FIFORevQueue(boolean firstParent) {
  25.         super(firstParent);
  26.     }

  27.     FIFORevQueue(Generator s) throws MissingObjectException,
  28.             IncorrectObjectTypeException, IOException {
  29.         super(s);
  30.     }

  31.     /** {@inheritDoc} */
  32.     @Override
  33.     public void add(RevCommit c) {
  34.         Block b = tail;
  35.         if (b == null) {
  36.             b = free.newBlock();
  37.             b.add(c);
  38.             head = b;
  39.             tail = b;
  40.             return;
  41.         } else if (b.isFull()) {
  42.             b = free.newBlock();
  43.             tail.next = b;
  44.             tail = b;
  45.         }
  46.         b.add(c);
  47.     }

  48.     /**
  49.      * Insert the commit pointer at the front of the queue.
  50.      *
  51.      * @param c
  52.      *            the commit to insert into the queue.
  53.      */
  54.     public void unpop(RevCommit c) {
  55.         Block b = head;
  56.         if (b == null) {
  57.             b = free.newBlock();
  58.             b.resetToMiddle();
  59.             b.add(c);
  60.             head = b;
  61.             tail = b;
  62.             return;
  63.         } else if (b.canUnpop()) {
  64.             b.unpop(c);
  65.             return;
  66.         }

  67.         b = free.newBlock();
  68.         b.resetToEnd();
  69.         b.unpop(c);
  70.         b.next = head;
  71.         head = b;
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public RevCommit next() {
  76.         final Block b = head;
  77.         if (b == null)
  78.             return null;

  79.         final RevCommit c = b.pop();
  80.         if (b.isEmpty()) {
  81.             head = b.next;
  82.             if (head == null)
  83.                 tail = null;
  84.             free.freeBlock(b);
  85.         }
  86.         return c;
  87.     }

  88.     /** {@inheritDoc} */
  89.     @Override
  90.     public void clear() {
  91.         head = null;
  92.         tail = null;
  93.         free.clear();
  94.     }

  95.     @Override
  96.     boolean everbodyHasFlag(int f) {
  97.         for (Block b = head; b != null; b = b.next) {
  98.             for (int i = b.headIndex; i < b.tailIndex; i++)
  99.                 if ((b.commits[i].flags & f) == 0)
  100.                     return false;
  101.         }
  102.         return true;
  103.     }

  104.     @Override
  105.     boolean anybodyHasFlag(int f) {
  106.         for (Block b = head; b != null; b = b.next) {
  107.             for (int i = b.headIndex; i < b.tailIndex; i++)
  108.                 if ((b.commits[i].flags & f) != 0)
  109.                     return true;
  110.         }
  111.         return false;
  112.     }

  113.     void removeFlag(int f) {
  114.         final int not_f = ~f;
  115.         for (Block b = head; b != null; b = b.next) {
  116.             for (int i = b.headIndex; i < b.tailIndex; i++)
  117.                 b.commits[i].flags &= not_f;
  118.         }
  119.     }

  120.     /** {@inheritDoc} */
  121.     @Override
  122.     public String toString() {
  123.         final StringBuilder s = new StringBuilder();
  124.         for (Block q = head; q != null; q = q.next) {
  125.             for (int i = q.headIndex; i < q.tailIndex; i++)
  126.                 describe(s, q.commits[i]);
  127.         }
  128.         return s.toString();
  129.     }
  130. }