UnitOfDistance – Measure and Convert your distance
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
/******************************************************************************* * Copyright (c) 2017 MyMiller Consulting LLC. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package name.mymiller.extensions.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 /** * 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()); } /** * 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; } /** * * @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; } } |