1 /**
2 * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of Konstanz nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 package org.perfidix.meter;
28
29 import java.math.BigDecimal;
30 import java.math.MathContext;
31
32 /**
33 * Meter to bench the amount of time used by the current Benchmark. The
34 * TimeMeter is in need of an instance of the {@link Time} enumeration to give
35 * back the suitable time.
36 *
37 * @see Time
38 * @author Sebastian Graf, University of Konstanz
39 */
40 public final class TimeMeter extends AbstractMeter {
41
42 /** Name of the Meter. */
43 private static final String NAME = "TimeMeter";
44
45 /**
46 * Instance of the enum <code>Time</code> for correct formatting of the
47 * time.
48 */
49 private transient final Time currentTime;
50
51 /**
52 * Constructor which is in need of a given time.
53 *
54 * @param paramTime
55 * the time for the values.
56 */
57 public TimeMeter(final Time paramTime) {
58 super();
59 currentTime = paramTime;
60 }
61
62 /**
63 * {@inheritDoc}
64 */
65 @Override
66 public double getValue() {
67 return new BigDecimal(System.nanoTime(), MathContext.DECIMAL128).divide(
68 new BigDecimal(currentTime.getNumberOfMilliSeconds(), MathContext.DECIMAL128),
69 MathContext.DECIMAL128).doubleValue();
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 public String getName() {
75 return NAME;
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public String getUnit() {
81 return currentTime.getUnit();
82 }
83
84 /** {@inheritDoc} */
85 @Override
86 public String getUnitDescription() {
87 return currentTime.getUnitDescription();
88 }
89
90 /** {@inheritDoc} */
91 @Override
92 public int hashCode() {
93 final int prime = 31;
94 int result = prime;
95 if (currentTime == null) {
96 result = prime * result;
97 } else {
98 result = prime * result + currentTime.hashCode();
99 }
100
101 return result;
102 }
103
104 /** {@inheritDoc} */
105 @Override
106 public boolean equals(final Object obj) {
107 boolean returnVal = true;
108 if (this == obj) {
109 returnVal = true;
110 }
111 if (getClass() != obj.getClass()) {
112 returnVal = false;
113 }
114 final TimeMeter other = (TimeMeter)obj;
115 if (currentTime == null) {
116 if (other.currentTime != null) {
117 returnVal = false;
118 }
119 } else {
120 if (!currentTime.equals(other.currentTime)) {
121 returnVal = false;
122 }
123 }
124 return returnVal;
125 }
126
127 }