1 package org.djunits.value.vdouble.scalar; 2 3 import org.djunits.unit.DimensionlessUnit; 4 import org.djunits.unit.MoneyPerAreaUnit; 5 import org.djunits.unit.MoneyPerDurationUnit; 6 import org.djunits.unit.MoneyPerEnergyUnit; 7 import org.djunits.unit.MoneyPerLengthUnit; 8 import org.djunits.unit.MoneyPerMassUnit; 9 import org.djunits.unit.MoneyPerVolumeUnit; 10 import org.djunits.unit.MoneyUnit; 11 12 /** 13 * Easy access methods for the Money DoubleScalar, which is relative by definition. Instead of: 14 * 15 * <pre> 16 * DoubleScalar.Rel<MoneyUnit> value = new DoubleScalar.Rel<MoneyUnit>(100.0, MoneyUnit.SI); 17 * </pre> 18 * 19 * we can now write: 20 * 21 * <pre> 22 * Money value = new Money(100.0, MoneyUnit.SI); 23 * </pre> 24 * 25 * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit 26 * used are compatible. 27 * <p> 28 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 29 * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>. 30 * <p> 31 * $LastChangedDate: 2019-02-27 23:44:43 +0100 (Wed, 27 Feb 2019) $, @version $Revision: 333 $, by $Author: averbraeck $, 32 * initial version Sep 5, 2015 <br> 33 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 34 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 35 */ 36 public class Money extends AbstractDoubleScalarRel<MoneyUnit, Money> 37 { 38 /** */ 39 private static final long serialVersionUID = 20150905L; 40 41 /** 42 * Construct Money scalar. 43 * @param value double value 44 * @param unit unit for the double value 45 */ 46 public Money(final double value, final MoneyUnit unit) 47 { 48 super(value, unit); 49 } 50 51 /** 52 * Construct Money scalar. 53 * @param value Scalar from which to construct this instance 54 */ 55 public Money(final Money value) 56 { 57 super(value); 58 } 59 60 /** {@inheritDoc} */ 61 @Override 62 public final Money instantiateRel(final double value, final MoneyUnit unit) 63 { 64 return new Money(value, unit); 65 } 66 67 /** 68 * Interpolate between two values. 69 * @param zero the low value 70 * @param one the high value 71 * @param ratio the ratio between 0 and 1, inclusive 72 * @return a Scalar at the ratio between 73 */ 74 public static Money interpolate(final Money zero, final Money one, final double ratio) 75 { 76 return new Money(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit()); 77 } 78 79 /** 80 * Return the maximum value of two monetary scalars. 81 * @param r1 the first scalar 82 * @param r2 the second scalar 83 * @return the maximum value of two monetary scalars 84 */ 85 public static Money max(final Money r1, final Money r2) 86 { 87 return (r1.gt(r2)) ? r1 : r2; 88 } 89 90 /** 91 * Return the maximum value of more than two monetary scalars. 92 * @param r1 the first scalar 93 * @param r2 the second scalar 94 * @param rn the other scalars 95 * @return the maximum value of more than two monetary scalars 96 */ 97 public static Money max(final Money r1, final Money r2, final Money... rn) 98 { 99 Money maxr = (r1.gt(r2)) ? r1 : r2; 100 for (Money r : rn) 101 { 102 if (r.gt(maxr)) 103 { 104 maxr = r; 105 } 106 } 107 return maxr; 108 } 109 110 /** 111 * Return the minimum value of two monetary scalars. 112 * @param r1 the first scalar 113 * @param r2 the second scalar 114 * @return the minimum value of two monetary scalars 115 */ 116 public static Money min(final Money r1, final Money r2) 117 { 118 return (r1.lt(r2)) ? r1 : r2; 119 } 120 121 /** 122 * Return the minimum value of more than two monetary scalars. 123 * @param r1 the first scalar 124 * @param r2 the second scalar 125 * @param rn the other scalars 126 * @return the minimum value of more than two monetary scalars 127 */ 128 public static Money min(final Money r1, final Money r2, final Money... rn) 129 { 130 Money minr = (r1.lt(r2)) ? r1 : r2; 131 for (Money r : rn) 132 { 133 if (r.lt(minr)) 134 { 135 minr = r; 136 } 137 } 138 return minr; 139 } 140 141 /** 142 * Calculate the division of Money and Money, which results in a Dimensionless scalar. 143 * @param v Money scalar 144 * @return Dimensionless scalar as a division of Money and Money 145 */ 146 public final Dimensionless divideBy(final Money v) 147 { 148 return new Dimensionless(this.si / v.si, DimensionlessUnit.SI); 149 } 150 151 /** 152 * Calculate the division of Money and Area, which results in a MoneyPerArea scalar. 153 * @param v Money scalar 154 * @return MoneyPerArea scalar as a division of Money and Area 155 */ 156 public final MoneyPerArea divideBy(final Area v) 157 { 158 return new MoneyPerArea(this.si / v.si, MoneyPerAreaUnit.getStandardMoneyPerAreaUnit()); 159 } 160 161 /** 162 * Calculate the division of Money and Energy, which results in a MoneyPerEnergy scalar. 163 * @param v Money scalar 164 * @return MoneyPerEnergy scalar as a division of Money and Energy 165 */ 166 public final MoneyPerEnergy divideBy(final Energy v) 167 { 168 return new MoneyPerEnergy(this.si / v.si, MoneyPerEnergyUnit.getStandardMoneyPerEnergyUnit()); 169 } 170 171 /** 172 * Calculate the division of Money and Length, which results in a MoneyPerLength scalar. 173 * @param v Money scalar 174 * @return MoneyPerLength scalar as a division of Money and Length 175 */ 176 public final MoneyPerLength divideBy(final Length v) 177 { 178 return new MoneyPerLength(this.si / v.si, MoneyPerLengthUnit.getStandardMoneyPerLengthUnit()); 179 } 180 181 /** 182 * Calculate the multiplication of Money and LinearDensity, which results in a MoneyPerLength scalar. 183 * @param v Money scalar 184 * @return MoneyPerLength scalar as a multiplication of Money and LinearDensity 185 */ 186 public final MoneyPerLength multiplyBy(final LinearDensity v) 187 { 188 return new MoneyPerLength(this.si * v.si, MoneyPerLengthUnit.getStandardMoneyPerLengthUnit()); 189 } 190 191 /** 192 * Calculate the division of Money and Mass, which results in a MoneyPerMass scalar. 193 * @param v Money scalar 194 * @return MoneyPerMass scalar as a division of Money and Mass 195 */ 196 public final MoneyPerMass divideBy(final Mass v) 197 { 198 return new MoneyPerMass(this.si / v.si, MoneyPerMassUnit.getStandardMoneyPerMassUnit()); 199 } 200 201 /** 202 * Calculate the division of Money and Duration, which results in a MoneyPerDuration scalar. 203 * @param v Money scalar 204 * @return MoneyPerDuration scalar as a division of Money and Duration 205 */ 206 public final MoneyPerDuration divideBy(final Duration v) 207 { 208 return new MoneyPerDuration(this.si / v.si, MoneyPerDurationUnit.getStandardMoneyPerDurationUnit()); 209 } 210 211 /** 212 * Calculate the multiplication of Money and Frequency, which results in a MoneyPerDuration scalar. 213 * @param v Money scalar 214 * @return MoneyPerDuration scalar as a multiplication of Money and Frequency 215 */ 216 public final MoneyPerDuration multiplyBy(final Frequency v) 217 { 218 return new MoneyPerDuration(this.si * v.si, MoneyPerDurationUnit.getStandardMoneyPerDurationUnit()); 219 } 220 221 /** 222 * Calculate the division of Money and Volume, which results in a MoneyPerVolume scalar. 223 * @param v Money scalar 224 * @return MoneyPerVolume scalar as a division of Money and Volume 225 */ 226 public final MoneyPerVolume divideBy(final Volume v) 227 { 228 return new MoneyPerVolume(this.si / v.si, MoneyPerVolumeUnit.getStandardMoneyPerVolumeUnit()); 229 } 230 231 }