1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.spf4j.perf.impl.chart;
33
34 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35 import java.time.Instant;
36 import java.time.format.DateTimeFormatter;
37 import org.jfree.chart.axis.NumberTickUnit;
38
39
40
41
42
43
44
45 @SuppressFBWarnings({"NFF_NON_FUNCTIONAL_FIELD", "SE_TRANSIENT_FIELD_NOT_RESTORED"})
46 class TimestampTickUnitImpl extends NumberTickUnit {
47
48 private static final long serialVersionUID = 0L;
49 private final long[] timestamps;
50 private final long stepMillis;
51 private final transient DateTimeFormatter formatter;
52
53 TimestampTickUnitImpl(final double size,
54 final long[] timestamps, final long stepMillis, final DateTimeFormatter formatter) {
55 super(size);
56 this.timestamps = timestamps;
57 this.formatter = formatter;
58 this.stepMillis = stepMillis;
59 }
60
61 @Override
62 public String valueToString(final double value) {
63 int ival = (int) Math.round(value);
64 long val;
65 if (ival >= timestamps.length) {
66 val = timestamps[timestamps.length - 1] + stepMillis * (ival - timestamps.length + 1);
67 } else if (ival < 0) {
68 val = timestamps[0] + ival * stepMillis;
69 } else {
70 val = timestamps[ival];
71 }
72 return formatter.format(Instant.ofEpochMilli(val));
73 }
74
75 @Override
76 public int hashCode() {
77 int hash = 7;
78 hash = 89 * hash + java.util.Arrays.hashCode(this.timestamps);
79 hash = 89 * hash + (int) (this.stepMillis ^ (this.stepMillis >>> 32));
80 return 89 * hash + (this.formatter != null ? this.formatter.hashCode() : 0);
81 }
82
83 @Override
84 public boolean equals(final Object obj) {
85 if (obj == null) {
86 return false;
87 }
88 if (getClass() != obj.getClass()) {
89 return false;
90 }
91 final TimestampTickUnitImpl other = (TimestampTickUnitImpl) obj;
92 if (!java.util.Arrays.equals(this.timestamps, other.timestamps)) {
93 return false;
94 }
95 if (this.stepMillis != other.stepMillis) {
96 return false;
97 }
98 return !(this.formatter != other.formatter
99 && (this.formatter == null || !this.formatter.equals(other.formatter)));
100 }
101
102 }