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