Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
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; | |
64 | } | |
65 | ||
66 | @Override | |
67 | public long getStart() { | |
68 | return fStart; | |
69 | } | |
70 | ||
71 | @Override | |
72 | public long getEnd() { | |
73 | return fEnd; | |
74 | } | |
75 | ||
76 | @Override | |
77 | public List<ICalledFunction> getChildren() { | |
78 | return fChildren; | |
79 | } | |
80 | ||
81 | @Override | |
82 | public @Nullable ICalledFunction getParent() { | |
83 | return fParent; | |
84 | } | |
85 | ||
86 | /** | |
87 | * Add the child to the segment's children, and subtract the child's | |
88 | * duration to the duration of the segment so we can calculate its self | |
89 | * time. | |
90 | * | |
91 | * @param child | |
92 | * The child to add to the segment's children | |
93 | */ | |
94 | public void addChild(ICalledFunction child) { | |
95 | if (child.getParent() != this) { | |
96 | throw new IllegalArgumentException("Child parent not the same as child being added to."); //$NON-NLS-1$ | |
97 | } | |
98 | fChildren.add(child); | |
99 | substractChildDuration(child.getEnd() - child.getStart()); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Subtract the child's duration to the duration of the segment. | |
104 | * | |
105 | * @param childDuration | |
106 | * The child's duration | |
107 | */ | |
108 | private void substractChildDuration(long childDuration) { | |
109 | fSelfTime -= childDuration; | |
110 | } | |
111 | ||
112 | @Override | |
113 | public long getSelfTime() { | |
114 | return fSelfTime; | |
115 | } | |
116 | ||
117 | @Override | |
118 | public int getDepth() { | |
119 | return fDepth; | |
120 | } | |
121 | ||
122 | @Override | |
123 | public int compareTo(@Nullable ISegment o) { | |
124 | if (o == null) { | |
125 | throw new IllegalArgumentException(); | |
126 | } | |
127 | return COMPARATOR.compare(this, o); | |
128 | } | |
129 | ||
130 | @Override | |
131 | public String toString() { | |
132 | return new String("[" + String.valueOf(fStart) + ", " + String.valueOf(fEnd) + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
133 | } | |
134 | ||
135 | @Override | |
136 | public int hashCode() { | |
137 | final int prime = 31; | |
138 | int result = 1; | |
139 | result = prime * result + fDepth; | |
140 | result = prime * result + (int) (fEnd ^ (fEnd >>> 32)); | |
141 | ICalledFunction parent = fParent; | |
142 | result = prime * result + ((parent == null) ? 0 : parent.hashCode()); | |
143 | result = prime * result + (int) (fSelfTime ^ (fSelfTime >>> 32)); | |
144 | result = prime * result + (int) (fStart ^ (fStart >>> 32)); | |
145 | result = prime * result + getSymbol().hashCode(); | |
146 | return result; | |
147 | } | |
148 | ||
149 | @Override | |
150 | public boolean equals(@Nullable Object obj) { | |
151 | if (this == obj) { | |
152 | return true; | |
153 | } | |
154 | if (obj == null) { | |
155 | return false; | |
156 | } | |
157 | if (getClass() != obj.getClass()) { | |
158 | return false; | |
159 | } | |
160 | AbstractCalledFunction other = (AbstractCalledFunction) obj; | |
161 | if (fDepth != other.fDepth) { | |
162 | return false; | |
163 | } | |
164 | if (fEnd != other.fEnd) { | |
165 | return false; | |
166 | } | |
167 | if (fParent == null) { | |
168 | if (other.fParent != null) { | |
169 | return false; | |
170 | } | |
171 | } else if (!Objects.equals(fParent, other.fParent)) { | |
172 | return false; | |
173 | } | |
174 | if (fSelfTime != other.fSelfTime) { | |
175 | return false; | |
176 | } | |
177 | if (fStart != other.fStart) { | |
178 | return false; | |
179 | } | |
180 | if (!Objects.equals(getSymbol(), other.getSymbol())) { | |
181 | return false; | |
182 | } | |
183 | return true; | |
184 | } | |
185 | ||
186 | } |