PlotCommit.java

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

  44. import org.eclipse.jgit.lib.AnyObjectId;
  45. import org.eclipse.jgit.lib.Ref;
  46. import org.eclipse.jgit.revwalk.RevCommit;

  47. /**
  48.  * A commit reference to a commit in the DAG.
  49.  *
  50.  * @param <L>
  51.  *            type of lane being used by the plotter.
  52.  * @see PlotCommitList
  53.  */
  54. public class PlotCommit<L extends PlotLane> extends RevCommit {
  55.     static final PlotCommit[] NO_CHILDREN = {};

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

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

  58.     PlotLane[] forkingOffLanes;

  59.     PlotLane[] passingLanes;

  60.     PlotLane[] mergingLanes;

  61.     PlotLane lane;

  62.     PlotCommit[] children;

  63.     Ref[] refs;

  64.     /**
  65.      * Create a new commit.
  66.      *
  67.      * @param id
  68.      *            the identity of this commit.
  69.      */
  70.     protected PlotCommit(AnyObjectId id) {
  71.         super(id);
  72.         forkingOffLanes = NO_LANES;
  73.         passingLanes = NO_LANES;
  74.         mergingLanes = NO_LANES;
  75.         children = NO_CHILDREN;
  76.         refs = NO_REFS;
  77.     }

  78.     void addForkingOffLane(PlotLane f) {
  79.         forkingOffLanes = addLane(f, forkingOffLanes);
  80.     }

  81.     void addPassingLane(PlotLane c) {
  82.         passingLanes = addLane(c, passingLanes);
  83.     }

  84.     void addMergingLane(PlotLane m) {
  85.         mergingLanes = addLane(m, mergingLanes);
  86.     }

  87.     private static PlotLane[] addLane(PlotLane l, PlotLane[] lanes) {
  88.         final int cnt = lanes.length;
  89.         if (cnt == 0)
  90.             lanes = new PlotLane[] { l };
  91.         else if (cnt == 1)
  92.             lanes = new PlotLane[] { lanes[0], l };
  93.         else {
  94.             final PlotLane[] n = new PlotLane[cnt + 1];
  95.             System.arraycopy(lanes, 0, n, 0, cnt);
  96.             n[cnt] = l;
  97.             lanes = n;
  98.         }
  99.         return lanes;
  100.     }

  101.     void addChild(PlotCommit c) {
  102.         final int cnt = children.length;
  103.         if (cnt == 0)
  104.             children = new PlotCommit[] { c };
  105.         else if (cnt == 1) {
  106.             if (!c.getId().equals(children[0].getId()))
  107.                 children = new PlotCommit[] { children[0], c };
  108.         } else {
  109.             for (PlotCommit pc : children)
  110.                 if (c.getId().equals(pc.getId()))
  111.                     return;
  112.             final PlotCommit[] n = new PlotCommit[cnt + 1];
  113.             System.arraycopy(children, 0, n, 0, cnt);
  114.             n[cnt] = c;
  115.             children = n;
  116.         }
  117.     }

  118.     /**
  119.      * Get the number of child commits listed in this commit.
  120.      *
  121.      * @return number of children; always a positive value but can be 0.
  122.      */
  123.     public final int getChildCount() {
  124.         return children.length;
  125.     }

  126.     /**
  127.      * Get the nth child from this commit's child list.
  128.      *
  129.      * @param nth
  130.      *            child index to obtain. Must be in the range 0 through
  131.      *            {@link #getChildCount()}-1.
  132.      * @return the specified child.
  133.      * @throws java.lang.ArrayIndexOutOfBoundsException
  134.      *             an invalid child index was specified.
  135.      */
  136.     public final PlotCommit getChild(int nth) {
  137.         return children[nth];
  138.     }

  139.     /**
  140.      * Determine if the given commit is a child (descendant) of this commit.
  141.      *
  142.      * @param c
  143.      *            the commit to test.
  144.      * @return true if the given commit built on top of this commit.
  145.      */
  146.     public final boolean isChild(PlotCommit c) {
  147.         for (PlotCommit a : children)
  148.             if (a == c)
  149.                 return true;
  150.         return false;
  151.     }

  152.     /**
  153.      * Get the number of refs for this commit.
  154.      *
  155.      * @return number of refs; always a positive value but can be 0.
  156.      */
  157.     public final int getRefCount() {
  158.         return refs.length;
  159.     }

  160.     /**
  161.      * Get the nth Ref from this commit's ref list.
  162.      *
  163.      * @param nth
  164.      *            ref index to obtain. Must be in the range 0 through
  165.      *            {@link #getRefCount()}-1.
  166.      * @return the specified ref.
  167.      * @throws java.lang.ArrayIndexOutOfBoundsException
  168.      *             an invalid ref index was specified.
  169.      */
  170.     public final Ref getRef(int nth) {
  171.         return refs[nth];
  172.     }

  173.     /**
  174.      * Obtain the lane this commit has been plotted into.
  175.      *
  176.      * @return the assigned lane for this commit.
  177.      */
  178.     @SuppressWarnings("unchecked")
  179.     public final L getLane() {
  180.         return (L) lane;
  181.     }

  182.     /** {@inheritDoc} */
  183.     @Override
  184.     public void reset() {
  185.         forkingOffLanes = NO_LANES;
  186.         passingLanes = NO_LANES;
  187.         mergingLanes = NO_LANES;
  188.         children = NO_CHILDREN;
  189.         lane = null;
  190.         super.reset();
  191.     }
  192. }