1 package org.djunits.value.vdouble.scalar.base;
2
3 import org.djunits.unit.AbsoluteLinearUnit;
4 import org.djunits.unit.Unit;
5 import org.djunits.value.Absolute;
6
7 /**
8 * The typed, abstract DoubleScalarAbs class that forms the basis of all DoubleScalar definitions and extensions.<br>
9 * Note: A relative scalar class can implement the toAbs() method if it has an absolute equivalent.
10 * <p>
11 * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12 * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
13 * </p>
14 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15 * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
16 * @param <AU> the absolute unit
17 * @param <A> the Absolute class for reference purposes
18 * @param <RU> the relative unit
19 * @param <R> the Relative class for reference purposes
20 */
21 public abstract class DoubleScalarAbs<AU extends AbsoluteLinearUnit<AU, RU>, A extends DoubleScalarAbs<AU, A, RU, R>,
22 RU extends Unit<RU>, R extends DoubleScalarRelWithAbs<AU, A, RU, R>> extends DoubleScalar<AU, A>
23 implements Absolute<AU, A, RU, R>
24 {
25 /** */
26 private static final long serialVersionUID = 20150626L;
27
28 /**
29 * Construct a new Absolute Immutable DoubleScalar.
30 * @param value the value of the new Absolute Immutable DoubleScalar
31 * @param unit the unit of the new Absolute Immutable DoubleScalar
32 */
33 public DoubleScalarAbs(final double value, final AU unit)
34 {
35 super(value, unit);
36 }
37
38 /**
39 * Construct a new Absolute Immutable DoubleScalar from an existing Absolute Immutable DoubleScalar.
40 * @param value the reference
41 */
42 public DoubleScalarAbs(final A value)
43 {
44 super(value);
45 }
46
47 /**
48 * Construct a new Relative Immutable DoubleScalar of the right type. Each extending class must implement this method.
49 * @param value the double value
50 * @param unit the unit
51 * @return R a new relative instance of the DoubleScalar of the right type
52 */
53 public abstract R instantiateRel(double value, RU unit);
54
55 /**
56 * Construct a new Absolute Immutable DoubleScalar of the right type. Each extending class must implement this method.
57 * @param value the double value
58 * @param unit the absolute unit
59 * @return A a new absolute instance of the DoubleScalar of the right type
60 */
61 public abstract A instantiateAbs(double value, AU unit);
62
63 @Override
64 public final A plus(final R increment)
65 {
66 AU targetUnit = getDisplayUnit();
67 return instantiateAbs(getInUnit() + increment.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
68 }
69
70 @Override
71 public final A minus(final R decrement)
72 {
73 AU targetUnit = getDisplayUnit();
74 return instantiateAbs(getInUnit() - decrement.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
75 }
76
77 @Override
78 public final R minus(final A decrement)
79 {
80 RU targetUnit = getDisplayUnit().getRelativeUnit();
81 return instantiateRel(getInUnit() - decrement.getInUnit(getDisplayUnit()), targetUnit);
82 }
83
84 /**********************************************************************************/
85 /********************************** MATH METHODS **********************************/
86 /**********************************************************************************/
87
88 @Override
89 public A abs()
90 {
91 return instantiateAbs(Math.abs(getInUnit()), getDisplayUnit());
92 }
93
94 @Override
95 public A ceil()
96 {
97 return instantiateAbs(Math.ceil(getInUnit()), getDisplayUnit());
98 }
99
100 @Override
101 public A floor()
102 {
103 return instantiateAbs(Math.floor(getInUnit()), getDisplayUnit());
104 }
105
106 @Override
107 public A neg()
108 {
109 return instantiateAbs(-getInUnit(), getDisplayUnit());
110 }
111
112 @Override
113 public A rint()
114 {
115 return instantiateAbs(Math.rint(getInUnit()), getDisplayUnit());
116 }
117
118 }