Skip to content

Commit 3ac5d97

Browse files
committed
Allow to compare GCModels
For several model elements a proper #equals & hashcode have been added. This allows to compare GCEvents & GCModels, e.g. in tests. This should however also come in handy in other situations, e.g when storing them in hash based collections
1 parent 5f81c2a commit 3ac5d97

File tree

12 files changed

+297
-89
lines changed

12 files changed

+297
-89
lines changed

src/main/java/com/tagtraum/perf/gcviewer/math/DoubleData.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,41 @@ public static double weightedAverage(double[] n, int[] weight) {
8585
}
8686
return sum / m;
8787
}
88+
89+
@Override
90+
public boolean equals(Object o) {
91+
if (this == o)
92+
return true;
93+
if (o == null || getClass() != o.getClass())
94+
return false;
95+
96+
DoubleData that = (DoubleData) o;
97+
98+
if (n != that.n)
99+
return false;
100+
if (Double.compare(that.sum, sum) != 0)
101+
return false;
102+
if (Double.compare(that.sumSquares, sumSquares) != 0)
103+
return false;
104+
if (Double.compare(that.min, min) != 0)
105+
return false;
106+
return Double.compare(that.max, max) == 0;
107+
108+
}
109+
110+
@Override
111+
public int hashCode() {
112+
int result;
113+
long temp;
114+
result = n;
115+
temp = Double.doubleToLongBits(sum);
116+
result = 31 * result + (int) (temp ^ (temp >>> 32));
117+
temp = Double.doubleToLongBits(sumSquares);
118+
result = 31 * result + (int) (temp ^ (temp >>> 32));
119+
temp = Double.doubleToLongBits(min);
120+
result = 31 * result + (int) (temp ^ (temp >>> 32));
121+
temp = Double.doubleToLongBits(max);
122+
result = 31 * result + (int) (temp ^ (temp >>> 32));
123+
return result;
124+
}
88125
}

src/main/java/com/tagtraum/perf/gcviewer/math/IntData.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class IntData implements Serializable {
1515
private long sum;
1616
private long sumSquares;
1717
private int min = Integer.MAX_VALUE;
18-
private int max = Integer.MIN_VALUE;
18+
private int max = Integer.MIN_VALUE;
1919

2020
public void add(int x) {
2121
sum += x;
@@ -83,4 +83,35 @@ public static long weightedAverage(long[] n, int[] weight) {
8383
}
8484
return sum / m;
8585
}
86+
87+
@Override
88+
public boolean equals(Object o) {
89+
if (this == o)
90+
return true;
91+
if (o == null || getClass() != o.getClass())
92+
return false;
93+
94+
IntData intData = (IntData) o;
95+
96+
if (n != intData.n)
97+
return false;
98+
if (sum != intData.sum)
99+
return false;
100+
if (sumSquares != intData.sumSquares)
101+
return false;
102+
if (min != intData.min)
103+
return false;
104+
return max == intData.max;
105+
106+
}
107+
108+
@Override
109+
public int hashCode() {
110+
int result = n;
111+
result = 31 * result + (int) (sum ^ (sum >>> 32));
112+
result = 31 * result + (int) (sumSquares ^ (sumSquares >>> 32));
113+
result = 31 * result + min;
114+
result = 31 * result + max;
115+
return result;
116+
}
86117
}

src/main/java/com/tagtraum/perf/gcviewer/math/RegressionLine.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,40 @@ private static double slope(double n, double sumX, double sumY, double sumXY, do
6262
return (n * sumXY - sumX * sumY) / (n * sumXSquare - sumX * sumX);
6363
}
6464

