]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/weather/ResultSet.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / function / weather / ResultSet.java
1 package tim.prune.function.weather;
2
3 /**
4  * Class to hold a set of (up to six) weather results,
5  * so that they don't have to be downloaded again
6  */
7 public class ResultSet
8 {
9         /** Array of six results */
10         private WeatherResults[] _results = new WeatherResults[6];
11         /** Location id for which these results apply */
12         private String _locationId = null;
13
14         /**
15          * Clear the array, forget all results
16          */
17         private void clear()
18         {
19                 for (int i=0; i<6; i++) {
20                         _results[i] = null;
21                 }
22         }
23
24         /**
25          * Get the specified weather results, if available
26          * @param inLocationId location id
27          * @param inCurrent true to get the current weather
28          * @param inDaily true to get the daily forecast
29          * @param inHourly true to get the three-hourly forecast
30          * @param inCelsius true to get celsius
31          * @return weather results, or null if not available
32          */
33         public WeatherResults getWeather(String inLocationId,
34                 boolean inCurrent, boolean inDaily, boolean inHourly, boolean inCelsius)
35         {
36                 // Check location
37                 if (inLocationId == null || _locationId == null || !inLocationId.equals(_locationId)) {
38                         return null;
39                 }
40                 // check forecast type
41                 final int numTypesGiven = (inCurrent ? 1 : 0) + (inDaily ? 1 : 0) + (inHourly ? 1 : 0);
42                 if (numTypesGiven != 1) {
43                         System.err.println("getWeather, numtypesgiven = " + numTypesGiven);
44                         return null; // can't ask for more or less than one type
45                 }
46                 // Pull out from array
47                 final int index = (inCurrent ? 0 : (inDaily ? 2 : 4)) + (inCelsius ? 1 : 0);
48                 return _results[index];
49         }
50
51         /**
52          * Store the given weather results
53          * @param inResults results object
54          * @param inLocationId location id
55          * @param inCurrent true if this is the current weather
56          * @param inDaily true if this is the daily forecast
57          * @param inHourly true if this is the three-hourly forecast
58          * @param inCelsius true if numbers are celsius
59          */
60         public void setWeather(WeatherResults inResults, String inLocationId,
61                 boolean inCurrent, boolean inDaily, boolean inHourly, boolean inCelsius)
62         {
63                 // Check location
64                 if (inLocationId == null || inLocationId.equals("")) {
65                         return;
66                 }
67                 if (_locationId == null || !inLocationId.equals(_locationId))
68                 {
69                         // coordinates have changed
70                         clear();
71                         _locationId = inLocationId;
72                 }
73                 // check forecast type
74                 final int numTypesGiven = (inCurrent ? 1 : 0) + (inDaily ? 1 : 0) + (inHourly ? 1 : 0);
75                 if (numTypesGiven != 1) {
76                         System.err.println("setWeather, numtypesgiven = " + numTypesGiven);
77                         return; // can't set more or less than one type
78                 }
79                 // Store in array
80                 final int index = (inCurrent ? 0 : (inDaily ? 2 : 4)) + (inCelsius ? 1 : 0);
81                 _results[index] = inResults;
82         }
83 }