1 package org.djunits.value.vfloat.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.vfloat.FloatMathFunctions; 9 10 /** 11 * The typed, abstract FloatScalarAbs class that forms the basis of all FloatScalar 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-2018 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> Absolute unit 23 * @param <RU> Relative unit 24 * @param <A> the absolute class for reference 25 * @param <R> the relative class for reference 26 */ 27 public abstract class AbstractFloatScalarAbs<AU extends AbsoluteLinearUnit<AU, RU>, 28 A extends AbstractFloatScalarAbs<AU, A, RU, R>, RU extends Unit<RU>, R extends AbstractFloatScalarRel<RU, R>> 29 extends AbstractFloatScalar<AU, A> implements Absolute, MathFunctionsAbs<A>, FloatMathFunctions<A> 30 { 31 /** */ 32 private static final long serialVersionUID = 20150626L; 33 34 /** 35 * Construct a new Absolute Immutable FloatScalar. 36 * @param value float; the value of the new Absolute Immutable FloatScalar 37 * @param unit U; the unit of the new Absolute Immutable FloatScalar 38 */ 39 public AbstractFloatScalarAbs(final float value, final AU unit) 40 { 41 super(unit, unit.isBaseSIUnit() ? value : (float) ValueUtil.expressAsSIUnit(value, unit)); 42 } 43 44 /** 45 * Construct a new Absolute Immutable FloatScalar from an existing Absolute Immutable FloatScalar. 46 * @param value A, an absolute typed FloatScalar; the reference 47 */ 48 public AbstractFloatScalarAbs(final A value) 49 { 50 super(value.getUnit(), value.si); 51 } 52 53 /** 54 * Construct a new Absolute Immutable FloatScalar of the right type. Each extending class must implement this method. 55 * @param value the float value 56 * @param unit the unit 57 * @return A a new absolute instance of the FloatScalar of the right type 58 */ 59 public abstract A instantiateAbs(float value, AU unit); 60 61 /** 62 * Construct a new Relative Immutable FloatScalar of the right type. Each extending class must implement this method. 63 * @param value the float value 64 * @param unit the unit 65 * @return R a new relative instance of the FloatScalar of the right type 66 */ 67 public abstract R instantiateRel(float 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 FloatScalar; amount by which the value is incremented 72 * @return Absolute FloatScalar 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 FloatScalar; amount by which the value is decremented 83 * @return Absolute FloatScalar 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 FloatScalar; amount by which the value is decremented 95 * @return Relative FloatScalar 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 /********************************** MATH METHODS **********************************/ 105 /**********************************************************************************/ 106 107 /** {@inheritDoc} */ 108 @Override 109 @SuppressWarnings("checkstyle:designforextension") 110 public A ceil() 111 { 112 return instantiateAbs((float) Math.ceil(getInUnit()), getUnit()); 113 } 114 115 /** {@inheritDoc} */ 116 @Override 117 @SuppressWarnings("checkstyle:designforextension") 118 public A floor() 119 { 120 return instantiateAbs((float) Math.floor(getInUnit()), getUnit()); 121 } 122 123 /** {@inheritDoc} */ 124 @Override 125 @SuppressWarnings("checkstyle:designforextension") 126 public A rint() 127 { 128 return instantiateAbs((float) Math.rint(getInUnit()), getUnit()); 129 } 130 131 /** {@inheritDoc} */ 132 @Override 133 @SuppressWarnings("checkstyle:designforextension") 134 public A round() 135 { 136 return instantiateAbs(Math.round(getInUnit()), getUnit()); 137 } 138 139 /** {@inheritDoc} */ 140 @Override 141 @SuppressWarnings("checkstyle:designforextension") 142 public A multiplyBy(final float constant) 143 { 144 return instantiateAbs(getInUnit() * constant, getUnit()); 145 } 146 147 /** {@inheritDoc} */ 148 @Override 149 @SuppressWarnings("checkstyle:designforextension") 150 public A divideBy(final float constant) 151 { 152 return instantiateAbs(getInUnit() / constant, getUnit()); 153 } 154 155 }