1 package tim.prune.function.weather;
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
8 * Class to represent a weather forecast
9 * for a single day or a 3-hour period
11 public class SingleForecast
13 private String _date = null;
14 private String _dayDescKey = null;
15 private String _timeFrom = null, _timeTo = null;
16 private String _imageName = null;
17 private String _desc = null;
18 private String _tempString = null;
19 private String _humidity = null;
20 private String _windDesc = null;
22 /** For getting today's and tomorrow's dates */
23 private static SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
25 /** Set the time of the forecast */
26 public void setTime(String inDate, String inTimeFrom, String inTimeTo)
29 if (inTimeFrom != null && inTimeFrom.length() > 10
30 && inTimeTo != null && inTimeTo.length() > 10)
32 _date = inTimeFrom.substring(0, 10);
33 _timeFrom = inTimeFrom.substring(11, 16);
34 _timeTo = inTimeTo.substring(11, 16);
36 _dayDescKey = getDayDescriptionKey(_date);
37 // System.out.println(_date + " is " + _dayDescKey);
41 * Set the symbol details
43 public void setSymbol(String inNumber, String inImageCode, String inDesc)
45 _imageName = getIconName(inNumber, inImageCode);
46 // System.out.println("For number " + inNumber + "(" + inDesc + ") and code " + inImageCode + ", the symbol is " + _imageName);
51 * Set the minimum and maximum temperatures (will be rounded to nearest int)
53 public void setTemps(String inMin, String inMax)
55 String tempMin = null, tempMax = null;
57 tempMin = "" + Math.round(Double.parseDouble(inMin));
58 } catch (Exception e) {}; // tempMin stays null if temp can't be parsed
60 tempMax = "" + Math.round(Double.parseDouble(inMax));
61 } catch (Exception e) {}; // tempMax stays null if temp can't be parsed
63 _tempString = tempMin;
64 if (tempMin != null && tempMax != null) {
65 if (!tempMin.equals(tempMax))
67 if (tempMin.charAt(0) == '-' && tempMax.charAt(0) != '-' && tempMax.charAt(0) != '0') {
68 // min temp is negative, max is positive, so add a + to the max
69 tempMax = "+" + tempMax;
71 _tempString = tempMin + " — " + tempMax;
74 else if (tempMax != null) {
75 _tempString = tempMax;
80 public void setHumidity(String inHumidity) {
81 _humidity = inHumidity;
84 /** Set description of wind */
85 public void setWindDesc(String inDesc) {
90 * Get the name of the image file for the given weather report
91 * @param inCode numeric three-digit code, as string
92 * @param inImage filename as given by openweather (just used for day/night)
93 * @return image file using GpsPrune's icons
95 public static String getIconName(String inCode, String inImage)
97 final boolean daytime = inImage == null || inImage.length() != 3 || inImage.charAt(2) != 'n';
98 final char leadDigit = (inCode == null || inCode.equals("")) ? '0' : inCode.charAt(0);
99 String iconName = null;
102 case '2': return "storm.png";
103 case '3': return "lightrain.png";
105 iconName = "rain.png";
106 if (inCode.equals("500")) {iconName = "lightrain.png";}
107 else if (inCode.equals("511")) {iconName = "hail.png";}
109 case '6': return "snow.png";
110 case '7': return "fog.png";
112 iconName = daytime ? "clouds-day.png" : "clouds-night.png";
113 if (inCode.equals("800")) {iconName = daytime ? "clear-day.png" : "clear-night.png";}
114 else if (inCode.equals("804")) {iconName = "clouds.png";}
117 iconName = "extreme.png";
118 if (inCode.equals("906")) {iconName = "hail.png";}
125 * MAYBE: Maybe split off into separate DateFunctions class?
127 * @return day description, such as "today" or "saturday"
129 private static String getDayDescriptionKey(String inDate)
131 if (inDate == null || inDate.length() != 10) {return null;}
132 Calendar cal = Calendar.getInstance();
133 String todaysDate = DATE_FORMATTER.format(cal.getTime());
134 if (inDate.equals(todaysDate)) {return "today";}
135 cal.add(Calendar.DATE, 1);
136 String tomorrowsDate = DATE_FORMATTER.format(cal.getTime());
137 if (inDate.equals(tomorrowsDate)) {return "tomorrow";}
138 // Construct a date with this string and find out its day
141 cal.setTime(DATE_FORMATTER.parse(inDate));
142 switch (cal.get(Calendar.DAY_OF_WEEK))
144 case Calendar.MONDAY : return "monday";
145 case Calendar.TUESDAY : return "tuesday";
146 case Calendar.WEDNESDAY : return "wednesday";
147 case Calendar.THURSDAY : return "thursday";
148 case Calendar.FRIDAY : return "friday";
149 case Calendar.SATURDAY : return "saturday";
150 case Calendar.SUNDAY : return "sunday";
153 catch (ParseException pe) {}
158 /** @return true if there are times present, not just a date */
159 public boolean hasTimes() {
160 return _timeFrom != null && _timeTo != null;
162 /** @return temperature range */
163 public String getTemps() {
168 public String getDate() {return _date;}
169 /** @return time from */
170 public String getTimeFrom() {return _timeFrom;}
171 /** @return time to */
172 public String getTimeTo() {return _timeTo;}
173 /** @return day description */
174 public String getDayDesc() {return _dayDescKey;}
176 /** @return image name */
177 public String getImageName() {return _imageName;}
178 /** @return description */
179 public String getDescription() {return _desc;}
181 /** @return humidity */
182 public String getHumidity() {return _humidity;}
183 /** @return wind description */
184 public String getWindDescription() {return _windDesc;}