I am developing an application in which the user can use different measurement systems. Currently, there is support for the metric and imperial measurement systems. To support these systems, I wrote a converter. Could you take a look at its code and tell how can it be improved.
public class SystemsMeasuresConverter {
private Context context;
private Profile profile;
public SystemsMeasuresConverter(Context context, Profile profile) {
this.context = context;
this.profile = profile;
}
/**
* Converts mass from some units to some units
*
* @param input mass in some units
* @return mass some units
*/
public Number convertToMassUnit(Number input, SystemsMeasures from, SystemsMeasures to) {
double constant = 1;
switch (from) {
case Imperial:
if (to == Metric) {
constant = 0.455;
}
break;
case Metric:
if (to == Imperial) {
constant = 2.2;
}
break;
}
return MathUtil.round(input.doubleValue() * constant, MathUtil.GLOBAL_SCALE);
}
/**
* Converts mass from some units to units selected in profile
*
* @param input mass in some units
* @return mass in units of measurements selected in the user profile
*/
public Number convertToMassUnit(Number input, SystemsMeasures from) {
return convertToMassUnit(input, from, profile.getSystemMeasures());
}
/**
* Converts mass from kilograms to units selected in profile
*
* @param input metric system
* @return mass in units of measurements selected in the user profile
*/
public Number convertToMassUnit(Number input) {
return convertToMassUnit(input, Metric, profile.getSystemMeasures());
}
/**
* Converts distance from units selected in profile to kilograms
*
* @param input metric system
* @return distance in units of measurements selected in the user profile
*/
public Number convertToKg(Number input) {
return convertToMassUnit(input, profile.getSystemMeasures(), SystemsMeasures.Metric);
}
/**
* Returns abbreviations for mass units selected in profile
*
* @return abbreviations for mass units selected in profile
*/
public String getStringFromCurrentMassUnit() {
SystemsMeasures unit = profile.getSystemMeasures();
switch (unit) {
case Metric:
return context.getString(R.string.kilogramsMassUnit);
case Imperial:
return context.getString(R.string.poundsMassUnit);
}
throw new RuntimeException("Cannot find Mass Unit");
}
/**
* Converts distance from some units to some units
*
* @param input mass in some units
* @return mass some units
*/
public Number convertToDistanceUnits(Number input, SystemsMeasures from, SystemsMeasures to) {
double constant = 1;
switch (from) {
case Imperial:
if (to == Metric) {
constant = 2.54;
}
break;
case Metric:
if (to == Imperial) {
constant = 0.394;
}
break;
}
return MathUtil.round(input.doubleValue() * constant, MathUtil.GLOBAL_SCALE);
}
/**
* Converts distance from some units to units selected in profile
*
* @param input distance in some units
* @return distance in units of measurements selected in the user profile
*/
public Number convertToDistanceUnits(Number input, SystemsMeasures from) {
return convertToDistanceUnits(input, from, profile.getSystemMeasures());
}
/**
* Converts distance from meters to units selected in profile
*
* @param input metric system
* @return distance in units of measurements selected in the user profile
*/
public Number convertToDistanceUnits(Number input) {
return convertToDistanceUnits(input, Metric, profile.getSystemMeasures());
}
/**
* Converts distance from units selected in profile to meters
*
* @param input metric system
* @return distance in units of measurements selected in the user profile
*/
public Number convertToMeter(Number input) {
return convertToDistanceUnits(input, profile.getSystemMeasures(), SystemsMeasures.Metric);
}
/**
* Returns abbreviations for distance units selected in profile
*
* @return abbreviations for distance units selected in profile
*/
public String getStringFromCurrentDistanceUnit() {
SystemsMeasures unit = profile.getSystemMeasures();
switch (unit) {
case Metric:
return context.getString(R.string.meters_distance_unit);
case Imperial:
return context.getString(R.string.feet_distance_unit);
}
throw new RuntimeException("Cannot find Mass Unit");
}
}
SystemsMeasures
public enum SystemsMeasures {
Metric(0),
Imperial(1);
private final int value;
SystemsMeasures(int value) {
this.value = value;
}
public int getValue(){
return value;
}
public static SystemsMeasures getById(int id) {
for (SystemsMeasures e : values()) {
if (e.value == id) return e;
}
return Metric;
}
}
context
andprofile
are package private. \$\endgroup\$getById()
could technically be improved by storing the enums in aHashMap
or something like this additionally but I don't think there are performance improvements as thete ate only two elements. \$\endgroup\$