1 package tim.prune.function.estimate;
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5 import java.util.Locale;
7 import tim.prune.I18nManager;
8 import tim.prune.config.Config;
9 import tim.prune.data.RangeStats;
10 import tim.prune.data.Unit;
11 import tim.prune.data.UnitSetLibrary;
14 * Class to hold, parse and convert the parameters for time estimation.
15 * These are five (metric) values which can be loaded and saved from config,
16 * and are used by the EstimateTime function
18 public class EstimationParameters
20 /** Minutes required for flat travel, fixed metric distance */
21 private double _flatMins = 0.0;
22 /** Minutes required for climbing, fixed metric distance */
23 private double _gentleClimbMins = 0.0, _steepClimbMins;
24 /** Minutes required for descending, fixed metric distance */
25 private double _gentleDescentMins = 0.0, _steepDescentMins;
26 /** True if parsing from a string failed */
27 private boolean _parseFailed = false;
29 /** Kilometres unit for comparison */
30 private static final Unit KILOMETRES = UnitSetLibrary.UNITS_KILOMETRES;
34 * Bare constructor using default values
36 public EstimationParameters()
42 * Constructor from config string
43 * @param inConfigString single, semicolon-separated string from config
45 public EstimationParameters(String inConfigString)
47 populateWithString(inConfigString);
54 * Reset all the values to their hardcoded defaults
56 public void resetToDefaults()
59 _gentleClimbMins = 12.0; _steepClimbMins = 18.0;
60 _gentleDescentMins = 0.0; _steepDescentMins = 12.0;
65 * Populate the values from the config, which means all values are metric
66 * @param inString semicolon-separated string of five parameters
68 private void populateWithString(String inString)
70 if (inString != null && !inString.trim().equals(""))
72 String[] params = inString.trim().split(";");
73 _parseFailed = (params == null || params.length != 5);
76 for (String p : params)
78 if (!isParamStringValid(p)) {
87 // Use fixed UK locale to parse these, because of fixed "." formatting
88 NumberFormat twoDpFormatter = NumberFormat.getNumberInstance(Locale.UK);
89 _flatMins = twoDpFormatter.parse(params[0]).doubleValue();
90 _gentleClimbMins = twoDpFormatter.parse(params[1]).doubleValue();
91 _steepClimbMins = twoDpFormatter.parse(params[2]).doubleValue();
92 _gentleDescentMins = twoDpFormatter.parse(params[3]).doubleValue();
93 _steepDescentMins = twoDpFormatter.parse(params[4]).doubleValue();
100 else _parseFailed = true;
104 * Populate the values using five user-entered strings (now Units-specific!)
105 * @param inFlat minutes for flat
106 * @param inGClimb minutes for gentle climb
107 * @param inSClimb minutes for steep climb
108 * @param inGDescent minutes for gentle descent
109 * @param inSDescent minutes for steep descent
111 public void populateWithStrings(String inFlat, String inGClimb, String inSClimb, String inGDescent, String inSDescent)
113 if (isParamStringValid(inFlat) && isParamStringValid(inGClimb) && isParamStringValid(inSClimb)
114 && isParamStringValid(inGDescent) && isParamStringValid(inSDescent))
116 Unit distUnit = Config.getUnitSet().getDistanceUnit();
117 Unit altUnit = Config.getUnitSet().getAltitudeUnit();
118 final double distFactor = (distUnit == KILOMETRES ? 1.0 : (5000/3.0 * distUnit.getMultFactorFromStd()));
119 final double altFactor = (altUnit.isStandard() ? 1.0 : (1.0/3.0 * altUnit.getMultFactorFromStd()));
120 NumberFormat localFormatter = NumberFormat.getNumberInstance();
123 _flatMins = localFormatter.parse(inFlat).doubleValue() * distFactor;
124 _gentleClimbMins = localFormatter.parse(inGClimb).doubleValue() * altFactor;
125 _steepClimbMins = localFormatter.parse(inSClimb).doubleValue() * altFactor;
126 _gentleDescentMins = localFormatter.parse(inGDescent).doubleValue() * altFactor;
127 _steepDescentMins = localFormatter.parse(inSDescent).doubleValue() * altFactor;
129 catch (Exception e) {_parseFailed = true;}
131 else _parseFailed = true;
135 * Populate with double metric values, for example the results of a Learning process
136 * @param inFlat time for 5km on the flat
137 * @param inGClimb time for 100m gentle climb
138 * @param inSClimb time for 100m steep climb
139 * @param inGDescent time for 100m gentle descent
140 * @param inSDescent time for 100m steep descent
142 public void populateWithMetrics(double inFlat, double inGClimb, double inSClimb, double inGDescent, double inSDescent)
145 _gentleClimbMins = inGClimb;
146 _steepClimbMins = inSClimb;
147 _gentleDescentMins = inGDescent;
148 _steepDescentMins = inSDescent;
152 * @param inString parameter string to check
153 * @return true if it looks valid (no letters, at least one digit)
155 private static boolean isParamStringValid(String inString)
157 if (inString == null) {return false;}
158 boolean hasDigit = false, currPunc = false, prevPunc = false;
159 for (int i=0; i<inString.length(); i++)
161 char c = inString.charAt(i);
162 if (Character.isLetter(c)) {return false;} // no letters allowed
163 currPunc = (c == '.' || c == ',');
164 if (currPunc && prevPunc) {return false;} // no consecutive . or , allowed
166 hasDigit = hasDigit || Character.isDigit(c);
168 return hasDigit; // must have at least one digit!
172 * @return true if the parameters are valid, with no parsing errors
174 public boolean isValid()
176 return !_parseFailed; // && _flatMins > 0.0 && _gentleClimbMins >= 0.0 && _steepClimbMins >= 0.0;
180 * @return five strings for putting in text fields for editing / display
182 public String[] getStrings()
184 Unit distUnit = Config.getUnitSet().getDistanceUnit();
185 Unit altUnit = Config.getUnitSet().getAltitudeUnit();
186 double distFactor = (distUnit == KILOMETRES ? 1.0 : (5000/3.0 * distUnit.getMultFactorFromStd()));
187 double altFactor = (altUnit.isStandard() ? 1.0 : (1.0/3.0 * altUnit.getMultFactorFromStd()));
188 // Use locale-specific number formatting, eg commas for german
189 NumberFormat numFormatter = NumberFormat.getNumberInstance();
190 if (numFormatter instanceof DecimalFormat) {
191 ((DecimalFormat) numFormatter).applyPattern("0.00");
193 // Conversion between units
194 return new String[] {
195 numFormatter.format(_flatMins / distFactor),
196 numFormatter.format(_gentleClimbMins / altFactor), numFormatter.format(_steepClimbMins / altFactor),
197 numFormatter.format(_gentleDescentMins / altFactor), numFormatter.format(_steepDescentMins / altFactor)
202 * @return unit-specific string describing the distance for the flat time (5km/3mi/3NM)
204 public static String getStandardDistance()
206 Unit distUnit = Config.getUnitSet().getDistanceUnit();
207 return (distUnit == KILOMETRES ? "5 " : "3 ") + I18nManager.getText(distUnit.getShortnameKey());
211 * @return unit-specific string describing the height difference for the climbs/descents (100m/300ft)
213 public static String getStandardClimb()
215 Unit altUnit = Config.getUnitSet().getAltitudeUnit();
216 return (altUnit.isStandard() ? "100 " : "300 ") + I18nManager.getText(altUnit.getShortnameKey());
220 * @return contents of parameters as a semicolon-separated (metric) string for the config
222 public String toConfigString()
224 return "" + twoDp(_flatMins) + ";" + twoDp(_gentleClimbMins) + ";" + twoDp(_steepClimbMins) + ";"
225 + twoDp(_gentleDescentMins) + ";" + twoDp(_steepDescentMins);
229 * @param inNum number to output
230 * @return formatted string to two decimal places, with decimal point
232 private static String twoDp(double inNum)
234 if (inNum < 0.0) return "-" + twoDp(-inNum);
235 int hundreds = (int) (inNum * 100 + 0.5);
236 return "" + (hundreds / 100) + "." + (hundreds % 100);
240 * Apply the parameters to the given range stats
241 * @param inStats stats of current range
242 * @return estimated number of minutes required
244 public double applyToStats(RangeStats inStats)
246 if (inStats == null || !inStats.isValid()) return 0.0;
247 final Unit METRES = UnitSetLibrary.UNITS_METRES;
248 final double STANDARD_CLIMB = 100.0; // metres
249 final double STANDARD_DISTANCE = 5.0; // kilometres
250 return _flatMins * inStats.getMovingDistanceKilometres() / STANDARD_DISTANCE
251 + _gentleClimbMins * inStats.getGentleAltitudeRange().getClimb(METRES) / STANDARD_CLIMB
252 + _steepClimbMins * inStats.getSteepAltitudeRange().getClimb(METRES) / STANDARD_CLIMB
253 + _gentleDescentMins * inStats.getGentleAltitudeRange().getDescent(METRES) / STANDARD_CLIMB
254 + _steepDescentMins * inStats.getSteepAltitudeRange().getDescent(METRES) / STANDARD_CLIMB;
258 * Combine two sets of parameters together
259 * @param inOther other set
260 * @param inFraction fractional weight
261 * @return combined set
263 public EstimationParameters combine(EstimationParameters inOther, double inFraction)
265 if (inFraction < 0.0 || inFraction > 1.0 || inOther == null) {
268 // inFraction is the weight of this one, weight of the other one is 1-inFraction
269 final double fraction2 = 1 - inFraction;
270 EstimationParameters combined = new EstimationParameters();
271 combined._flatMins = inFraction * _flatMins + fraction2 * inOther._flatMins;
272 combined._gentleClimbMins = inFraction * _gentleClimbMins + fraction2 * inOther._gentleClimbMins;
273 combined._gentleDescentMins = inFraction * _gentleDescentMins + fraction2 * inOther._gentleDescentMins;
274 combined._steepClimbMins = inFraction * _steepClimbMins + fraction2 * inOther._steepClimbMins;
275 combined._steepDescentMins = inFraction * _steepDescentMins + fraction2 * inOther._steepDescentMins;