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 @Override
95 @SuppressWarnings("checkstyle:designforextension")
96 public R abs()
97 {
98 return instantiateRel(Math.abs(getInUnit()), getUnit());
99 }
100
101
102 @Override
103 @SuppressWarnings("checkstyle:designforextension")
104 public R ceil()
105 {
106 return instantiateRel(Math.ceil(getInUnit()), getUnit());
107 }
108
109
110 @Override
111 @SuppressWarnings("checkstyle:designforextension")
112 public R floor()
113 {
114 return instantiateRel(Math.floor(getInUnit()), getUnit());
115 }
116
117
118 @Override
119 @SuppressWarnings("checkstyle:designforextension")
120 public R rint()
121 {
122 return instantiateRel(Math.rint(getInUnit()), getUnit());
123 }
124
125
126 @Override
127 @SuppressWarnings("checkstyle:designforextension")
128 public R round()
129 {
130 return instantiateRel(Math.round(getInUnit()), getUnit());
131 }
132
133
134 @Override
135 @SuppressWarnings("checkstyle:designforextension")
136 public R neg()
137 {
138 return instantiateRel(-getInUnit(), getUnit());
139 }
140
141
142 @Override
143 @SuppressWarnings("checkstyle:designforextension")
144 public R multiplyBy(final double constant)
145 {
146 return instantiateRel(getInUnit() * constant, getUnit());
147 }
148
149
150 @Override
151 @SuppressWarnings("checkstyle:designforextension")
152 public R divideBy(final double constant)
153 {
154 return instantiateRel(getInUnit() / constant, getUnit());
155 }
156 }