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
44 package org.eclipse.jgit.revplot;
45
46 import org.eclipse.jgit.lib.AnyObjectId;
47 import org.eclipse.jgit.lib.Ref;
48 import org.eclipse.jgit.revwalk.RevCommit;
49
50 /**
51 * A commit reference to a commit in the DAG.
52 *
53 * @param <L>
54 * type of lane being used by the plotter.
55 * @see PlotCommitList
56 */
57 public class PlotCommit<L extends PlotLane> extends RevCommit {
58 static final PlotCommit[] NO_CHILDREN = {};
59
60 static final PlotLane[] NO_LANES = {};
61
62 static final Ref[] NO_REFS = {};
63
64 PlotLane[] forkingOffLanes;
65
66 PlotLane[] passingLanes;
67
68 PlotLane[] mergingLanes;
69
70 PlotLane lane;
71
72 PlotCommit[] children;
73
74 Ref[] refs;
75
76 /**
77 * Create a new commit.
78 *
79 * @param id
80 * the identity of this commit.
81 */
82 protected PlotCommit(AnyObjectId id) {
83 super(id);
84 forkingOffLanes = NO_LANES;
85 passingLanes = NO_LANES;
86 mergingLanes = NO_LANES;
87 children = NO_CHILDREN;
88 refs = NO_REFS;
89 }
90
91 void addForkingOffLane(PlotLane f) {
92 forkingOffLanes = addLane(f, forkingOffLanes);
93 }
94
95 void addPassingLane(PlotLane c) {
96 passingLanes = addLane(c, passingLanes);
97 }
98
99 void addMergingLane(PlotLane m) {
100 mergingLanes = addLane(m, mergingLanes);
101 }
102
103 private static PlotLaneref="../../../../org/eclipse/jgit/revplot/PlotLane.html#PlotLane">PlotLane/../../../org/eclipse/jgit/revplot/PlotLane.html#PlotLane">PlotLane[] addLane(PlotLaneref="../../../../org/eclipse/jgit/revplot/PlotLane.html#PlotLane">PlotLane l, PlotLane[] lanes) {
104 final int cnt = lanes.length;
105 if (cnt == 0)
106 lanes = new PlotLane[] { l };
107 else if (cnt == 1)
108 lanes = new PlotLane[] { lanes[0], l };
109 else {
110 final PlotLaneotLane.html#PlotLane">PlotLane[] n = new PlotLane[cnt + 1];
111 System.arraycopy(lanes, 0, n, 0, cnt);
112 n[cnt] = l;
113 lanes = n;
114 }
115 return lanes;
116 }
117
118 void addChild(PlotCommit c) {
119 final int cnt = children.length;
120 if (cnt == 0)
121 children = new PlotCommit[] { c };
122 else if (cnt == 1) {
123 if (!c.getId().equals(children[0].getId()))
124 children = new PlotCommit[] { children[0], c };
125 } else {
126 for (PlotCommit pc : children)
127 if (c.getId().equals(pc.getId()))
128 return;
129 final PlotCommitCommit.html#PlotCommit">PlotCommit[] n = new PlotCommit[cnt + 1];
130 System.arraycopy(children, 0, n, 0, cnt);
131 n[cnt] = c;
132 children = n;
133 }
134 }
135
136 /**
137 * Get the number of child commits listed in this commit.
138 *
139 * @return number of children; always a positive value but can be 0.
140 */
141 public final int getChildCount() {
142 return children.length;
143 }
144
145 /**
146 * Get the nth child from this commit's child list.
147 *
148 * @param nth
149 * child index to obtain. Must be in the range 0 through
150 * {@link #getChildCount()}-1.
151 * @return the specified child.
152 * @throws java.lang.ArrayIndexOutOfBoundsException
153 * an invalid child index was specified.
154 */
155 public final PlotCommit getChild(int nth) {
156 return children[nth];
157 }
158
159 /**
160 * Determine if the given commit is a child (descendant) of this commit.
161 *
162 * @param c
163 * the commit to test.
164 * @return true if the given commit built on top of this commit.
165 */
166 public final boolean isChild(PlotCommit c) {
167 for (PlotCommit a : children)
168 if (a == c)
169 return true;
170 return false;
171 }
172
173 /**
174 * Get the number of refs for this commit.
175 *
176 * @return number of refs; always a positive value but can be 0.
177 */
178 public final int getRefCount() {
179 return refs.length;
180 }
181
182 /**
183 * Get the nth Ref from this commit's ref list.
184 *
185 * @param nth
186 * ref index to obtain. Must be in the range 0 through
187 * {@link #getRefCount()}-1.
188 * @return the specified ref.
189 * @throws java.lang.ArrayIndexOutOfBoundsException
190 * an invalid ref index was specified.
191 */
192 public final Ref getRef(int nth) {
193 return refs[nth];
194 }
195
196 /**
197 * Obtain the lane this commit has been plotted into.
198 *
199 * @return the assigned lane for this commit.
200 */
201 @SuppressWarnings("unchecked")
202 public final L getLane() {
203 return (L) lane;
204 }
205
206 /** {@inheritDoc} */
207 @Override
208 public void reset() {
209 forkingOffLanes = NO_LANES;
210 passingLanes = NO_LANES;
211 mergingLanes = NO_LANES;
212 children = NO_CHILDREN;
213 lane = null;
214 super.reset();
215 }
216 }