1 package org.djunits.value.vdouble.scalar; 2 3 import org.djunits.unit.AbsoluteLinearUnit; 4 import org.djunits.unit.Unit; 5 import org.djunits.value.Absolute; 6 import org.djunits.value.MathFunctionsAbs; 7 import org.djunits.value.ValueUtil; 8 import org.djunits.value.vdouble.DoubleMathFunctions; 9 10 /** 11 * The typed, abstract DoubleScalarAbs class that forms the basis of all DoubleScalar definitions and extensions.<br> 12 * Note: A relative scalar class can implement the toAbs() method if it has an absolute equivalent. 13 * <p> 14 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 15 * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>. 16 * </p> 17 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $, 18 * initial version Oct 13, 2016 <br> 19 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 20 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 21 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a> 22 * @param <AU> the absolute unit 23 * @param <A> the Absolute class for reference purposes 24 * @param <RU> the relative unit 25 * @param <R> the Relative class for reference purposes 26 */ 27 public abstract class AbstractDoubleScalarAbs<AU extends AbsoluteLinearUnit<AU, RU>, 28 A extends AbstractDoubleScalarAbs<AU, A, RU, R>, RU extends Unit<RU>, R extends AbstractDoubleScalarRel<RU, R>> 29 extends AbstractDoubleScalar<AU, A> implements Absolute, MathFunctionsAbs<A>, DoubleMathFunctions<A> 30 { 31 /** */ 32 private static final long serialVersionUID = 20150626L; 33 34 /** 35 * Construct a new Absolute Immutable DoubleScalar. 36 * @param value double; the value of the new Absolute Immutable DoubleScalar 37 * @param unit AU; the unit of the new Absolute Immutable DoubleScalar 38 */ 39 public AbstractDoubleScalarAbs(final double value, final AU unit) 40 { 41 super(unit, unit.isBaseSIUnit() ? value : ValueUtil.expressAsSIUnit(value, unit)); 42 } 43 44 /** 45 * Construct a new Absolute Immutable DoubleScalar from an existing Absolute Immutable DoubleScalar. 46 * @param value A, an absolute typed DoubleScalar; the reference 47 */ 48 public AbstractDoubleScalarAbs(final A value) 49 { 50 super(value.getUnit(), value.si); 51 } 52 53 /** 54 * Construct a new Absolute Immutable DoubleScalar of the right type. Each extending class must implement this method. 55 * @param value double; the double value 56 * @param unit AU; the unit 57 * @return A a new absolute instance of the DoubleScalar of the right type 58 */ 59 public abstract A instantiateAbs(double value, AU unit); 60 61 /** 62 * Construct a new Relative Immutable DoubleScalar of the right type. Each extending class must implement this method. 63 * @param value double; the double value 64 * @param unit RU; the unit 65 * @return R a new relative instance of the DoubleScalar of the right type 66 */ 67 public abstract R instantiateRel(double value, RU unit); 68 69 /** 70 * Increment the value by the supplied value and return the result using the current unit of the Absolute value. 71 * @param increment R, a relative typed DoubleScalar; amount by which the value is incremented 72 * @return Absolute DoubleScalar 73 */ 74 public final A plus(final R increment) 75 { 76 AU targetUnit = getUnit(); 77 return instantiateAbs(getInUnit() + increment.getInUnit(targetUnit.getRelativeUnit()), targetUnit); 78 } 79 80 /** 81 * Decrement the value by the supplied value and return the result using the current unit of the Absolute value. 82 * @param decrement R, a relative typed DoubleScalar; amount by which the value is decremented 83 * @return Absolute DoubleScalar 84 */ 85 public final A minus(final R decrement) 86 { 87 AU targetUnit = getUnit(); 88 return instantiateAbs(getInUnit() - decrement.getInUnit(targetUnit.getRelativeUnit()), targetUnit); 89 } 90 91 /** 92 * Decrement the value by the supplied value and return the result using the relative unit belonging to the unit of the 93 * Absolute value. 94 * @param decrement A, an absolute typed DoubleScalar; amount by which the value is decremented 95 * @return Relative DoubleScalar 96 */ 97 public final R minus(final A decrement) 98 { 99 RU targetUnit = getUnit().getRelativeUnit(); 100 return instantiateRel(getInUnit() - decrement.getInUnit(getUnit()), targetUnit); 101 } 102 103 /** 104 * Interpolate between two values. 105 * @param zero Duration; the low value 106 * @param one Duration; the high value 107 * @param ratio double; the ratio between 0 and 1, inclusive 108 * @return a Scalar at the ratio between 109 * @param <AU> the absolute unit 110 * @param <A> the Absolute class for reference purposes 111 * @param <RU> the relative unit 112 * @param <R> the Relative class for reference purposes 113 */ 114 public static <AU extends AbsoluteLinearUnit<AU, RU>, A extends AbstractDoubleScalarAbs<AU, A, RU, R>, RU extends Unit<RU>, 115 R extends AbstractDoubleScalarRel<RU, R>> A interpolate(final A zero, final A one, final double ratio) 116 { 117 return zero.instantiateAbs(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit()); 118 } 119 120 /**********************************************************************************/ 121 /********************************** MATH METHODS **********************************/ 122 /**********************************************************************************/ 123 124 /** {@inheritDoc} */ 125 @Override 126 @SuppressWarnings("checkstyle:designforextension") 127 public A ceil() 128 { 129 return instantiateAbs(Math.ceil(getInUnit()), getUnit()); 130 } 131 132 /** {@inheritDoc} */ 133 @Override 134 @SuppressWarnings("checkstyle:designforextension") 135 public A floor() 136 { 137 return instantiateAbs(Math.floor(getInUnit()), getUnit()); 138 } 139 140 /** {@inheritDoc} */ 141 @Override 142 @SuppressWarnings("checkstyle:designforextension") 143 public A rint() 144 { 145 return instantiateAbs(Math.rint(getInUnit()), getUnit()); 146 } 147 148 /** {@inheritDoc} */ 149 @Override 150 @SuppressWarnings("checkstyle:designforextension") 151 public A round() 152 { 153 return instantiateAbs(Math.round(getInUnit()), getUnit()); 154 } 155 156 /** {@inheritDoc} */ 157 @Override 158 @SuppressWarnings("checkstyle:designforextension") 159 public A multiplyBy(final double constant) 160 { 161 return instantiateAbs(getInUnit() * constant, getUnit()); 162 } 163 164 /** {@inheritDoc} */ 165 @Override 166 @SuppressWarnings("checkstyle:designforextension") 167 public A divideBy(final double constant) 168 { 169 return instantiateAbs(getInUnit() / constant, getUnit()); 170 } 171 172 }