View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
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   * This class although implements Serializable is not implemented correctly to serialize. Since I do not intend to
41   * serialize this, I will skip the proper implementation.
42   *
43   * @author zoly
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 }