65+
@Override
66+
public boolean equals(Object o) {
67+
if (this == o)
68+
return true;
69+
if (o == null || getClass() != o.getClass())
70+
return false;
71+
72+
RegressionLine that = (RegressionLine) o;
73+
74+
if (Double.compare(that.sumX, sumX) != 0)
75+
return false;
76+
if (Double.compare(that.sumY, sumY) != 0)
77+
return false;
78+
if (Double.compare(that.sumXSquare, sumXSquare) != 0)
79+
return false;
80+
if (Double.compare(that.sumXY, sumXY) != 0)
81+
return false;
82+
return n == that.n;
83+
84+
}
85+
86+
@Override
87+
public int hashCode() {
88+
int result;
89+
long temp;
90+
temp = Double.doubleToLongBits(sumX);
91+
result = (int) (temp ^ (temp >>> 32));
92+
temp = Double.doubleToLongBits(sumY);
93+
result = 31 * result + (int) (temp ^ (temp >>> 32));
94+
temp = Double.doubleToLongBits(sumXSquare);
95+
result = 31 * result + (int) (temp ^ (temp >>> 32));
96+
temp = Double.doubleToLongBits(sumXY);
97+
result = 31 * result + (int) (temp ^ (temp >>> 32));
98+
result = 31 * result + n;
99+
return result;
100+
}
65101
}

src/main/java/com/tagtraum/perf/gcviewer/model/AbstractGCEvent.java

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22

33
import java.io.Serializable;
44
import java.time.ZonedDateTime;
5-
import java.util.ArrayList;
6-
import java.util.Collections;
7-
import java.util.HashMap;
8-
import java.util.Iterator;
9-
import java.util.List;
10-
import java.util.Map;
11-
import java.util.Set;
12-
import java.util.TreeSet;
5+
import java.util.*;
136

