timing.core: simplify hashCode, equals and toString of AbstractCalledFunction
[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;
c2845a63 55 private final int fProcessId;
2d8d933f 56
c2845a63 57 public AbstractCalledFunction(long start, long end, int depth, int processId, @Nullable ICalledFunction parent) {
2d8d933f
SF
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;
55f777f4
PT
65 // It'll be modified once we add a child to it
66 fSelfTime = fEnd - fStart;
c2845a63 67 fProcessId = processId;
2d8d933f
SF
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
c2845a63
BH
126 @Override
127 public int getProcessId() {
128 return fProcessId;
129 }
130
2d8d933f
SF
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() {
bc84ff58 141 return '[' + String.valueOf(fStart) + ", " + String.valueOf(fEnd) + ']'; //$NON-NLS-1$
2d8d933f
SF
142 }
143
144 @Override
145 public int hashCode() {
bc84ff58 146 return Objects.hash(fDepth, fEnd, fParent, fSelfTime, fStart, getSymbol());
2d8d933f
SF
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 }
2d8d933f
SF
167 if (fSelfTime != other.fSelfTime) {
168 return false;
169 }
170 if (fStart != other.fStart) {
171 return false;
172 }
bc84ff58
MK
173 if (!Objects.equals(fParent, other.getParent())) {
174 return false;
175 }
2d8d933f
SF
176 if (!Objects.equals(getSymbol(), other.getSymbol())) {
177 return false;
178 }
179 return true;
180 }
181
182}
This page took 0.033626 seconds and 5 git commands to generate.