1 package org.djunits.unit; 2 3 import org.djunits.unit.scale.LinearScale; 4 import org.djunits.unit.scale.Scale; 5 import org.djunits.unit.scale.StandardScale; 6 import org.djunits.unit.unitsystem.UnitSystem; 7 8 /** 9 * A linear unit with easy-access constructor with a linear factor, and access to the linear factor. <br> 10 * A linear unit is a unit that is linearly related to the SI standard unit. E.g. Mile is linearly related to meter (the SI unit 11 * for length). Unlike temperature in degrees Celsius which is <strong>not</strong> linearly related to the Kelvin (the SI unit 12 * for temperature). 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://opentrafficsim.org/docs/license.html">OpenTrafficSim 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 11, 2015 <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 * @param <U> the linear Unit. 22 */ 23 public abstract class LinearUnit<U extends LinearUnit<U>> extends Unit<U> 24 { 25 /** */ 26 private static final long serialVersionUID = 20151011L; 27 28 /** 29 * Build a standard linear unit and create the fields for a unit. If the parameter standardUnit is true, it is a standard 30 * unit where name is the nameKey, and abbreviation is the abbreviationKey; if false, this unit is a user-defined unit where 31 * the localization files do not have an entry. If standardUnit is true, a UnitException is silently ignored; if 32 * standardUnit is false a UnitException is thrown as a RunTimeException. 33 * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself 34 * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit, 35 * otherwise the abbreviation itself 36 * @param unitSystem the unit system, e.g. SI or Imperial 37 * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit 38 */ 39 public LinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, 40 final boolean standardUnit) 41 { 42 super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, standardUnit); 43 } 44 45 /** 46 * Build a unit with a linear conversion factor to another unit. If the parameter standardUnit is true, it is a standard 47 * unit where name is the nameKey, and abbreviation is the abbreviationKey; if false, this unit is a user-defined unit where 48 * the localization files do not have an entry. If standardUnit is true, a UnitException is silently ignored; if 49 * standardUnit is false a UnitException is thrown as a RunTimeException. 50 * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself 51 * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit, 52 * otherwise the abbreviation itself 53 * @param unitSystem the unit system, e.g. SI or Imperial 54 * @param referenceUnit the unit to convert to 55 * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit 56 * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit 57 */ 58 protected LinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, 59 final U referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit) 60 { 61 super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, 62 referenceUnit == null ? StandardScale.SCALE 63 : new LinearScale( 64 referenceUnit.getScale().getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit), 65 standardUnit); 66 } 67 68 /** 69 * Build a unit with a specific conversion scale to/from the standard unit. If the parameter standardUnit is true, it is a 70 * standard unit where name is the nameKey, and abbreviation is the abbreviationKey; if false, this unit is a user-defined 71 * unit where the localization files do not have an entry. If standardUnit is true, a UnitException is silently ignored; if 72 * standardUnit is false a UnitException is thrown as a RunTimeException. 73 * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself 74 * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit, 75 * otherwise the abbreviation itself 76 * @param unitSystem the unit system, e.g. SI or Imperial 77 * @param scale the conversion scale to use for this unit 78 * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit 79 */ 80 protected LinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, 81 final Scale scale, final boolean standardUnit) 82 { 83 super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, scale, standardUnit); 84 } 85 86 /** {@inheritDoc} */ 87 @Override 88 @SuppressWarnings("checkstyle:designforextension") 89 public LinearScale getScale() 90 { 91 return (LinearScale) super.getScale(); 92 } 93 94 /** 95 * @return the conversion factor to the standard unit (e.g., the SI unit) 96 */ 97 public final double getScaleFactor() 98 { 99 return getScale().getConversionFactorToStandardUnit(); 100 } 101 102 /** {@inheritDoc} */ 103 @Override 104 public int hashCode() 105 { 106 final int prime = 31; 107 int result = super.hashCode(); 108 long temp; 109 temp = Double.doubleToLongBits(this.getScaleFactor()); 110 result = prime * result + (int) (temp ^ (temp >>> 32)); 111 return result; 112 } 113 114 /** {@inheritDoc} */ 115 @SuppressWarnings("checkstyle:needbraces") 116 @Override 117 public boolean equals(final Object obj) 118 { 119 if (this == obj) 120 return true; 121 if (!super.equals(obj)) 122 return false; 123 if (getClass() != obj.getClass()) 124 return false; 125 LinearUnit<?> other = (LinearUnit<?>) obj; 126 if (Double.doubleToLongBits(this.getScaleFactor()) != Double.doubleToLongBits(other.getScaleFactor())) 127 return false; 128 return true; 129 } 130 131 /** {@inheritDoc} */ 132 @SuppressWarnings("checkstyle:needbraces") 133 @Override 134 public boolean equalsIgnoreNaming(final Object obj) 135 { 136 if (this == obj) 137 return true; 138 if (getClass() != obj.getClass()) 139 return false; 140 LinearUnit<?> other = (LinearUnit<?>) obj; 141 if (Double.doubleToLongBits(getScaleFactor()) != Double.doubleToLongBits(other.getScaleFactor())) 142 return false; 143 return true; 144 } 145 146 147 }