1 package org.djunits.value.vdouble.vector.base;
2
3 import org.djunits.unit.AbsoluteLinearUnit;
4 import org.djunits.unit.Unit;
5 import org.djunits.value.Absolute;
6 import org.djunits.value.ValueRuntimeException;
7 import org.djunits.value.vdouble.function.DoubleMathFunctions;
8 import org.djunits.value.vdouble.scalar.base.DoubleScalarAbs;
9 import org.djunits.value.vdouble.scalar.base.DoubleScalarRelWithAbs;
10 import org.djunits.value.vdouble.vector.data.DoubleVectorData;
11
12 /**
13 * AbstractMutableDoubleVectorRelWithAbs.java.
14 * <p>
15 * Copyright (c) 2019-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16 * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
17 * </p>
18 * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
19 * @param <AU> the absolute unit belonging to the relative unit
20 * @param <A> the absolute scalar type belonging to the absolute vector type
21 * @param <AV> the (immutable or mutable) absolute vector type
22 * @param <RU> the relative unit belonging to the absolute unit
23 * @param <R> the relative scalar type belonging to the relative vector type
24 * @param <RV> the relative (immutable or mutable) vector type with this unit
25 */
26 // @formatter:off
27 public abstract class DoubleVectorAbs<
28 AU extends AbsoluteLinearUnit<AU, RU>,
29 A extends DoubleScalarAbs<AU, A, RU, R>,
30 AV extends DoubleVectorAbs<AU, A, AV, RU, R, RV>,
31 RU extends Unit<RU>,
32 R extends DoubleScalarRelWithAbs<AU, A, RU, R>,
33 RV extends DoubleVectorRelWithAbs<AU, A, AV, RU, R, RV>>
34 extends DoubleVector<AU, A, AV>
35 implements Absolute<AU, AV, RU, RV>
36 // @formatter:on
37 {
38 /** */
39 private static final long serialVersionUID = 20190908L;
40
41 /**
42 * Construct a new Relative Mutable DoubleVector.
43 * @param data an internal data object
44 * @param unit the unit
45 */
46 protected DoubleVectorAbs(final DoubleVectorData data, final AU unit)
47 {
48 super(data.copy(), unit);
49 }
50
51 @Override
52 public AV plus(final RV increment) throws ValueRuntimeException
53 {
54 return instantiateVector(this.getData().plus(increment.getData()), getDisplayUnit());
55 }
56
57 @Override
58 public AV minus(final RV decrement) throws ValueRuntimeException
59 {
60 return instantiateVector(this.getData().minus(decrement.getData()), getDisplayUnit());
61 }
62
63 @Override
64 public RV minus(final AV decrement) throws ValueRuntimeException
65 {
66 return instantiateVectorRel(this.getData().minus(decrement.getData()), decrement.getDisplayUnit().getRelativeUnit());
67 }
68
69 /**
70 * Decrement all values of this vector by the decrement. This only works if this vector is mutable.
71 * @param decrement the scalar by which to decrement all values
72 * @return this modified vector
73 * @throws ValueRuntimeException in case this vector is immutable
74 */
75 @SuppressWarnings("unchecked")
76 public AV decrementBy(final R decrement)
77 {
78 checkCopyOnWrite();
79 assign(DoubleMathFunctions.DEC(decrement.si));
80 return (AV) this;
81 }
82
83 /**
84 * Decrement all values of this vector by the decrement on a value by value basis. This only works if this vector is
85 * mutable.
86 * @param decrement the vector that contains the values by which to decrement the corresponding values
87 * @return this modified vector
88 * @throws ValueRuntimeException in case this vector is immutable, when the sizes of the vectors differ, or
89 * <code>decrement</code> is null
90 */
91 @SuppressWarnings("unchecked")
92 public AV decrementBy(final RV decrement)
93 {
94 checkCopyOnWrite();
95 getData().decrementBy(decrement.getData());
96 return (AV) this;
97 }
98
99 /**
100 * Instantiate a new relative vector of the class of this absolute vector. This can be used instead of the
101 * DoubleVector.instiantiate() methods in case another vector of this absolute vector class is known. The method is faster
102 * than DoubleVector.instantiate, and it will also work if the vector is user-defined.
103 * @param dvd the data used to instantiate the vector
104 * @param displayUnit the display unit of the relative vector
105 * @return a relative vector of the correct type, belonging to this absolute vector type
106 */
107 public abstract RV instantiateVectorRel(DoubleVectorData dvd, RU displayUnit);
108
109 /**
110 * Instantiate a new relative scalar for the class of this absolute vector. This can be used instead of the
111 * DoubleScalar.instiantiate() methods in case a vector of this class is known. The method is faster than
112 * DoubleScalar.instantiate, and it will also work if the vector and/or scalar are user-defined.
113 * @param valueSI the SI value of the relative scalar
114 * @param displayUunit the unit in which the relative value will be displayed
115 * @return a relative scalar of the correct type, belonging to this absolute vector type
116 */
117 public abstract R instantiateScalarRelSI(double valueSI, RU displayUunit);
118
119 }