LinearUnit.java

package org.djunits.unit;

import org.djunits.unit.scale.LinearScale;
import org.djunits.unit.scale.Scale;
import org.djunits.unit.scale.StandardScale;
import org.djunits.unit.unitsystem.UnitSystem;

/**
 * A linear unit with easy-access constructor with a linear factor, and access to the linear factor. <br>
 * 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
 * for length). Unlike temperature in degrees Celsius which is <strong>not</strong> linearly related to the Kelvin (the SI unit
 * for temperature).
 * <p>
 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
 * </p>
 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
 * initial version Oct 11, 2015 <br>
 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
 * @param <U> the linear Unit.
 */
public abstract class LinearUnit<U extends LinearUnit<U>> extends Unit<U>
{
    /** */
    private static final long serialVersionUID = 20151011L;

    /**
     * Build a standard linear unit and create the fields for a unit. If the parameter standardUnit is true, it is a standard
     * unit where name is the nameKey, and abbreviation is the abbreviationKey; if false, this unit is a user-defined unit where
     * the localization files do not have an entry. If standardUnit is true, a UnitException is silently ignored; if
     * standardUnit is false a UnitException is thrown as a RunTimeException.
     * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
     *            name itself
     * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
     *            unit, otherwise the abbreviation itself
     * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
     * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
     *            unit
     */
    public LinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
            final boolean standardUnit)
    {
        super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, standardUnit);
    }

    /**
     * Build a unit with a linear conversion factor to another unit. If the parameter standardUnit is true, it is a standard
     * unit where name is the nameKey, and abbreviation is the abbreviationKey; if false, this unit is a user-defined unit where
     * the localization files do not have an entry. If standardUnit is true, a UnitException is silently ignored; if
     * standardUnit is false a UnitException is thrown as a RunTimeException.
     * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
     *            name itself
     * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
     *            unit, otherwise the abbreviation itself
     * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
     * @param referenceUnit U; the unit to convert to
     * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
     *            unit
     * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
     *            unit
     */
    protected LinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
            final U referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
    {
        super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem,
                referenceUnit == null ? StandardScale.SCALE
                        : new LinearScale(
                                referenceUnit.getScale().getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit),
                standardUnit);
    }

    /**
     * Build a unit with a specific conversion scale to/from the standard unit. If the parameter standardUnit is true, it is a
     * standard unit where name is the nameKey, and abbreviation is the abbreviationKey; if false, this unit is a user-defined
     * unit where the localization files do not have an entry. If standardUnit is true, a UnitException is silently ignored; if
     * standardUnit is false a UnitException is thrown as a RunTimeException.
     * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
     *            name itself
     * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
     *            unit, otherwise the abbreviation itself
     * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
     * @param scale Scale; the conversion scale to use for this unit
     * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
     *            unit
     */
    protected LinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
            final Scale scale, final boolean standardUnit)
    {
        super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, scale, standardUnit);
    }

    /** {@inheritDoc} */
    @Override
    @SuppressWarnings("checkstyle:designforextension")
    public LinearScale getScale()
    {
        return (LinearScale) super.getScale();
    }

    /**
     * @return the conversion factor to the standard unit (e.g., the SI unit)
     */
    public final double getScaleFactor()
    {
        return getScale().getConversionFactorToStandardUnit();
    }

    /** {@inheritDoc} */
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = super.hashCode();
        long temp;
        temp = Double.doubleToLongBits(this.getScaleFactor());
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    /** {@inheritDoc} */
    @SuppressWarnings("checkstyle:needbraces")
    @Override
    public boolean equals(final Object obj)
    {
        if (this == obj)
            return true;
        if (!super.equals(obj))
            return false;
        if (getClass() != obj.getClass())
            return false;
        LinearUnit<?> other = (LinearUnit<?>) obj;
        if (Double.doubleToLongBits(this.getScaleFactor()) != Double.doubleToLongBits(other.getScaleFactor()))
            return false;
        return true;
    }

    /** {@inheritDoc} */
    @SuppressWarnings("checkstyle:needbraces")
    @Override
    public boolean equalsIgnoreNaming(final Object obj)
    {
        if (this == obj)
            return true;
        if (getClass() != obj.getClass())
            return false;
        LinearUnit<?> other = (LinearUnit<?>) obj;
        if (Double.doubleToLongBits(getScaleFactor()) != Double.doubleToLongBits(other.getScaleFactor()))
            return false;
        return true;
    }

}