1 package org.djunits.value.vdouble.scalar;
2
3 import org.djunits.unit.Unit;
4 import org.djunits.value.MathFunctionsRel;
5 import org.djunits.value.Relative;
6 import org.djunits.value.ValueUtil;
7 import org.djunits.value.vdouble.DoubleMathFunctions;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public abstract class AbstractDoubleScalarRel<U extends Unit<U>, R extends AbstractDoubleScalarRel<U, R>>
25 extends AbstractDoubleScalar<U, R> implements Relative, MathFunctionsRel<R>, DoubleMathFunctions<R>
26 {
27
28 private static final long serialVersionUID = 20150626L;
29
30
31
32
33
34
35 public AbstractDoubleScalarRel(final double value, final U unit)
36 {
37 super(unit, unit.isBaseSIUnit() ? value : ValueUtil.expressAsSIUnit(value, unit));
38 }
39
40
41
42
43
44 public AbstractDoubleScalarRel(final R value)
45 {
46 super(value.getUnit(), value.si);
47 }
48
49
50
51
52
53
54
55 public abstract R instantiateRel(double value, U unit);
56
57
58
59
60
61
62
63 public final R plus(final R increment)
64 {
65 if (getUnit().isBaseSIUnit())
66 {
67 return instantiateRel(this.si + increment.si, getUnit().getStandardUnit());
68 }
69 return getUnit().equals(increment.getUnit()) ? instantiateRel(getInUnit() + increment.getInUnit(), getUnit())
70 : instantiateRel(this.si + increment.si, getUnit().getStandardUnit());
71 }
72
73
74
75
76
77
78
79 public final R minus(final R decrement)
80 {
81 if (getUnit().isBaseSIUnit())
82 {
83 return instantiateRel(this.si - decrement.si, getUnit().getStandardUnit());
84 }
85 return getUnit().equals(decrement.getUnit()) ? instantiateRel(getInUnit() - decrement.getInUnit(), getUnit())
86 : instantiateRel(this.si - decrement.si, getUnit().getStandardUnit());
87 }
88
89
90
91
92
93
94
95
96
97
98 public static <U extends Unit<U>, R extends AbstractDoubleScalarRel<U, R>> R interpolate(final R zero, final R one,
99 final double ratio)
100 {
101 return zero.instantiateRel(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
102 }
103
104
105
106
107
108
109 @Override
110 @SuppressWarnings("checkstyle:designforextension")
111 public R abs()
112 {
113 return instantiateRel(Math.abs(getInUnit()), getUnit());
114 }
115
116
117 @Override
118 @SuppressWarnings("checkstyle:designforextension")
119 public R ceil()
120 {
121 return instantiateRel(Math.ceil(getInUnit()), getUnit());
122 }
123
124
125 @Override
126 @SuppressWarnings("checkstyle:designforextension")
127 public R floor()
128 {
129 return instantiateRel(Math.floor(getInUnit()), getUnit());
130 }
131
132
133 @Override
134 @SuppressWarnings("checkstyle:designforextension")
135 public R rint()
136 {
137 return instantiateRel(Math.rint(getInUnit()), getUnit());
138 }
139
140
141 @Override
142 @SuppressWarnings("checkstyle:designforextension")
143 public R round()
144 {
145 return instantiateRel(Math.round(getInUnit()), getUnit());
146 }
147
148
149 @Override
150 @SuppressWarnings("checkstyle:designforextension")
151 public R neg()
152 {
153 return instantiateRel(-getInUnit(), getUnit());
154 }
155
156
157 @Override
158 @SuppressWarnings("checkstyle:designforextension")
159 public R multiplyBy(final double constant)
160 {
161 return instantiateRel(getInUnit() * constant, getUnit());
162 }
163
164
165 @Override
166 @SuppressWarnings("checkstyle:designforextension")
167 public R divideBy(final double constant)
168 {
169 return instantiateRel(getInUnit() / constant, getUnit());
170 }
171 }