package cgeo.geocaching.location;

import cgeo.geocaching.settings.Settings;

import org.apache.commons.lang3.tuple.ImmutablePair;

import java.util.Locale;

public class Units {

    private Units() {
        // utility class
    }

    public static ImmutablePair<Double, String> scaleDistance(final double distanceKilometers) {
        double distance;
        final String units;
        if (Settings.useImperialUnits()) {
            distance = distanceKilometers / IConversion.MILES_TO_KILOMETER;
            if (distance >= 0.1) {
                units = "mi";
            } else {
                distance *= 5280;
                units = "ft";
            }
        } else if (distanceKilometers >= 1) {
            distance = distanceKilometers;
            units = "km";
        } else {
            distance = distanceKilometers * 1000;
            units = "m";
        }
        return new ImmutablePair<>(distance, units);
    }

    public static String getDistanceFromKilometers(final Float distanceKilometers) {
        if (distanceKilometers == null) {
            return "?";
        }

        final ImmutablePair<Double, String> scaled = scaleDistance(distanceKilometers);
        final String formatString;
        if (scaled.left >= 100) {
            formatString = "%.0f %s";
        } else if (scaled.left >= 10) {
            formatString = "%.1f %s";
        } else {
            formatString = "%.2f %s";
        }

        return String.format(formatString, scaled.left, scaled.right);
    }

    public static String getDistanceFromMeters(final float meters) {
        return getDistanceFromKilometers(meters / 1000f);
    }

    public static String getSpeed(final float kilometersPerHour) {
        if (Settings.useImperialUnits()) {
            return String.format(Locale.US, "%.0f mph", kilometersPerHour / IConversion.MILES_TO_KILOMETER);
        }
        return String.format(Locale.US, "%.0f km/h", kilometersPerHour);
    }
}

@@ -328,61 +328,8 @@
 ) %7B%0A
-        double distance;%0A        final String units;%0A
     
@@ -375,32 +375,45 @@
             
+final double 
 distance
  = distanceK
@@ -392,32 +392,37 @@
  double distance
+Miles
  = distanceKilom
@@ -485,16 +485,21 @@
 distance
+Miles
  %3E= 0.1)
@@ -521,20 +521,55 @@
     
-units =
+return new ImmutablePair%3C%3E(distanceMiles,
  %22mi%22
+)
 ;%0A  
@@ -571,39 +571,32 @@
 );%0A            %7D
- else %7B
 %0A               
@@ -592,28 +592,51 @@
         
-    
+return new ImmutablePair%3C%3E(
 distance
  *= 5280
@@ -631,61 +631,28 @@
 ance
+Miles
  *
-=
  5280
-;%0A                units = %22ft%22;%0A            %7D
+, %22ft%22);
 %0A   
@@ -710,63 +710,60 @@
     
-distance = distanceKilometers;%0A            units =
+return new ImmutablePair%3C%3E(distanceKilometers,
  %22km%22
+)
 ;%0A  
@@ -793,19 +793,35 @@
     
-distance = 
+return new ImmutablePair%3C%3E(
 dist
@@ -845,33 +845,14 @@
 1000
-;%0A            units =
+,
  %22m%22
+)
 ;%0A  
@@ -863,61 +863,8 @@
   %7D%0A
-        return new ImmutablePair%3C%3E(distance, units);%0A
     

fix checkstyle violation