Fri. Mar 29th, 2024
UnitOfDistance

Well I was bored one night after creating the UnitOfMemory and decided what else could be useful in the future.  I came up with a UnitOfDistance to convert distances over land.  Miles v’s kilometers and everything in between.  The class was easy to come up with, finding the exact conversion rates however was a different story.  The class works by basing everything on meters.  A nice conversion method from converting from one measurement type to another.

If the precision level is not high enough for what you need, simply modify their values in the Enum list to what you do need.

 

package name.mymiller.lang;

import java.io.Serializable;

/**
 * @author jmiller Defined UnitOfMeasures
 */

public enum UnitOfDistance implements Serializable {
    // @formatter:off
    /**
     * Standard U.S. Miles
     */
    Miles("Miles", 1609.34, 0.000621371),
    /**
     * Feet
     */
    Feet("Feet", 3.28084, 0.3048),
    /**
     * Inches
     */
    Inches("Inches", 39.3701, 0.0254),
    /**
     * Nautical Miles as defined at the equator
     */
    Nautical_Miles("Nautical Miles", 1852.0, 0.000539957),
    /**
     * Metric Based
     */
    Kilometers("Kilometers", 1000.0, 0.001),
    /**
     * Metric Based
     */
    Meters("Meters", 1.0, 1.0),
    /**
     * Metric Based
     */
    Centimeters("Centimeters", 100.0, 0.01),
    /**
     * Metric Based
     */
    Millimeters("Millimeters", 1000.0, 0.001);
    // @formatter:on

    /**
     * Field to hold the conversion rate of unit to meters
     */
    private double conversionRateToMeters = 0.0;
    /**
     * Field to hold the conversion rate from meters to unit
     */
    private double conversionRateFromMeters = 0.0;
    /**
     * Label to use with unit of measure
     */
    private String label = null;

    /**
     * Constructor for this enum to set values.
     *
     * @param label                    Label to use with the unit of measure
     * @param conversionRateFromMeters Field to hold the conversion rate from meters
     *                                 to unit
     * @param conversionRateToMeters   Field to hold the conversion rate of unit to
     *                                 meters
     */
    UnitOfDistance(final String label, final Double conversionRateFromMeters, final double conversionRateToMeters) {
        this.label = label;
        this.conversionRateToMeters = conversionRateToMeters;
        this.conversionRateFromMeters = conversionRateFromMeters;
    }

    /**
     * Convert a measurement from one type to another
     *
     * @param convertTo   UnitOfDistance to convert measurement to.
     * @param convertFrom UnitOfDistance measurement is in.
     * @param distance    Distance to convert
     * @return Double indicating the distance in the new UnitOfDistance
     */
    public static double convert(UnitOfDistance convertTo, UnitOfDistance convertFrom, double distance) {
        return convertTo.getConversionRateFromMeters() * (distance * convertFrom.getConversionRateToMeters());
    }

    /**
     * @return Conversion Rate To Meters
     */
    public double getConversionRateFromMeters() {
        return this.conversionRateFromMeters;
    }

    /**
     * @return Conversion Rate from Meters
     */
    public double getConversionRateToMeters() {
        return this.conversionRateToMeters;
    }

    /**
     * @return Name given for this unit of measure.
     */
    public String getLabel() {
        return this.label;
    }
}

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

One thought on “UnitOfDistance – Measure and Convert your distance”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d