b44ea808067af1a2fe286c54f78d6648a86f0cf8
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / internal / analysis / timing / core / callgraph / AbstractCalledFunction.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
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 *******************************************************************************/
9
10 package org.eclipse.tracecompass.internal.analysis.timing.core.callgraph;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.util.ArrayList;
15 import java.util.Comparator;
16 import java.util.List;
17 import java.util.Objects;
18
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.common.core.NonNullUtils;
21 import org.eclipse.tracecompass.segmentstore.core.ISegment;
22 import org.eclipse.tracecompass.segmentstore.core.SegmentComparators;
23
24 import com.google.common.collect.Ordering;
25
26 /**
27 * Called Functuon common class, defines the start, end, depth, parent and
28 * children. Does not define the symbol
29 *
30 * @author Matthew Khouzam
31 * @author Sonia Farrah
32 */
33 abstract class AbstractCalledFunction implements ICalledFunction {
34
35 static final Comparator<ISegment> COMPARATOR;
36 static {
37 /*
38 * checkNotNull() has to be called separately, or else it breaks the
39 * type inference.
40 */
41 Comparator<ISegment> comp = Ordering.from(SegmentComparators.INTERVAL_START_COMPARATOR).compound(SegmentComparators.INTERVAL_END_COMPARATOR);
42 COMPARATOR = checkNotNull(comp);
43 }
44
45 /**
46 * Serial Version UID
47 */
48 private static final long serialVersionUID = 7992199223906717340L;
49
50 protected final long fStart;
51 protected final long fEnd;
52 protected final int fDepth;
53 private final List<ICalledFunction> fChildren = new ArrayList<>();
54 private final @Nullable ICalledFunction fParent;
55 protected long fSelfTime = 0;
56 private final int fProcessId;
57
58 public AbstractCalledFunction(long start, long end, int depth, int processId, @Nullable ICalledFunction parent) {
59 if (start > end) {
60 throw new IllegalArgumentException(Messages.TimeError + "[" + start + "," + end + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
61 }
62 fStart = start;
63 fEnd = end;
64 fDepth = depth;
65 fParent = parent;
66 // It'll be modified once we add a child to it
67 fSelfTime = fEnd - fStart;
68 fProcessId = processId;
69 }
70
71 @Override
72 public long getStart() {
73 return fStart;
74 }
75
76 @Override
77 public long getEnd() {
78 return fEnd;
79 }
80
81 @Override
82 public List<ICalledFunction> getChildren() {
83 return fChildren;
84 }
85
86 @Override
87 public @Nullable ICalledFunction getParent() {
88 return fParent;
89 }
90
91 @Override
92 public String getName() {
93 return NonNullUtils.nullToEmptyString(getSymbol().toString());
94 }
95
96 /**
97 * Add the child to the segment's children, and subtract the child's
98 * duration to the duration of the segment so we can calculate its self
99 * time.
100 *
101 * @param child
102 * The child to add to the segment's children
103 */
104 public void addChild(ICalledFunction child) {
105 if (child.getParent() != this) {
106 throw new IllegalArgumentException("Child parent not the same as child being added to."); //$NON-NLS-1$
107 }
108 fChildren.add(child);
109 substractChildDuration(child.getEnd() - child.getStart());
110 }
111
112 /**
113 * Subtract the child's duration to the duration of the segment.
114 *
115 * @param childDuration
116 * The child's duration
117 */
118 private void substractChildDuration(long childDuration) {
119 fSelfTime -= childDuration;
120 }
121
122 @Override
123 public long getSelfTime() {
124 return fSelfTime;
125 }
126
127 @Override
128 public int getDepth() {
129 return fDepth;
130 }
131
132 @Override
133 public int getProcessId() {
134 return fProcessId;
135 }
136
137 @Override
138 public int compareTo(@Nullable ISegment o) {
139 if (o == null) {
140 throw new IllegalArgumentException();
141 }
142 return COMPARATOR.compare(this, o);
143 }
144
145 @Override
146 public String toString() {
147 return '[' + String.valueOf(fStart) + ", " + String.valueOf(fEnd) + ']' + " Duration: " + getLength() + ", Self Time: " + fSelfTime; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
148 }
149
150 @Override
151 public int hashCode() {
152 return Objects.hash(fDepth, fEnd, fParent, fSelfTime, fStart, getSymbol());
153 }
154
155 @Override
156 public boolean equals(@Nullable Object obj) {
157 if (this == obj) {
158 return true;
159 }
160 if (obj == null) {
161 return false;
162 }
163 if (getClass() != obj.getClass()) {
164 return false;
165 }
166 AbstractCalledFunction other = (AbstractCalledFunction) obj;
167 if (fDepth != other.fDepth) {
168 return false;
169 }
170 if (fEnd != other.fEnd) {
171 return false;
172 }
173 if (fSelfTime != other.fSelfTime) {
174 return false;
175 }
176 if (fStart != other.fStart) {
177 return false;
178 }
179 if (!Objects.equals(fParent, other.getParent())) {
180 return false;
181 }
182 if (!Objects.equals(getSymbol(), other.getSymbol())) {
183 return false;
184 }
185 return true;
186 }
187
188 }
This page took 0.034729 seconds and 4 git commands to generate.