147
/**
158
* The abstract gc event is the base class for all types of events. All sorts of general
@@ -19,7 +12,6 @@
1912
* @author <a href="mailto:[email protected]">Joerg Wuethrich</a>
2013
*/
2114
public abstract class AbstractGCEvent<T extends AbstractGCEvent<T>> implements Serializable {
22-
private final Iterator<T> EMPTY_ITERATOR = Collections.emptyIterator();
2315
private ZonedDateTime datestamp;
2416
private double timestamp;
2517
private ExtendedType extendedType = ExtendedType.UNDEFINED;
@@ -30,7 +22,7 @@ public abstract class AbstractGCEvent<T extends AbstractGCEvent<T>> implements S
3022
private double pause;
3123

3224
public Iterator<T> details() {
33-
if (details == null) return EMPTY_ITERATOR;
25+
if (details == null) return Collections.emptyIterator();
3426
return details.iterator();
3527
}
3628

@@ -160,27 +152,7 @@ public void setTenuredDetail(boolean tenuredDetail) {
160152
this.tenuredDetail = tenuredDetail;
161153
}
162154

163-
// better than nothing hashcode
164-
public int hashCode() {
165-
return toString().hashCode();
166-
}
167-
168-
// better than nothing equals
169-
public boolean equals(Object obj) {
170-
if (this == obj) {
171-
return true;
172-
}
173-
if (obj == null) {
174-
return false;
175-
}
176-
if (!(obj instanceof AbstractGCEvent<?>)) {
177-
return false;
178-
}
179-
180-
return toString().equals(obj.toString());
181-
}
182-
183-
public String toString() {
155+
public String toString() {
184156
StringBuffer sb = new StringBuffer(128);
185157
toStringBuffer(sb);
186158
return sb.toString();
@@ -248,6 +220,28 @@ public void setPause(double pause) {
248220
this.pause = pause;
249221
}
250222

223+
@Override
224+
public boolean equals(Object o) {
225+
if (this == o)
226+
return true;
227+
if (!(o instanceof AbstractGCEvent))
228+
return false;
229+
AbstractGCEvent<?> that = (AbstractGCEvent<?>) o;
230+
return Double.compare(that.timestamp, timestamp) == 0 &&
231+
tenuredDetail == that.tenuredDetail &&
232+
Double.compare(that.pause, pause) == 0 &&
233+
Objects.equals(datestamp, that.datestamp) &&
234+
Objects.equals(extendedType, that.extendedType) &&
235+
Objects.equals(typeAsString, that.typeAsString) &&
236+
generation == that.generation &&
237+
Objects.equals(details, that.details);
238+
}
239+
240+
@Override
241+
public int hashCode() {
242+
return Objects.hash(datestamp, timestamp, extendedType, tenuredDetail, typeAsString, generation, details);
243+
}
244+
251245
/**
252246
* Wrapper for the {@link Type} class adding a field for the full type name. That name may
253247
* be different from the name in <code>Type</code>. Since all other attributes of
@@ -316,8 +310,7 @@ public Concurrency getConcurrency() {
316310
public String toString() {
317311
return fullName;
318312
}
319-
320-
}
313+
}
321314

322315
/**
323316
* Representation of an event type

src/main/java/com/tagtraum/perf/gcviewer/model/ConcurrentGCEvent.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* ConcurrentGCEvent.
55
* @author <a href="mailto:[email protected]">Hendrik Schreiber</a>
66
*/
7-
public class ConcurrentGCEvent extends AbstractGCEvent<ConcurrentGCEvent> {
7+
public final class ConcurrentGCEvent extends AbstractGCEvent<ConcurrentGCEvent> {
88

99
private double duration;
1010

@@ -37,7 +37,7 @@ public boolean hasDuration() {
3737
return !getExtendedType().getName().endsWith("-start");
3838
}
3939

40-
public void toStringBuffer(StringBuffer sb) {
40+
public void toStringBuffer(StringBuffer sb) {
4141
sb.append(getTimestamp());
4242
sb.append(": [");
4343
sb.append(getExtendedType().getName());
@@ -50,4 +50,28 @@ public void toStringBuffer(StringBuffer sb) {
5050
}
5151
sb.append(']');
5252
}
53+
54+
@Override
55+
public boolean equals(Object o) {
56+
if (this == o)
57+
return true;
58+
if (o == null || getClass() != o.getClass())
59+
return false;
60+
if (!super.equals(o))
61+
return false;
62+
63+
ConcurrentGCEvent that = (ConcurrentGCEvent) o;
64+
65+
return Double.compare(that.duration, duration) == 0;
66+
67+
}
68+
69+
@Override
70+
public int hashCode() {
71+
int result = super.hashCode();
72+
long temp;
73+
temp = Double.doubleToLongBits(duration);
74+
result = 31 * result + (int) (temp ^ (temp >>> 32));
75+
return result;
76+
}
5377
}

src/main/java/com/tagtraum/perf/gcviewer/model/G1GcEvent.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,4 @@ public class G1GcEvent extends GCEvent {
1414
public String getTypeAsString() {
1515
return getExtendedType().getName();
1616
}
17-
18-
@Override
19-
public void toStringBuffer(StringBuffer sb) {
20-
super.toStringBuffer(sb);
21-
}
22-
2317
}

src/main/java/com/tagtraum/perf/gcviewer/model/GCEvent.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.tagtraum.perf.gcviewer.model;
22

3+
import java.util.Objects;
4+
35
/**
46
* The GCEvent is the type of event that contains memory (preused, postused, total) and
57
* pause information.
@@ -143,7 +145,7 @@ public void toStringBuffer(StringBuffer sb) {
143145
sb.append(getExtendedType() != null ? getExtendedType().getName() : ExtendedType.UNDEFINED);
144146
if (details != null) {
145147
sb.append(' ');
146-
for (GCEvent event : details) {
148+
for (AbstractGCEvent event : details) {
147149
event.toStringBuffer(sb);
148150
}
149151
sb.append(' ');
@@ -161,4 +163,22 @@ public void toStringBuffer(StringBuffer sb) {
161163
sb.append(" secs]");
162164
}
163165

166+
@Override
167+
public boolean equals(Object o) {
168+
if (this == o)
169+
return true;
170+
if (!(o instanceof GCEvent))
171+
return false;
172+
if (!super.equals(o))
173+
return false;
174+
GCEvent gcEvent = (GCEvent) o;
175+
return preUsed == gcEvent.preUsed &&
176+
postUsed == gcEvent.postUsed &&
177+
total == gcEvent.total;
178+
}
179+
180+
@Override
181+
public int hashCode() {
182+
return Objects.hash(super.hashCode(), preUsed, postUsed, total);
183+
}
164184
}

0 commit comments

Comments
 (0)