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