PlotCommit.java

  1. /*
  2.  * Copyright (C) 2008, 2014 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.revplot;

  11. import org.eclipse.jgit.lib.AnyObjectId;
  12. import org.eclipse.jgit.lib.Ref;
  13. import org.eclipse.jgit.revwalk.RevCommit;

  14. /**
  15.  * A commit reference to a commit in the DAG.
  16.  *
  17.  * @param <L>
  18.  *            type of lane being used by the plotter.
  19.  * @see PlotCommitList
  20.  */
  21. public class PlotCommit<L extends PlotLane> extends RevCommit {
  22.     static final PlotCommit[] NO_CHILDREN = {};

  23.     static final PlotLane[] NO_LANES = {};

  24.     static final Ref[] NO_REFS = {};

  25.     PlotLane[] forkingOffLanes;

  26.     PlotLane[] passingLanes;

  27.     PlotLane[] mergingLanes;

  28.     PlotLane lane;

  29.     PlotCommit[] children;

  30.     Ref[] refs;

  31.     /**
  32.      * Create a new commit.
  33.      *
  34.      * @param id
  35.      *            the identity of this commit.
  36.      */
  37.     protected PlotCommit(AnyObjectId id) {
  38.         super(id);
  39.         forkingOffLanes = NO_LANES;
  40.         passingLanes = NO_LANES;
  41.         mergingLanes = NO_LANES;
  42.         children = NO_CHILDREN;
  43.         refs = NO_REFS;
  44.     }

  45.     void addForkingOffLane(PlotLane f) {
  46.         forkingOffLanes = addLane(f, forkingOffLanes);
  47.     }

  48.     void addPassingLane(PlotLane c) {
  49.         passingLanes = addLane(c, passingLanes);
  50.     }

  51.     void addMergingLane(PlotLane m) {
  52.         mergingLanes = addLane(m, mergingLanes);
  53.     }

  54.     private static PlotLane[] addLane(PlotLane l, PlotLane[] lanes) {
  55.         final int cnt = lanes.length;
  56.         switch (cnt) {
  57.         case 0:
  58.             lanes = new PlotLane[] { l };
  59.             break;
  60.         case 1:
  61.             lanes = new PlotLane[] { lanes[0], l };
  62.             break;
  63.         default:
  64.             final PlotLane[] n = new PlotLane[cnt + 1];
  65.             System.arraycopy(lanes, 0, n, 0, cnt);
  66.             n[cnt] = l;
  67.             lanes = n;
  68.             break;
  69.         }
  70.         return lanes;
  71.     }

  72.     void addChild(PlotCommit c) {
  73.         final int cnt = children.length;
  74.         switch (cnt) {
  75.         case 0:
  76.             children = new PlotCommit[] { c };
  77.             break;
  78.         case 1:
  79.             if (!c.getId().equals(children[0].getId()))
  80.                 children = new PlotCommit[] { children[0], c };
  81.             break;
  82.         default:
  83.             for (PlotCommit pc : children)
  84.                 if (c.getId().equals(pc.getId()))
  85.                     return;
  86.             final PlotCommit[] n = new PlotCommit[cnt + 1];
  87.             System.arraycopy(children, 0, n, 0, cnt);
  88.             n[cnt] = c;
  89.             children = n;
  90.             break;
  91.         }
  92.     }

  93.     /**
  94.      * Get the number of child commits listed in this commit.
  95.      *
  96.      * @return number of children; always a positive value but can be 0.
  97.      */
  98.     public final int getChildCount() {
  99.         return children.length;
  100.     }

  101.     /**
  102.      * Get the nth child from this commit's child list.
  103.      *
  104.      * @param nth
  105.      *            child index to obtain. Must be in the range 0 through
  106.      *            {@link #getChildCount()}-1.
  107.      * @return the specified child.
  108.      * @throws java.lang.ArrayIndexOutOfBoundsException
  109.      *             an invalid child index was specified.
  110.      */
  111.     public final PlotCommit getChild(int nth) {
  112.         return children[nth];
  113.     }

  114.     /**
  115.      * Determine if the given commit is a child (descendant) of this commit.
  116.      *
  117.      * @param c
  118.      *            the commit to test.
  119.      * @return true if the given commit built on top of this commit.
  120.      */
  121.     @SuppressWarnings("ReferenceEquality")
  122.     public final boolean isChild(PlotCommit c) {
  123.         for (PlotCommit a : children)
  124.             if (a == c)
  125.                 return true;
  126.         return false;
  127.     }

  128.     /**
  129.      * Get the number of refs for this commit.
  130.      *
  131.      * @return number of refs; always a positive value but can be 0.
  132.      */
  133.     public final int getRefCount() {
  134.         return refs.length;
  135.     }

  136.     /**
  137.      * Get the nth Ref from this commit's ref list.
  138.      *
  139.      * @param nth
  140.      *            ref index to obtain. Must be in the range 0 through
  141.      *            {@link #getRefCount()}-1.
  142.      * @return the specified ref.
  143.      * @throws java.lang.ArrayIndexOutOfBoundsException
  144.      *             an invalid ref index was specified.
  145.      */
  146.     public final Ref getRef(int nth) {
  147.         return refs[nth];
  148.     }

  149.     /**
  150.      * Obtain the lane this commit has been plotted into.
  151.      *
  152.      * @return the assigned lane for this commit.
  153.      */
  154.     @SuppressWarnings("unchecked")
  155.     public final L getLane() {
  156.         return (L) lane;
  157.     }

  158.     /** {@inheritDoc} */
  159.     @Override
  160.     public void reset() {
  161.         forkingOffLanes = NO_LANES;
  162.         passingLanes = NO_LANES;
  163.         mergingLanes = NO_LANES;
  164.         children = NO_CHILDREN;
  165.         lane = null;
  166.         super.reset();
  167.     }
  168. }