X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Ffunction%2Fweather%2FResultSet.java;fp=src%2Ftim%2Fprune%2Ffunction%2Fweather%2FResultSet.java;h=664d7aee1fe5e5409f6d8a2b23394339bb712f02;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/function/weather/ResultSet.java b/src/tim/prune/function/weather/ResultSet.java new file mode 100644 index 0000000..664d7ae --- /dev/null +++ b/src/tim/prune/function/weather/ResultSet.java @@ -0,0 +1,83 @@ +package tim.prune.function.weather; + +/** + * Class to hold a set of (up to six) weather results, + * so that they don't have to be downloaded again + */ +public class ResultSet +{ + /** Array of six results */ + private WeatherResults[] _results = new WeatherResults[6]; + /** Location id for which these results apply */ + private String _locationId = null; + + /** + * Clear the array, forget all results + */ + private void clear() + { + for (int i=0; i<6; i++) { + _results[i] = null; + } + } + + /** + * Get the specified weather results, if available + * @param inLocationId location id + * @param inCurrent true to get the current weather + * @param inDaily true to get the daily forecast + * @param inHourly true to get the three-hourly forecast + * @param inCelsius true to get celsius + * @return weather results, or null if not available + */ + public WeatherResults getWeather(String inLocationId, + boolean inCurrent, boolean inDaily, boolean inHourly, boolean inCelsius) + { + // Check location + if (inLocationId == null || _locationId == null || !inLocationId.equals(_locationId)) { + return null; + } + // check forecast type + final int numTypesGiven = (inCurrent ? 1 : 0) + (inDaily ? 1 : 0) + (inHourly ? 1 : 0); + if (numTypesGiven != 1) { + System.err.println("getWeather, numtypesgiven = " + numTypesGiven); + return null; // can't ask for more or less than one type + } + // Pull out from array + final int index = (inCurrent ? 0 : (inDaily ? 2 : 4)) + (inCelsius ? 1 : 0); + return _results[index]; + } + + /** + * Store the given weather results + * @param inResults results object + * @param inLocationId location id + * @param inCurrent true if this is the current weather + * @param inDaily true if this is the daily forecast + * @param inHourly true if this is the three-hourly forecast + * @param inCelsius true if numbers are celsius + */ + public void setWeather(WeatherResults inResults, String inLocationId, + boolean inCurrent, boolean inDaily, boolean inHourly, boolean inCelsius) + { + // Check location + if (inLocationId == null || inLocationId.equals("")) { + return; + } + if (_locationId == null || !inLocationId.equals(_locationId)) + { + // coordinates have changed + clear(); + _locationId = inLocationId; + } + // check forecast type + final int numTypesGiven = (inCurrent ? 1 : 0) + (inDaily ? 1 : 0) + (inHourly ? 1 : 0); + if (numTypesGiven != 1) { + System.err.println("setWeather, numtypesgiven = " + numTypesGiven); + return; // can't set more or less than one type + } + // Store in array + final int index = (inCurrent ? 0 : (inDaily ? 2 : 4)) + (inCelsius ? 1 : 0); + _results[index] = inResults; + } +}