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