1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.analysis
.timing
.core
.callgraph
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Comparator
;
16 import java
.util
.List
;
17 import java
.util
.Objects
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
21 import org
.eclipse
.tracecompass
.segmentstore
.core
.SegmentComparators
;
23 import com
.google
.common
.collect
.Ordering
;
26 * Called Functuon common class, defines the start, end, depth, parent and
27 * children. Does not define the symbol
29 * @author Matthew Khouzam
30 * @author Sonia Farrah
32 abstract class AbstractCalledFunction
implements ICalledFunction
{
34 static final Comparator
<ISegment
> COMPARATOR
;
37 * checkNotNull() has to be called separately, or else it breaks the
40 Comparator
<ISegment
> comp
= Ordering
.from(SegmentComparators
.INTERVAL_START_COMPARATOR
).compound(SegmentComparators
.INTERVAL_END_COMPARATOR
);
41 COMPARATOR
= checkNotNull(comp
);
47 private static final long serialVersionUID
= 7992199223906717340L;
49 protected final long fStart
;
50 protected final long fEnd
;
51 protected final int fDepth
;
52 private final List
<ICalledFunction
> fChildren
= new ArrayList
<>();
53 private final @Nullable ICalledFunction fParent
;
54 protected long fSelfTime
= 0;
56 public AbstractCalledFunction(long start
, long end
, int depth
, @Nullable ICalledFunction parent
) {
58 throw new IllegalArgumentException(Messages
.TimeError
+ "[" + start
+ "," + end
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
64 // It'll be modified once we add a child to it
65 fSelfTime
= fEnd
- fStart
;
69 public long getStart() {
74 public long getEnd() {
79 public List
<ICalledFunction
> getChildren() {
84 public @Nullable ICalledFunction
getParent() {
89 * Add the child to the segment's children, and subtract the child's
90 * duration to the duration of the segment so we can calculate its self
94 * The child to add to the segment's children
96 public void addChild(ICalledFunction child
) {
97 if (child
.getParent() != this) {
98 throw new IllegalArgumentException("Child parent not the same as child being added to."); //$NON-NLS-1$
100 fChildren
.add(child
);
101 substractChildDuration(child
.getEnd() - child
.getStart());
105 * Subtract the child's duration to the duration of the segment.
107 * @param childDuration
108 * The child's duration
110 private void substractChildDuration(long childDuration
) {
111 fSelfTime
-= childDuration
;
115 public long getSelfTime() {
120 public int getDepth() {
125 public int compareTo(@Nullable ISegment o
) {
127 throw new IllegalArgumentException();
129 return COMPARATOR
.compare(this, o
);
133 public String
toString() {
134 return new String("[" + String
.valueOf(fStart
) + ", " + String
.valueOf(fEnd
) + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
138 public int hashCode() {
139 final int prime
= 31;
141 result
= prime
* result
+ fDepth
;
142 result
= prime
* result
+ (int) (fEnd ^
(fEnd
>>> 32));
143 ICalledFunction parent
= fParent
;
144 result
= prime
* result
+ ((parent
== null) ?
0 : parent
.hashCode());
145 result
= prime
* result
+ (int) (fSelfTime ^
(fSelfTime
>>> 32));
146 result
= prime
* result
+ (int) (fStart ^
(fStart
>>> 32));
147 result
= prime
* result
+ getSymbol().hashCode();
152 public boolean equals(@Nullable Object obj
) {
159 if (getClass() != obj
.getClass()) {
162 AbstractCalledFunction other
= (AbstractCalledFunction
) obj
;
163 if (fDepth
!= other
.fDepth
) {
166 if (fEnd
!= other
.fEnd
) {
169 if (fParent
== null) {
170 if (other
.fParent
!= null) {
173 } else if (!Objects
.equals(fParent
, other
.fParent
)) {
176 if (fSelfTime
!= other
.fSelfTime
) {
179 if (fStart
!= other
.fStart
) {
182 if (!Objects
.equals(getSymbol(), other
.getSymbol())) {