Remove extra item.select() calls
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / internal / analysis / timing / core / callgraph / AbstractCalledFunction.java
CommitLineData
2d8d933f
SF
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
10package org.eclipse.tracecompass.internal.analysis.timing.core.callgraph;
11
12import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14import java.util.ArrayList;
15import java.util.Comparator;
16import java.util.List;
17import java.util.Objects;
18
19import org.eclipse.jdt.annotation.Nullable;
20import org.eclipse.tracecompass.segmentstore.core.ISegment;
21import org.eclipse.tracecompass.segmentstore.core.SegmentComparators;
22
23import 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 */
32abstract 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
56 public AbstractCalledFunction(long start, long end, int depth, @Nullable ICalledFunction parent) {
57 if (start > end) {
58 throw new IllegalArgumentException(Messages.TimeError + "[" + start + "," + end + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
59 }
60 fStart = start;
61 fEnd = end;
62 fDepth = depth;
63 fParent = parent;
55f777f4
PT
64 // It'll be modified once we add a child to it
65 fSelfTime = fEnd - fStart;
2d8d933f
SF
66 }
67
68 @Override
69 public long getStart() {
70 return fStart;
71 }
72
73 @Override
74 public long getEnd() {
75 return fEnd;
76 }
77
78 @Override
79 public List<ICalledFunction> getChildren() {
80 return fChildren;
81 }
82
83 @Override
84 public @Nullable ICalledFunction getParent() {
85 return fParent;
86 }
87
88 /**
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
91 * time.
92 *
93 * @param child
94 * The child to add to the segment's children
95 */
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$
99 }
100 fChildren.add(child);
101 substractChildDuration(child.getEnd() - child.getStart());
102 }
103
104 /**
105 * Subtract the child's duration to the duration of the segment.
106 *
107 * @param childDuration
108 * The child's duration
109 */
110 private void substractChildDuration(long childDuration) {
111 fSelfTime -= childDuration;
112 }
113
114 @Override
115 public long getSelfTime() {
116 return fSelfTime;
117 }
118
119 @Override
120 public int getDepth() {
121 return fDepth;
122 }
123
124 @Override
125 public int compareTo(@Nullable ISegment o) {
126 if (o == null) {
127 throw new IllegalArgumentException();
128 }
129 return COMPARATOR.compare(this, o);
130 }
131
132 @Override
133 public String toString() {
134 return new String("[" + String.valueOf(fStart) + ", " + String.valueOf(fEnd) + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
135 }
136
137 @Override
138 public int hashCode() {
139 final int prime = 31;
140 int result = 1;
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();
148 return result;
149 }
150
151 @Override
152 public boolean equals(@Nullable Object obj) {
153 if (this == obj) {
154 return true;
155 }
156 if (obj == null) {
157 return false;
158 }
159 if (getClass() != obj.getClass()) {
160 return false;
161 }
162 AbstractCalledFunction other = (AbstractCalledFunction) obj;
163 if (fDepth != other.fDepth) {
164 return false;
165 }
166 if (fEnd != other.fEnd) {
167 return false;
168 }
169 if (fParent == null) {
170 if (other.fParent != null) {
171 return false;
172 }
173 } else if (!Objects.equals(fParent, other.fParent)) {
174 return false;
175 }
176 if (fSelfTime != other.fSelfTime) {
177 return false;
178 }
179 if (fStart != other.fStart) {
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.033004 seconds and 5 git commands to generate.