1 package org.djunits.unit.scale;
2
3 import org.djunits.unit.util.UnitRuntimeException;
4 import org.djutils.exceptions.Throw;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public class LinearScale implements Scale
20 {
21
22 private static final long serialVersionUID = 20151011L;
23
24
25 private final double conversionFactorToStandardUnit;
26
27
28
29
30
31
32 public LinearScale(final double conversionFactorToStandardUnit)
33 {
34 Throw.when(conversionFactorToStandardUnit == 0.0, UnitRuntimeException.class,
35 "conversion factor for linear scale cannnot be 0");
36 this.conversionFactorToStandardUnit = conversionFactorToStandardUnit;
37 }
38
39 @SuppressWarnings("checkstyle:designforextension")
40 @Override
41 public double toStandardUnit(final double value)
42 {
43 return value * this.conversionFactorToStandardUnit;
44 }
45
46 @SuppressWarnings("checkstyle:designforextension")
47 @Override
48 public double fromStandardUnit(final double value)
49 {
50 return value / this.conversionFactorToStandardUnit;
51 }
52
53
54
55
56
57 public final double getConversionFactorToStandardUnit()
58 {
59 return this.conversionFactorToStandardUnit;
60 }
61
62 @Override
63 public boolean isBaseSIScale()
64 {
65 return this.conversionFactorToStandardUnit == 1.0;
66 }
67
68 @Override
69 public int hashCode()
70 {
71 final int prime = 31;
72 int result = 1;
73 long temp;
74 temp = Double.doubleToLongBits(this.conversionFactorToStandardUnit);
75 result = prime * result + (int) (temp ^ (temp >>> 32));
76 return result;
77 }
78
79 @SuppressWarnings("checkstyle:needbraces")
80 @Override
81 public boolean equals(final Object obj)
82 {
83 if (this == obj)
84 return true;
85 if (obj == null)
86 return false;
87 if (getClass() != obj.getClass())
88 return false;
89 LinearScale other = (LinearScale) obj;
90 if (Double.doubleToLongBits(this.conversionFactorToStandardUnit) != Double
91 .doubleToLongBits(other.conversionFactorToStandardUnit))
92 return false;
93 return true;
94 }
95
96 @Override
97 public String toString()
98 {
99 return "LinearScale [conversionFactorToStandardUnit=" + this.conversionFactorToStandardUnit + "]";
100 }
101
102 }