1
0
Fork 0
mirror of https://bitbucket.org/ethauvin/owm-japis.git synced 2025-04-25 17:57:11 -07:00

2.5.0.1 is ready!

This commit is contained in:
Ashutosh Kumar Singh 2013-08-10 23:52:09 +05:30
parent 391485a605
commit de1786a2d9
118 changed files with 32045 additions and 0 deletions

View file

@ -0,0 +1,633 @@
/*
* Copyright (C)2013 Ashutosh Kumar Singh [me@AKSingh.net]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package net.aksingh.java.api.owm;
import java.util.Date;
import org.json.JSONObject;
/**
* This class provides default implementations for {@link net.aksingh.java.api.owm.CurrentWeatherData}
* and {@link net.aksingh.java.api.owm.ForecastWeatherData} classes.
* Standard behaviors like the <code>has</code> and the <code>get</code>
* methods for information about weather or forecast (for example,
* temperature, pressure, weather, clouds, wind, etc.) are defined here.
*
* @author Ashutosh Kumar Singh
* @version 2013/07/26
* @since 2.5.0.1
*/
abstract public class AbstractWeatherData {
/** Key for JSON object - Clouds */
protected final String JSON_CLOUDS = "clouds";
/** Key for JSON object - Coordinates (Geographic coordinates) */
protected final String JSON_COORD = "coord";
/** Key for JSON object - Main (Temperature, pressure, etc.) */
protected final String JSON_MAIN = "main";
/** Key for JSON array - Weather (Weather name, description, etc.) */
protected final String JSON_WEATHER = "weather";
/** Key for JSON object - Wind */
protected final String JSON_WIND = "wind";
/*
************************
* Defining sub-classes
************************
*/
/**
* This class provides default implementations for <code>Clouds</code>.
* Standard behaviors like the <code>has</code> and the <code>get</code>
* methods for information about clouds (for example, percentage of
* clouds, etc.) are defined here.
*
* @author Ashutosh Kumar Singh
* @version 2013/07/27
* @since 2.5.0.1
*/
abstract public static class Clouds {
/**
* Key for JSON variable <code>Clouds -> All</code>
* (percentage of all clouds)
*/
private final String JSON_CLOUDS_ALL = "all";
/** Percentage of all clouds */
private final float percentOfClouds;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Clouds() {
this.percentOfClouds = Float.NaN;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about clouds
*/
public Clouds(JSONObject jsonObj) {
this.percentOfClouds = (float) jsonObj.optDouble(this.JSON_CLOUDS_ALL, Double.NaN);
}
/**
* Tells if the data for percentage of all clouds is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasPercentageOfClouds() {
return (this.percentOfClouds != Float.NaN);
}
/**
* Returns data for percentage of all clouds.
* @return Percentage of all clouds if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getPercentageOfClouds() {
return this.percentOfClouds;
}
}
/**
* This class provides default implementations for <code>Coord</code>.
* Standard behaviors like the <code>has</code> and the <code>get</code>
* methods for information about geographic coordinates (latitude
* and longitude) are defined here.
*
* @author Ashutosh Kumar Singh
* @version 2013/07/28
*/
abstract public static class Coord {
/** Key for JSON variable <code>Coord -> Latitude</code> */
private final String JSON_COORD_LATITUDE = "lat";
/** Key for JSON variable <code>Coord -> Longitude</code> */
private final String JSON_COORD_LONGITUDE = "lon";
/** Latitude */
private final float lat;
/** Longitude */
private final float lon;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Coord() {
this.lat = Float.NaN;
this.lon = Float.NaN;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about coordinates
*/
public Coord(JSONObject jsonObj) {
this.lat = (float) jsonObj.optDouble(this.JSON_COORD_LATITUDE, Double.NaN);
this.lon = (float) jsonObj.optDouble(this.JSON_COORD_LONGITUDE, Double.NaN);
}
/**
* Tells if the data for latitude of the city is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasLatitude() {
return (this.lat != Float.NaN);
}
/**
* Tells if the data for longitude of the city is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasLongitude() {
return (this.lon != Float.NaN);
}
/**
* Returns data for latitude of the city.
* @return Latitude of the city if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getLatitude() {
return this.lat;
}
/**
* Returns data for longitude of the city.
* @return Longitude of the city if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getLongitude() {
return this.lon;
}
}
/**
* This class provides default implementations for <code>Main</code>.
* Standard behaviors like the <code>has</code> and the <code>get</code>
* methods for information about main weather elements (for example,
* temperature, pressure, humidity, etc.) are defined here.
*
* @author Ashutosh Kumar Singh
* @version 2013/07/28
* @since 2.5.0.1
*/
abstract public static class Main {
/** Key for JSON variable <code>Main -> Temperature</code> */
private final String JSON_MAIN_TEMP = "temp";
/** Key for JSON variable <code>Main -> Minimum temperature</code> */
private final String JSON_MAIN_TEMP_MIN = "temp_min";
/** Key for JSON variable <code>Main -> Maximum temperature</code> */
private final String JSON_MAIN_TEMP_MAX = "temp_max";
/** Key for JSON variable <code>Main -> Pressure</code> */
private final String JSON_MAIN_PRESSURE = "pressure";
/** Key for JSON variable <code>Main -> Humidity</code> */
private final String JSON_MAIN_HUMIDITY = "humidity";
/** Temperature */
private final float temp;
/** Minimum temperature */
private final float minTemp;
/** Maximum temperature */
private final float maxTemp;
/** Pressure */
private final float pressure;
/** Humidity */
private final float humidity;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Main() {
this.temp = Float.NaN;
this.minTemp = Float.NaN;
this.maxTemp = Float.NaN;
this.pressure = Float.NaN;
this.humidity = Float.NaN;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about main weather
* elements. For example, temperature, pressure, etc.
*/
public Main(JSONObject jsonObj) {
this.temp = (float) jsonObj.optDouble(this.JSON_MAIN_TEMP, Double.NaN);
this.minTemp = (float) jsonObj.optDouble(this.JSON_MAIN_TEMP_MIN, Double.NaN);
this.maxTemp = (float) jsonObj.optDouble(this.JSON_MAIN_TEMP_MAX, Double.NaN);
this.pressure = (float) jsonObj.optDouble(this.JSON_MAIN_PRESSURE, Double.NaN);
this.humidity = (float) jsonObj.optDouble(this.JSON_MAIN_HUMIDITY, Double.NaN);
}
/**
* Tells if the data for temperature of the city is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasTemperature() {
return (this.temp != Float.NaN);
}
/**
* Tells if the data for minimum temperature of the city is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasMinTemperature() {
return (this.minTemp != Float.NaN);
}
/**
* Tells if the data for maximum temperature of the city is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasMaxTemperature() {
return (this.maxTemp != Float.NaN);
}
/**
* Tells if the data for pressure of the city is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasPressure() {
return (this.pressure != Float.NaN);
}
/**
* Tells if the data for humidity of the city is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasHumidity() {
return (this.humidity != Float.NaN);
}
/**
* Returns data for temperature of the city.
* @return Temperature of the city if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getTemperature() {
return this.temp;
}
/**
* Returns data for minimum temperature of the city.
* @return Minimum temperature of the city if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getMinTemperature() {
return this.minTemp;
}
/**
* Returns data for maximum temperature of the city.
* @return Maximum temperature of the city if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getMaxTemperature() {
return this.maxTemp;
}
/**
* Returns data for pressure of the city.
* @return Pressure of the city if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getPressure() {
return this.pressure;
}
/**
* Returns data for humidity of the city.
* @return Humidity of the city if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getHumidity() {
return this.humidity;
}
}
/**
* This class provides default implementations for <code>Weather</code>.
* Standard behaviors like the <code>has</code> and the <code>get</code>
* methods for information about weather (for example, id, name,
* description, icon, etc.) are defined here.
*
* @author Ashutosh Kumar Singh
* @version 2013/07/28
* @since 2.5.0.1
*/
abstract public static class Weather {
/** Key for JSON variable in array <code>Weather -> ID</code> */
private final String JSON_WEATHER_ID = "id";
/** Key for JSON variable in array <code>Weather -> Main (name of weather)</code> */
private final String JSON_WEATHER_MAIN = "main";
/** Key for JSON variable <code>Weather -> Description</code> */
private final String JSON_WEATHER_DESCRIPTION = "description";
/** Key for JSON variable in array <code>Weather -> Icon</code> */
private final String JSON_WEATHER_ICON = "icon";
/** Weather ID */
private final int id;
/** Weather name */
private final String name;
/** Weather description */
private final String description;
/** Weather icon */
private final String icon;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Weather() {
this.id = Integer.MIN_VALUE;
this.name = null;
this.description = null;
this.icon = null;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about weather id, name, etc.
*/
public Weather(JSONObject jsonObj) {
this.id = jsonObj.optInt(this.JSON_WEATHER_ID, Integer.MIN_VALUE);
this.name = jsonObj.optString(this.JSON_WEATHER_MAIN, null);
this.description = jsonObj.optString(this.JSON_WEATHER_DESCRIPTION, null);
this.icon = jsonObj.optString(this.JSON_WEATHER_ICON, null);
}
/**
* Tells if the data for weather's code is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasWeatherCode() {
return (this.id != Integer.MIN_VALUE);
}
/**
* Tells if the data for weather's name is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasWeatherName() {
return (this.name != null);
}
/**
* Tells if the data for weather's description is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasWeatherDescription() {
return (this.description != null);
}
/**
* Tells if the data for name of weather's icon is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasWeatherIconName() {
return (this.icon != null);
}
/**
* Returns data for code for weather of the city.
* @return Code for weather of the city if available,
* otherwise <code>Integer.MIN_VALUE</code>
*/
public int getWeatherCode() {
return this.id;
}
/**
* Returns data for name for weather of the city.
* @return Name for weather of the city if available,
* otherwise <code>null</code>
*/
public String getWeatherName() {
return this.name;
}
/**
* Returns data for description for weather of the city.
* @return Description for weather of the city if available,
* otherwise <code>null</code>
*/
public String getWeatherDescription() {
return this.description;
}
/**
* Returns data for name of icon for weather of the city.
* @return Name of icon for weather of the city if available,
* otherwise <code>null</code>
*/
public String getWeatherIconName() {
return this.icon;
}
}
/**
* This class provides default implementations for <code>Wind</code>.
* Standard behaviors like the <code>has</code> and the <code>get</code>
* methods for information about wind (for example, speed, degree,
* etc.) are defined here.
*
* @author Ashutosh Kumar Singh
* @version 2013/07/28
* @since 2.5.0.1
*/
abstract public static class Wind {
/** Key for JSON variable <code>Wind -> Speed</code> */
private final String JSON_WIND_SPEED = "speed";
/** Key for JSON variable <code>Wind -> Degree</code> */
private final String JSON_WIND_DEGREE = "deg";
/** Wind speed */
private final float speed;
/** Wind degree (direction of wind) */
private final float degree;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Wind() {
this.speed = Float.NaN;
this.degree = Float.NaN;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about wind
*/
public Wind(JSONObject jsonObj) {
this.speed = (float) jsonObj.optDouble(this.JSON_WIND_SPEED, Double.NaN);
this.degree = (float) jsonObj.optDouble(this.JSON_WIND_DEGREE, Double.NaN);
}
/**
* Tells if the data for speed of wind in the city is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasWindSpeed() {
return (this.speed != Float.NaN);
}
/**
* Tells if the data for degree (degree gives direction) of wind
* in the city is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasWindDegree() {
return (this.hasWindSpeed() && (this.degree != Float.NaN));
}
/**
* Returns data for speed of wind in the city.
* @return Speed of wind in the city if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getWindSpeed() {
return this.speed;
}
/**
* Returns data for degree of wind in the city.
* @return Degree of wind in the city if available,
* otherwise <code>Float.NaN</code>, i.e., Not a Number.
*/
public float getWindDegree() {
return this.degree;
}
}
/*
***********************
* Declaring this class
***********************
*/
/** Key for JSON variable Date-Time (date & time of the weather) */
private final String JSON_DATE_TIME = "dt";
/**
* Date and time of the weather. This is answer for the question that
* when is/will be this weather.
*/
private final Date dateTime;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public AbstractWeatherData() {
this.dateTime = null;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing weather data
*/
public AbstractWeatherData(JSONObject jsonObj) {
// getting seconds from the data
long sec = (jsonObj != null) ? jsonObj.optLong(this.JSON_DATE_TIME, Long.MIN_VALUE) : Long.MIN_VALUE;
// converting seconds to Date object
if (sec != Long.MIN_VALUE) {
this.dateTime = new Date(sec);
} else {
this.dateTime = null;
}
}
/**
* Tells if the data for date and time of this weather is available or not.
* @return <code>true</code> if data available, otherwise <code>false</code>
*/
public boolean hasDateTime() {
return (this.dateTime != null);
}
/**
* Returns data for date and time of this weather.
* @return Date and time (in object of {@link java.util.Date}) if available,
* otherwise <code>null</code>
*/
public Date getDateTime() {
return this.dateTime;
}
}

View file

@ -0,0 +1,656 @@
/*
* Copyright (C)2013 Ashutosh Kumar Singh [me@AKSingh.net]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package net.aksingh.java.api.owm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Parses current weather data (from the JSON data) and provides methods
* to get/access the information about current weather.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public class CurrentWeatherData extends AbstractWeatherData {
/** Key for JSON object - Rain */
private final String JSON_RAIN = "rain";
/** Key for JSON object - Sys */
private final String JSON_SYS = "sys";
/*
************************
* Declaring sub-classes
************************
*/
/**
* Parses data about clouds (from the JSON data) and provides methods
* to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Clouds extends AbstractWeatherData.Clouds {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Clouds() {
super();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about clouds
*/
public Clouds(JSONObject jsonObj) {
super(jsonObj);
}
}
/**
* Parses data about geographic coordinates (from the JSON data)
* and provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Coord extends AbstractWeatherData.Coord {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Coord() {
super();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about coordinates
*/
public Coord(JSONObject jsonObj) {
super(jsonObj);
}
}
/**
* Parses data about main weather elements (from the JSON data) and
* provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Main extends AbstractWeatherData.Main {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Main() {
super();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about main
* weather elements (temperature, pressure, etc.)
*/
public Main(JSONObject jsonObj) {
super(jsonObj);
}
}
/**
* Parses data about rain (from the JSON data) and provides methods
* to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Rain {
/** Key for JSON variable <code>Rain -> Rain per 3 hours</code> */
private final String JSON_RAIN_3HOURS = "3h";
/** Rain per 3 hours */
private final float rain3h;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Rain() {
this.rain3h = Float.NaN;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about rain
*/
public Rain(JSONObject jsonObj) {
this.rain3h = (float) jsonObj.optDouble(this.JSON_RAIN_3HOURS, Double.NaN);
}
public boolean hasRain3Hours() {
return (this.rain3h != Float.NaN);
}
public float getRain3Hours() {
return this.rain3h;
}
}
/**
* Parses data about country, sunrise, and sunset (from the JSON data)
* and provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Sys {
/** Key for JSON variable <code>Sys -> Country</code> */
private final String JSON_SYS_COUNTRY_CODE = "country";
/** Key for JSON variable <code>Sys -> Sunrise</code> */
private final String JSON_SYS_SUNRISE = "sunrise";
/** Key for JSON variable <code>Sys -> Sunset</code> */
private final String JSON_SYS_SUNSET = "sunset";
/** Country code for the city */
private final String countryCode;
/** Sunrise time */
private final Date sunrise;
/** Sunset time */
private final Date sunset;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Sys() {
this.countryCode = null;
this.sunrise = null;
this.sunset = null;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about country, sunrise,
* and sunset.
*/
public Sys(JSONObject jsonObj) {
this.countryCode = jsonObj.optString(this.JSON_SYS_COUNTRY_CODE, null);
long sr_secs = jsonObj.optLong(this.JSON_SYS_SUNRISE, Long.MIN_VALUE);
if (sr_secs != Long.MIN_VALUE) {
this.sunrise = new Date(sr_secs);
} else {
this.sunrise = null;
}
long ss_secs = jsonObj.optLong(this.JSON_SYS_SUNSET, Long.MIN_VALUE);
if (ss_secs != Long.MIN_VALUE) {
this.sunset = new Date(ss_secs);
} else {
this.sunset = null;
}
}
public boolean hasCountryCode() {
return (this.countryCode != null);
}
public boolean hasSunriseTime() {
return (this.sunrise != null);
}
public boolean hasSunsetTime() {
return (this.sunset != null);
}
public String getCountryCode() {
return this.countryCode;
}
public Date getSunriseTime() {
return this.sunrise;
}
public Date getSunsetTime() {
return this.sunset;
}
}
/**
* Parses data about weather code, name, etc. (from the JSON data)
* and provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Weather extends AbstractWeatherData.Weather {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Weather () {
super();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about weather id, name, etc.
*/
public Weather (JSONObject jsonObj) {
super(jsonObj);
}
}
/**
* Parses data about winds (from the JSON data) and provides methods
* to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Wind extends AbstractWeatherData.Wind {
/** Key for JSON variable <code>Wind -> Gust</code> */
private final String JSON_WIND_GUST = "gust";
/** Wind gust */
private final float gust;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Wind () {
super ();
this.gust = Float.NaN;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about wind
*/
public Wind (JSONObject jsonObj) {
super (jsonObj);
this.gust = (float) jsonObj.optDouble(this.JSON_WIND_GUST, Double.NaN);
}
public boolean hasWindGust() {
return (this.gust != Float.NaN);
}
public float getWindGust() {
return this.gust;
}
}
/*
***********************
* Declaring this class
***********************
*/
/** Key for JSON variable <code>Base</code> */
private final String JSON_BASE = "base";
/** Key for JSON variable <code>City code (ID)</code> */
private final String JSON_CITY_ID = "id";
/** Key for JSON variable <code>City name</code> */
private final String JSON_CITY_NAME = "name";
/** Key for JSON variable <code>Response code</code> */
private final String JSON_RESPONSE_CODE = "cod";
/** Base */
private final String base;
/** City code (ID) */
private final long cityID;
/** City name */
private final String cityName;
/** Response code */
private final int responseCode;
private final Clouds clouds;
private final Coord coord;
private final Main main;
private final Rain rain;
private final Sys sys;
private final Wind wind;
/** List of weather information (code, name, etc.) */
private final List<Weather> weatherList;
/** Count (number) of elements in list of weather information */
private final int weatherListCount;
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing current weather data
*/
public CurrentWeatherData(JSONObject jsonObj) {
super(jsonObj);
this.base = (jsonObj != null) ? jsonObj.optString(this.JSON_BASE, null) : null;
this.cityID = (jsonObj != null) ? jsonObj.optLong(this.JSON_CITY_ID, Long.MIN_VALUE) : Long.MIN_VALUE;
this.cityName = (jsonObj != null) ? jsonObj.optString(this.JSON_CITY_NAME, null) : null;
this.responseCode = (jsonObj != null) ? jsonObj.optInt(this.JSON_RESPONSE_CODE, Integer.MIN_VALUE) : Integer.MIN_VALUE;
JSONObject jsonObjClouds = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_CLOUDS) : null;
this.clouds = (jsonObjClouds != null) ? new Clouds(jsonObjClouds) : new Clouds();
JSONObject jsonObjCoord = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_COORD) : null;
this.coord = (jsonObjCoord != null) ? new Coord(jsonObjCoord) : new Coord();
JSONObject jsonObjMain = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_MAIN) : null;
this.main = (jsonObjMain != null) ? new Main(jsonObjMain) : new Main();
JSONObject jsonObjRain = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_RAIN) : null;
this.rain = (jsonObjRain != null) ? new Rain(jsonObjRain) : new Rain();
JSONObject jsonObjSys = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_SYS) : null;
this.sys = (jsonObjSys != null) ? new Sys(jsonObjSys) : new Sys();
JSONArray jsonArrWeather = (jsonObj != null) ? jsonObj.optJSONArray(this.JSON_WEATHER) : null;
this.weatherList = (jsonArrWeather != null) ? new ArrayList<Weather>(jsonArrWeather.length()) : Collections.EMPTY_LIST;
if (this.weatherList != Collections.EMPTY_LIST) {
for (int i = 0; i < jsonArrWeather.length(); i++) {
JSONObject jsonObjWeather = jsonArrWeather.optJSONObject (i);
if (jsonObjWeather != null) {
this.weatherList.add(new Weather(jsonObjWeather));
}
}
}
this.weatherListCount = this.weatherList.size();
JSONObject jsonObjWind = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_WIND) : null;
this.wind = (jsonObjWind != null) ? new Wind(jsonObjWind) : new Wind();
}
public boolean hasBaseStation() {
return (this.base != null);
}
public boolean hasCityCode() {
return (this.cityID != Long.MIN_VALUE);
}
public boolean hasCityName() {
return (this.cityName != null);
}
public boolean hasResponseCode() {
return (this.responseCode != Integer.MIN_VALUE);
}
public String getBaseStation() {
return this.base;
}
public long getCityCode() {
return this.cityID;
}
public String getCityName() {
return this.cityName;
}
public int getResponseCode() {
return this.responseCode;
}
// Objects
public Clouds getClouds_Object() {
return this.clouds;
}
public Coord getCoordinates_Object() {
return this.coord;
}
public Main getMainData_Object() {
return this.main;
}
public Rain getRain_Object() {
return this.rain;
}
public Sys getSysData_Object() {
return this.sys;
}
public Wind getWind_Object() {
return this.wind;
}
// Lists
public boolean hasWeather_List() {
return (this.weatherListCount != 0);
}
public int getWeather_List_Count() {
return this.weatherListCount;
}
public List<Weather> getWeather_List() {
return this.weatherList;
}
}

View file

@ -0,0 +1,707 @@
/*
* Copyright (C)2013 Ashutosh Kumar Singh [me@AKSingh.net]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package net.aksingh.java.api.owm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Parses daily forecast data (from the JSON data) and provides methods
* to get/access the information about daily forecasted weather.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Objects: Data initialized with default/non-parameterized constructor<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public class DailyForecastData {
/** Key for JSON object - City */
private final String JSON_CITY = "city";
/** Key for JSON object - List of forecasts */
private final String JSON_FORECAST_LIST = "list";
/*
************************
* Declaring sub-classes
************************
*/
/**
* Parses data about city (from the JSON data)
* and provides methods to get/access the information.
* For example, city name, coordinates, country name, population, etc.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class City {
/** Key for JSON object - Coordinates */
private final String JSON_CITY_COORD = "coord";
/**
* Parses data about geographic coordinates (from the JSON data)
* and provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Coord extends AbstractWeatherData.Coord {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Coord() {
super();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about clouds
*/
public Coord(JSONObject jsonObj) {
super(jsonObj);
}
}
/** Key for JSON variable <code>City code (ID)</code> */
private final String JSON_CITY_ID = "id";
/** Key for JSON variable <code>City name</code> */
private final String JSON_CITY_NAME = "name";
/** Key for JSON variable <code>Country code of city</code> */
private final String JSON_CITY_COUNTRY_CODE = "country";
/** Key for JSON variable <code>Population of city</code> */
private final String JSON_CITY_POPULATION = "population";
/** City code (ID) */
private final long cityID;
/** City name */
private final String cityName;
/** Country code of city */
private final String countryCode;
/** Population of city */
private final long population;
private final Coord coord;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public City() {
this.cityID = Long.MIN_VALUE;
this.cityName = null;
this.countryCode = null;
this.population = Long.MIN_VALUE;
this.coord = new Coord();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about city
*/
public City(JSONObject jsonObj) {
this.cityID = (jsonObj != null) ? jsonObj.optLong(this.JSON_CITY_ID, Long.MIN_VALUE) : Long.MIN_VALUE;
this.cityName = (jsonObj != null) ? jsonObj.optString(this.JSON_CITY_NAME, null) : null;
this.countryCode = (jsonObj != null) ? jsonObj.optString(this.JSON_CITY_COUNTRY_CODE, null) : null;
this.population = (jsonObj != null) ? jsonObj.optLong(this.JSON_CITY_POPULATION, Long.MIN_VALUE) : Long.MIN_VALUE;
JSONObject jsonObjCoord = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_CITY_COORD) : null;
this.coord = (jsonObjCoord != null) ? new Coord(jsonObjCoord) : new Coord();
}
public boolean hasCityCode() {
return (this.cityID != Long.MIN_VALUE);
}
public boolean hasCityName() {
return (this.cityName != null);
}
public boolean hasCountryCode() {
return (this.countryCode != null);
}
public boolean hasCityPopulation() {
return (this.population != Long.MIN_VALUE);
}
public long getCityCode() {
return this.cityID;
}
public String getCityName() {
return this.cityName;
}
public String getCountryCode() {
return this.countryCode;
}
public long getCityPopulation() {
return this.population;
}
// Objects
public Coord getCoordinates_Object() {
return this.coord;
}
}
/**
* Parses data about forecasts (from the JSON data)
* and provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Forecast extends AbstractWeatherData {
/** Key for JSON object - Temperature */
public final String JSON_TEMP = "temp";
/*
***************************
* Declaring sub-sub-classes
***************************
*/
/**
* Parses data about weather (from the JSON data)
* and provides methods to get/access the information.
* For example, weather id, name, etc.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Weather extends AbstractWeatherData.Weather {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Weather () {
super();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about weather
*/
public Weather (JSONObject jsonObj) {
super(jsonObj);
}
}
/**
* Parses data about temperature (from the JSON data)
* and provides methods to get/access the information.
* For example, weather id, name, etc.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Temperature {
/** Key for JSON variable <code>Temp -> Day</code> */
public final String JSON_TEMP_DAY = "day";
/** Key for JSON variable <code>Temp -> Minimum</code> */
public final String JSON_TEMP_MIN = "min";
/** Key for JSON variable <code>Temp -> Maximum</code> */
public final String JSON_TEMP_MAX = "max";
/** Key for JSON variable <code>Temp -> Night</code> */
public final String JSON_TEMP_NIGHT = "night";
/** Key for JSON variable <code>Temp -> Evening</code> */
public final String JSON_TEMP_EVENING = "eve";
/** Key for JSON variable <code>Temp -> Morning</code> */
public final String JSON_TEMP_MORNING = "morn";
/** Day temperature */
private final float dayTemp;
/** Minimum temperature */
private final float minTemp;
/** Maximum temperature */
private final float maxTemp;
/** Night temperature */
private final float nightTemp;
/** Evening temperature */
private final float eveTemp;
/** Morning temperature */
private final float mornTemp;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Temperature() {
this.dayTemp = Float.NaN;
this.minTemp = Float.NaN;
this.maxTemp = Float.NaN;
this.nightTemp = Float.NaN;
this.eveTemp = Float.NaN;
this.mornTemp = Float.NaN;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about temperature
*/
public Temperature(JSONObject jsonObj) {
this.dayTemp = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_TEMP_DAY, Double.NaN) : Float.NaN;
this.minTemp = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_TEMP_MIN, Double.NaN) : Float.NaN;
this.maxTemp = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_TEMP_MAX, Double.NaN) : Float.NaN;
this.nightTemp = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_TEMP_NIGHT, Double.NaN) : Float.NaN;
this.eveTemp = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_TEMP_EVENING, Double.NaN) : Float.NaN;
this.mornTemp = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_TEMP_MORNING, Double.NaN) : Float.NaN;
}
public boolean hasDayTemperature() {
return (this.dayTemp != Float.NaN);
}
public boolean hasMinimumTemperature() {
return (this.minTemp != Float.NaN);
}
public boolean hasMaximumTemperature() {
return (this.maxTemp != Float.NaN);
}
public boolean hasNightTemperature() {
return (this.nightTemp != Float.NaN);
}
public boolean hasEveningTemperature() {
return (this.eveTemp != Float.NaN);
}
public boolean hasMorningTemperature() {
return (this.mornTemp != Float.NaN);
}
public float getDayTemperature() {
return this.dayTemp;
}
public float getMinimumTemperature() {
return this.minTemp;
}
public float getMaximumTemperature() {
return this.maxTemp;
}
public float getNightTemperature() {
return this.nightTemp;
}
public float getEveningTemperature() {
return this.eveTemp;
}
public float getMorningTemperature() {
return this.mornTemp;
}
}
/*
************************
* Declaring this sub-class
************************
*/
/** Key for JSON variable <code>Pressure</code> */
private final String JSON_FORECAST_PRESSURE = "pressure";
/** Key for JSON variable <code>Humidity</code> */
private final String JSON_FORECAST_HUMIDITY = "humidity";
/** Key for JSON variable <code>Wind speed</code> */
private final String JSON_FORECAST_WIND_SPEED = "speed";
/** Key for JSON variable <code>Wind degree</code> */
private final String JSON_FORECAST_WIND_DEGREE = "deg";
/** Key for JSON variable <code>Percentage of clouds</code> */
private final String JSON_FORECAST_CLOUDS = "clouds";
/** Pressure */
private final float pressure;
/** Humidity */
private final float humidity;
/** Wind speed */
private final float windSpeed;
/** Wind degree */
private final float windDegree;
/** Percentage of clouds */
private final float cloudsPercent;
private final Temperature temp;
/** List of weather information (code, name, etc.) */
private final List<Weather> weatherList;
/** Count (number) of elements in list of weather information */
private final int weatherListCount;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Forecast() {
super();
this.pressure = Float.NaN;
this.humidity = Float.NaN;
this.windSpeed = Float.NaN;
this.windDegree = Float.NaN;
this.cloudsPercent = Float.NaN;
this.temp = new Temperature();
this.weatherList = Collections.EMPTY_LIST;
this.weatherListCount = this.weatherList.size();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about forecasts
*/
public Forecast(JSONObject jsonObj) {
super(jsonObj);
JSONObject jsonObjTemp = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_TEMP) : null;
this.temp = (jsonObjTemp != null) ? new Temperature(jsonObjTemp) : new Temperature();
this.humidity = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_FORECAST_HUMIDITY, Double.NaN) : Float.NaN;
this.pressure = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_FORECAST_PRESSURE, Double.NaN) : Float.NaN;
this.windSpeed = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_FORECAST_WIND_SPEED, Double.NaN) : Float.NaN;
this.windDegree = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_FORECAST_WIND_DEGREE, Double.NaN) : Float.NaN;
this.cloudsPercent = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_FORECAST_CLOUDS, Double.NaN) : Float.NaN;
JSONArray jsonArrWeather = (jsonObj != null) ? jsonObj.optJSONArray(this.JSON_WEATHER) : null;
this.weatherList = (jsonArrWeather != null) ? new ArrayList<Weather>(jsonArrWeather.length()) : Collections.EMPTY_LIST;
if (this.weatherList != Collections.EMPTY_LIST) {
for (int i = 0; i < jsonArrWeather.length (); i++) {
JSONObject jsonObjWeather = jsonArrWeather.optJSONObject (i);
if (jsonObjWeather != null) {
this.weatherList.add(new Weather(jsonObjWeather));
}
}
}
this.weatherListCount = this.weatherList.size();
}
public boolean hasHumidity() {
return (this.humidity != Float.NaN);
}
public boolean hasPressure() {
return (this.pressure != Float.NaN);
}
public boolean hasWindSpeed() {
return (this.windSpeed != Float.NaN);
}
public boolean hasWindDegree() {
return (this.windDegree != Float.NaN);
}
public boolean hasPercentageOfClouds() {
return (this.cloudsPercent != Float.NaN);
}
public float getHumidity() {
return this.humidity;
}
public float getPressure() {
return this.pressure;
}
public float getWindSpeed() {
return this.windSpeed;
}
public float getWindDegree() {
return this.windDegree;
}
public float getPercentageOfClouds() {
return this.cloudsPercent;
}
// Objects
public Temperature getTemperature_Object() {
return this.temp;
}
// Lists
public boolean hasWeather_List() {
return (this.weatherListCount != 0);
}
public int getWeather_List_Count() {
return this.weatherListCount;
}
public List<Weather> getWeather_List() {
return this.weatherList;
}
}
/*
***********************
* Declaring this class
***********************
*/
/** Key for JSON variable <code>Response code</code> */
private final String JSON_RESPONSE_CODE = "cod";
/** Key for JSON variable <code>Response time</code> */
private final String JSON_RESPONSE_TIME = "message";
/** Key for JSON variable <code>Forecast count</code> */
private final String JSON_RESPONSE_FORECAST_COUNT = "cnt";
/** Response code */
private final String responseCode;
/** Response time */
private final float responseTime;
/** Forecast count */
private final int responseForecastCount;
private final City city;
/** List of forecast information */
private final List<Forecast> forecastList;
/** Count (number) of elements in list of forecast information */
private final int forecastListCount;
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about daily forecasts
*/
public DailyForecastData(JSONObject jsonObj) {
this.responseCode = (jsonObj != null) ? jsonObj.optString(this.JSON_RESPONSE_CODE, null) : null;
this.responseTime = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_RESPONSE_TIME, Double.NaN) : Float.NaN;
this.responseForecastCount = (jsonObj != null) ? jsonObj.optInt(this.JSON_RESPONSE_FORECAST_COUNT, Integer.MIN_VALUE) : Integer.MIN_VALUE;
JSONObject jsonObjCity = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_CITY) : null;
this.city = (jsonObjCity != null) ? new City(jsonObjCity) : new City();
JSONArray jsonArrForecast = (jsonObj != null) ? jsonObj.optJSONArray(this.JSON_FORECAST_LIST) : null;
this.forecastList = (jsonArrForecast != null) ? new ArrayList<Forecast>(jsonArrForecast.length()) : Collections.EMPTY_LIST;
if (this.forecastList != Collections.EMPTY_LIST) {
for (int i = 0; i < jsonArrForecast.length(); i++) {
JSONObject jsonObjWeather = jsonArrForecast.optJSONObject(i);
if (jsonObjWeather != null) {
this.forecastList.add(new Forecast(jsonObjWeather));
}
}
}
this.forecastListCount = this.forecastList.size();
}
public boolean hasResponseCode() {
return (this.responseCode != null);
}
public boolean hasResponseTime() {
return (this.responseTime != Float.NaN);
}
public boolean hasResponseForecastCount() {
return (this.responseForecastCount != Integer.MIN_VALUE);
}
public String getResponseCode() {
return this.responseCode;
}
public float getResponseTime() {
return this.responseTime;
}
public int getResponseForecastCount() {
return this.responseForecastCount;
}
// Objects
public City getCity_Object() {
return this.city;
}
// Lists
public boolean hasForecast_List() {
return (this.forecastListCount != 0);
}
public int getForecast_List_Count() {
return this.forecastListCount;
}
public List<Forecast> getForecast_List() {
return this.forecastList;
}
}

View file

@ -0,0 +1,803 @@
/*
* Copyright (C)2013 Ashutosh Kumar Singh [me@AKSingh.net]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package net.aksingh.java.api.owm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Parses forecast weather data (from the JSON data) and provides methods
* to get/access the information about forecasted weather.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Objects: Data initialized with default/non-parameterized constructor<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public class ForecastWeatherData {
/** Key for JSON object - City */
private final String JSON_CITY = "city";
/** Key for JSON object - List of forecasts */
private final String JSON_FORECAST_LIST = "list";
/*
************************
* Declaring sub-classes
************************
*/
/**
* Parses data about city (from the JSON data)
* and provides methods to get/access the information.
* For example, city name, coordinates, country name, population, etc.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class City {
/** Key for JSON object - Coordinates */
private final String JSON_CITY_COORD = "coord";
/**
* Parses data about geographic coordinates (from the JSON data)
* and provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Coord extends AbstractWeatherData.Coord {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Coord() {
super();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about clouds
*/
public Coord(JSONObject jsonObj) {
super(jsonObj);
}
}
/** Key for JSON variable <code>City code (ID)</code> */
private final String JSON_CITY_ID = "id";
/** Key for JSON variable <code>City name</code> */
private final String JSON_CITY_NAME = "name";
/** Key for JSON variable <code>Country code of city</code> */
private final String JSON_CITY_COUNTRY_CODE = "country";
/** Key for JSON variable <code>Population of city</code> */
private final String JSON_CITY_POPULATION = "population";
/** City code (ID) */
private final long cityID;
/** City name */
private final String cityName;
/** Country code of city */
private final String countryCode;
/** Population of city */
private final long population;
private final Coord coord;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public City() {
this.cityID = Long.MIN_VALUE;
this.cityName = null;
this.countryCode = null;
this.population = Long.MIN_VALUE;
this.coord = new Coord();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about city
*/
public City(JSONObject jsonObj) {
this.cityID = (jsonObj != null) ? jsonObj.optLong(this.JSON_CITY_ID, Long.MIN_VALUE) : Long.MIN_VALUE;
this.cityName = (jsonObj != null) ? jsonObj.optString(this.JSON_CITY_NAME, null) : null;
this.countryCode = (jsonObj != null) ? jsonObj.optString(this.JSON_CITY_COUNTRY_CODE, null) : null;
this.population = (jsonObj != null) ? jsonObj.optLong(this.JSON_CITY_POPULATION, Long.MIN_VALUE) : Long.MIN_VALUE;
JSONObject jsonObjCoord = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_CITY_COORD) : null;
this.coord = (jsonObjCoord != null) ? new Coord(jsonObjCoord) : new Coord();
}
public boolean hasCityCode() {
return (this.cityID != Long.MIN_VALUE);
}
public boolean hasCityName() {
return (this.cityName != null);
}
public boolean hasCountryCode() {
return (this.countryCode != null);
}
public boolean hasCityPopulation() {
return (this.population != Long.MIN_VALUE);
}
public long getCityCode() {
return this.cityID;
}
public String getCityName() {
return this.cityName;
}
public String getCountryCode() {
return this.countryCode;
}
public long getCityPopulation() {
return this.population;
}
// Objects
public Coord getCoordinates_Object() {
return this.coord;
}
}
/**
* Parses data about forecasts (from the JSON data)
* and provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Forecast extends AbstractWeatherData {
/** Key for JSON object - Sys (pod, etc.) */
private final String JSON_SYS = "sys";
/*
***************************
* Declaring sub-sub-classes
***************************
*/
/**
* Parses data about clouds (from the JSON data)
* and provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Clouds extends AbstractWeatherData.Clouds {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Clouds() {
super();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about clouds
*/
public Clouds(JSONObject jsonObj) {
super(jsonObj);
}
}
/**
* Parses data about main weather elements (from the JSON data)
* and provides methods to get/access the information.
* For example, temperature, pressure, sea level, etc.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Main extends AbstractWeatherData.Main {
/** Key for JSON variable <code>Main -> Sea level</code> */
private final String JSON_MAIN_SEA_LEVEL = "sea_level";
/** Key for JSON variable <code>Main -> Ground level</code> */
private final String JSON_MAIN_GRND_LEVEL = "grnd_level";
/** Key for JSON variable <code>Main -> Temperature KF</code> */
private final String JSON_MAIN_TMP_KF = "temp_kf";
/** Sea level */
private final float seaLevel;
/** Ground level */
private final float groundLevel;
/** Temperature KF */
private final float tempKF;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Main() {
super();
this.seaLevel = Float.NaN;
this.groundLevel = Float.NaN;
this.tempKF = Float.NaN;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about main
* weather elements
*/
public Main(JSONObject jsonObj) {
super(jsonObj);
this.seaLevel = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_MAIN_SEA_LEVEL, Float.NaN) : Float.NaN;
this.groundLevel = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_MAIN_GRND_LEVEL, Float.NaN) : Float.NaN;
this.tempKF = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_MAIN_TMP_KF, Float.NaN) : Float.NaN;
}
public boolean hasSeaLevel() {
return (this.seaLevel != Float.NaN);
}
public boolean hasGroundLevel() {
return (this.groundLevel != Float.NaN);
}
public boolean hasTempKF() {
return (this.tempKF != Float.NaN);
}
public float getSeaLevel() {
return this.seaLevel;
}
public float getGroundLevel() {
return this.groundLevel;
}
public float getTempKF() {
return this.tempKF;
}
}
/**
* Parses data about main weather elements (from the JSON data)
* and provides methods to get/access the information.
* For example, temperature, pressure, sea level, etc.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Sys {
/** Key for JSON variable <code>Sys -> Pod</code> */
private final String JSON_SYS_POD = "pod";
/** Pod */
private final String pod;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Sys() {
this.pod = null;
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about sys.
* For example, pod, etc.
*/
public Sys(JSONObject jsonObj) {
this.pod = (jsonObj != null) ? jsonObj.optString(this.JSON_SYS_POD, null) : null;
}
public boolean hasPod() {
return (this.pod != null);
}
public String getPod() {
return this.pod;
}
}
/**
* Parses data about weather (from the JSON data)
* and provides methods to get/access the information.
* For example, weather id, name, etc.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Weather extends AbstractWeatherData.Weather {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Weather () {
super();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about weather
* code, name, etc.
*/
public Weather (JSONObject jsonObj) {
super(jsonObj);
}
}
/**
* Parses data about wind (from the JSON data)
* and provides methods to get/access the information.
* This class provides <code>has</code> and <code>get</code> methods
* to access the information.
*
* <p><code>has</code> methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
* <p><code>get</code> methods can be used to access the data, if the data
* exists, otherwise <code>get</code> methods will give value as per
* following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Wind extends AbstractWeatherData.Wind {
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Wind () {
super ();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about wind
*/
public Wind (JSONObject jsonObj) {
super (jsonObj);
}
}
/*
************************
* Declaring this sub-class
************************
*/
/** Key for JSON variable <code>Date time text</code> */
private final String JSON_DATE_TIME_TEXT = "dt_txt";
/** Date time text */
private final String dateTimeText;
private final Clouds clouds;
private final Main main;
private final Sys sys;
private final Wind wind;
/** List of weather information (code, name, etc.) */
private final List<Weather> weatherList;
/** Count (number) of elements in list of weather information */
private final int weatherListCount;
/**
* Non-parameterized constructor
*
* <p>Initializes variables as per following basis:<br>
* Boolean: <code>false</code><br>
* Integral: Minimum value (MIN_VALUE)<br>
* Floating point: Not a number (NaN)<br>
* Others: <code>null</code><br>
*/
public Forecast() {
super();
this.dateTimeText = null;
this.clouds = new Clouds();
this.main = new Main();
this.sys = new Sys();
this.weatherList = Collections.EMPTY_LIST;
this.weatherListCount = this.weatherList.size();
this.wind = new Wind();
}
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about forecasts
*/
public Forecast(JSONObject jsonObj) {
super(jsonObj);
this.dateTimeText = (jsonObj != null) ? jsonObj.optString(this.JSON_DATE_TIME_TEXT, null) : null;
JSONObject jsonObjClouds = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_CLOUDS) : null;
this.clouds = (jsonObjClouds != null) ? new Clouds(jsonObjClouds) : new Clouds();
JSONObject jsonObjMain = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_MAIN) : null;
this.main = (jsonObjMain != null) ? new Main(jsonObjMain) : new Main();
JSONObject jsonObjSys = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_SYS) : null;
this.sys = (jsonObjSys != null) ? new Sys(jsonObjSys) : new Sys();
JSONArray jsonArrWeather = (jsonObj != null) ? jsonObj.optJSONArray(this.JSON_WEATHER) : null;
this.weatherList = (jsonArrWeather != null) ? new ArrayList<Weather>(jsonArrWeather.length()) : Collections.EMPTY_LIST;
if (this.weatherList != Collections.EMPTY_LIST) {
for (int i = 0; i < jsonArrWeather.length (); i++) {
JSONObject jsonObjWeather = jsonArrWeather.optJSONObject (i);
if (jsonObjWeather != null) {
this.weatherList.add(new Weather(jsonObjWeather));
}
}
}
this.weatherListCount = this.weatherList.size();
JSONObject jsonObjWind = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_WIND) : null;
this.wind = (jsonObjWind != null) ? new Wind(jsonObjWind) : new Wind();
}
public boolean hasDateTimeText() {
return (this.dateTimeText != null);
}
public String getDateTimeText() {
return this.dateTimeText;
}
// Objects
public Clouds getClouds_Object() {
return this.clouds;
}
public Main getMainData_Object() {
return this.main;
}
public Sys getSysData_Object() {
return this.sys;
}
public Wind getWind_Object() {
return this.wind;
}
// Lists
public boolean hasWeather_List() {
return (this.weatherListCount != 0);
}
public int getWeather_List_Count() {
return this.weatherListCount;
}
public List<Weather> getWeather_List() {
return this.weatherList;
}
}
/*
***********************
* Declaring this class
***********************
*/
/** Key for JSON variable <code>Response code</code> */
private final String JSON_RESPONSE_CODE = "cod";
/** Key for JSON variable <code>Response time</code> */
private final String JSON_RESPONSE_TIME = "message";
/** Key for JSON variable <code>Forecast count</code> */
private final String JSON_RESPONSE_FORECAST_COUNT = "cnt";
/** Response code */
private final String responseCode;
/** Response time */
private final float responseTime;
/** Forecast count */
private final int responseForecastCount;
private final City city;
/** List of forecast information */
private final List<Forecast> forecastList;
/** Count (number) of elements in list of forecast information */
private final int forecastListCount;
/**
* Parameterized constructor
*
* Initializes variables from values from the given JSON object.
*
* @param jsonObj JSON object containing data about clouds
*/
public ForecastWeatherData(JSONObject jsonObj) {
this.responseCode = (jsonObj != null) ? jsonObj.optString(this.JSON_RESPONSE_CODE, null) : null;
this.responseTime = (jsonObj != null) ? (float) jsonObj.optDouble(this.JSON_RESPONSE_TIME, Double.NaN) : Float.NaN;
this.responseForecastCount = (jsonObj != null) ? jsonObj.optInt(this.JSON_RESPONSE_FORECAST_COUNT, Integer.MIN_VALUE) : Integer.MIN_VALUE;
JSONObject jsonObjCity = (jsonObj != null) ? jsonObj.optJSONObject(this.JSON_CITY) : null;
this.city = (jsonObjCity != null) ? new City(jsonObjCity) : new City();
JSONArray jsonArrForecast = (jsonObj != null) ? jsonObj.optJSONArray(this.JSON_FORECAST_LIST) : null;
this.forecastList = (jsonArrForecast != null) ? new ArrayList<Forecast>(jsonArrForecast.length()) : Collections.EMPTY_LIST;
if (this.forecastList != Collections.EMPTY_LIST) {
for (int i = 0; i < jsonArrForecast.length(); i++) {
JSONObject jsonObjWeather = jsonArrForecast.optJSONObject(i);
if (jsonObjWeather != null) {
this.forecastList.add(new Forecast(jsonObjWeather));
}
}
}
this.forecastListCount = this.forecastList.size();
}
public boolean hasResponseCode() {
return (this.responseCode != null);
}
public boolean hasResponseTime() {
return (this.responseTime != Float.NaN);
}
public boolean hasResponseForecastCount() {
return (this.responseForecastCount != Integer.MIN_VALUE);
}
public String getResponseCode() {
return this.responseCode;
}
public float getResponseTime() {
return this.responseTime;
}
public int getResponseForecastCount() {
return this.responseForecastCount;
}
// Objects
public City getCity_Object() {
return this.city;
}
// Lists
public boolean hasForecast_List() {
return (this.forecastListCount != 0);
}
public int getForecast_List_Count() {
return this.forecastListCount;
}
public List<Forecast> getForecast_List() {
return this.forecastList;
}
}

View file

@ -0,0 +1,551 @@
/*
* Copyright (C)2013 Ashutosh Kumar Singh [me@AKSingh.net]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package net.aksingh.java.api.owm;
import java.io.IOException;
import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Provides methods to get weather, forecast, and other data from
* OpenWeatherMap.org
*
* @author Ashutosh Kumar Singh
* @version 2013/08/05
* @since 2.5.0.1
*/
public class OpenWeatherMap {
public static enum OWM_URL {
// Base URL for OpenWeatherMap.org API 2.5
BASE_URL ("http://api.openweathermap.org/data/2.5/"),
// Parameters
PARAMETER_CURRENT_WEATHER ("weather?"),
PARAMETER_FORECAST_WEATHER ("forecast?"),
PARAMETER_DAILY_FORECAST ("forecast/daily?"),
PARAMETER_SEARCH_CITY ("find?"),
PARAMETER_COUNT ("cnt="),
PARAMETER_CITY_NAME ("q="),
PARAMETER_CITY_ID ("id="),
PARAMETER_LATITUDE ("lat="),
PARAMETER_LONGITUDE ("lon="),
PARAMETER_MODE ("mode="),
PARAMETER_UNITS ("units="),
PARAMETER_APPID ("APPID="),
// Possible values of parameters
PARAMETER_MODE_VALUE_JSON ("json"),
PARAMETER_UNITS_VALUE_IMPERIAL ("imperial"),
PARAMETER_UNITS_VALUE_METRIC ("metric");
private final String value;
private OWM_URL(String val) {
this.value = val;
}
/**
* Returns the parameter.
* @return Parameter
*/
public String getParameter() {
return this.value;
}
}
public static class OWM_Response {
private final String OWM_MODE_VALUE;
private final String OWM_UNITS_VALUE;
private final String OWM_APPID_VALUE;
public OWM_Response(String apiKey) {
this(OWM_URL.PARAMETER_UNITS_VALUE_IMPERIAL, apiKey);
}
public OWM_Response(OWM_URL unit, String apiKey) {
this.OWM_MODE_VALUE = OWM_URL.PARAMETER_MODE_VALUE_JSON.getParameter();
this.OWM_UNITS_VALUE = unit.getParameter();
this.OWM_APPID_VALUE = apiKey;
}
public String currentWeatherByCityName(String cityName)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_CURRENT_WEATHER.getParameter() +
OWM_URL.PARAMETER_CITY_NAME.getParameter() +
Tools.HTML.strictAddress(cityName) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String currentWeatherByCityName(String cityName, String countryCode)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_CURRENT_WEATHER.getParameter() +
OWM_URL.PARAMETER_CITY_NAME.getParameter() +
Tools.HTML.strictAddress(cityName) +
"," + countryCode + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String currentWeatherByCityCode(long cityCode)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_CURRENT_WEATHER.getParameter() +
OWM_URL.PARAMETER_CITY_ID.getParameter() +
Long.toString(cityCode) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String currentWeatherByCoordinates(float latitude, float longitude)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_CURRENT_WEATHER.getParameter() +
OWM_URL.PARAMETER_LATITUDE.getParameter() +
Float.toString(latitude) + "&" +
OWM_URL.PARAMETER_LONGITUDE.getParameter() +
Float.toString(longitude) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String forecastWeatherByCityName(String cityName)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_FORECAST_WEATHER.getParameter() +
OWM_URL.PARAMETER_CITY_NAME.getParameter() +
Tools.HTML.strictAddress(cityName) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String forecastWeatherByCityName(String cityName, String countryCode)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_FORECAST_WEATHER.getParameter() +
OWM_URL.PARAMETER_CITY_NAME.getParameter() +
Tools.HTML.strictAddress(cityName) +
"," + countryCode + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String forecastWeatherByCityCode(long cityCode)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_FORECAST_WEATHER.getParameter() +
OWM_URL.PARAMETER_CITY_ID.getParameter() +
Long.toString(cityCode) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String forecastWeatherByCoordinates(float latitude, float longitude)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_FORECAST_WEATHER.getParameter() +
OWM_URL.PARAMETER_LATITUDE.getParameter() +
Float.toString(latitude) + "&" +
OWM_URL.PARAMETER_LONGITUDE.getParameter() +
Float.toString(longitude) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String dailyForecastByCityName(String cityName, byte count)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_DAILY_FORECAST.getParameter() +
OWM_URL.PARAMETER_CITY_NAME.getParameter() +
Tools.HTML.strictAddress(cityName) + "&" +
OWM_URL.PARAMETER_COUNT.getParameter() +
Byte.toString(count) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String dailyForecastByCityName(String cityName, String countryCode, byte count)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_DAILY_FORECAST.getParameter() +
OWM_URL.PARAMETER_CITY_NAME.getParameter() +
Tools.HTML.strictAddress(cityName) +
"," + countryCode + "&" +
OWM_URL.PARAMETER_COUNT.getParameter() +
Byte.toString(count) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String dailyForecastByCityCode(long cityCode, byte count)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_DAILY_FORECAST.getParameter() +
OWM_URL.PARAMETER_CITY_ID.getParameter() +
Long.toString(cityCode) + "&" +
OWM_URL.PARAMETER_COUNT.getParameter() +
Byte.toString(count) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
public String dailyForecastByCoordinates(float latitude, float longitude, byte count)
throws MalformedURLException, IOException {
String url;
String response;
url = OWM_URL.BASE_URL.getParameter() +
OWM_URL.PARAMETER_DAILY_FORECAST.getParameter() +
OWM_URL.PARAMETER_LATITUDE.getParameter() +
Float.toString(latitude) + "&" +
OWM_URL.PARAMETER_COUNT.getParameter() +
Byte.toString(count) + "&" +
OWM_URL.PARAMETER_LONGITUDE.getParameter() +
Float.toString(longitude) + "&" +
OWM_URL.PARAMETER_MODE.getParameter() +
this.OWM_MODE_VALUE + "&" +
OWM_URL.PARAMETER_UNITS.getParameter() +
this.OWM_UNITS_VALUE + "&" +
OWM_URL.PARAMETER_APPID.getParameter() +
this.OWM_APPID_VALUE;
response = Tools.Downloader.downloadPage(url);
return response;
}
}
/***********************
* Declaring this class
***********************/
private final OWM_Response owmResponse;
public OpenWeatherMap(String apiKey) {
this(OWM_URL.PARAMETER_UNITS_VALUE_IMPERIAL, apiKey);
}
public OpenWeatherMap(OWM_URL unit, String apiKey) {
this.owmResponse = new OWM_Response(unit, apiKey);
}
public CurrentWeatherData currentWeatherByCityName(String cityName)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
CurrentWeatherData cwd;
jsonResponse = owmResponse.currentWeatherByCityName(cityName);
cwd = this.currentWeatherFromResponse(jsonResponse);
return cwd;
}
public CurrentWeatherData currentWeatherByCityName(String cityName, String countryCode)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
CurrentWeatherData cwd;
jsonResponse = owmResponse.currentWeatherByCityName(cityName, countryCode);
cwd = this.currentWeatherFromResponse(jsonResponse);
return cwd;
}
public CurrentWeatherData currentWeatherByCityCode(long cityCode)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
CurrentWeatherData cwd;
jsonResponse = owmResponse.currentWeatherByCityCode(cityCode);
cwd = this.currentWeatherFromResponse(jsonResponse);
return cwd;
}
public CurrentWeatherData currentWeatherByCoordinates(float latitude, float longitude)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
CurrentWeatherData cwd;
jsonResponse = owmResponse.currentWeatherByCoordinates(latitude, longitude);
cwd = this.currentWeatherFromResponse(jsonResponse);
return cwd;
}
public CurrentWeatherData currentWeatherFromResponse(String jsonResponse)
throws MalformedURLException, IOException, JSONException {
JSONObject jsonObj;
CurrentWeatherData cwd;
jsonObj = (jsonResponse != null) ? new JSONObject(jsonResponse) : null;
cwd = new CurrentWeatherData(jsonObj);
return cwd;
}
public ForecastWeatherData forecastWeatherByCityName(String cityName)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
ForecastWeatherData fwd;
jsonResponse = owmResponse.forecastWeatherByCityName(cityName);
fwd = this.forecastWeatherFromResponse(jsonResponse);
return fwd;
}
public ForecastWeatherData forecastWeatherByCityName(String cityName, String countryCode)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
ForecastWeatherData fwd;
jsonResponse = owmResponse.forecastWeatherByCityName(cityName, countryCode);
fwd = this.forecastWeatherFromResponse(jsonResponse);
return fwd;
}
public ForecastWeatherData forecastWeatherByCityCode(long cityCode)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
ForecastWeatherData fwd;
jsonResponse = owmResponse.forecastWeatherByCityCode(cityCode);
fwd = this.forecastWeatherFromResponse(jsonResponse);
return fwd;
}
public ForecastWeatherData forecastWeatherByCoordinates(float latitude, float longitude)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
ForecastWeatherData fwd;
jsonResponse = owmResponse.forecastWeatherByCoordinates(latitude, longitude);
fwd = this.forecastWeatherFromResponse(jsonResponse);
return fwd;
}
public ForecastWeatherData forecastWeatherFromResponse(String jsonResponse)
throws MalformedURLException, IOException, JSONException {
JSONObject jsonObj;
ForecastWeatherData fwd;
jsonObj = (jsonResponse != null) ? new JSONObject(jsonResponse) : null;
fwd = new ForecastWeatherData(jsonObj);
return fwd;
}
public DailyForecastData dailyForecastByCityName(String cityName, byte count)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
DailyForecastData dfd;
jsonResponse = owmResponse.dailyForecastByCityName(cityName, count);
dfd = this.dailyForecastFromResponse(jsonResponse);
return dfd;
}
public DailyForecastData dailyForecastByCityName(String cityName, String countryCode, byte count)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
DailyForecastData dfd;
jsonResponse = owmResponse.dailyForecastByCityName(cityName, countryCode, count);
dfd = this.dailyForecastFromResponse(jsonResponse);
return dfd;
}
public DailyForecastData dailyForecastByCityCode(long cityCode, byte count)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
DailyForecastData dfd;
jsonResponse = owmResponse.dailyForecastByCityCode(cityCode, count);
dfd = this.dailyForecastFromResponse(jsonResponse);
return dfd;
}
public DailyForecastData dailyForecastByCoordinates(float latitude, float longitude, byte count)
throws MalformedURLException, IOException, JSONException {
String jsonResponse;
DailyForecastData dfd;
jsonResponse = owmResponse.dailyForecastByCoordinates(latitude, longitude, count);
dfd = this.dailyForecastFromResponse(jsonResponse);
return dfd;
}
public DailyForecastData dailyForecastFromResponse(String jsonResponse)
throws MalformedURLException, IOException, JSONException {
JSONObject jsonObj;
DailyForecastData dfd;
jsonObj = (jsonResponse != null) ? new JSONObject(jsonResponse) : null;
dfd = new DailyForecastData(jsonObj);
return dfd;
}
}

View file

@ -0,0 +1,209 @@
/*
* Copyright (C)2013 Ashutosh Kumar Singh [me@AKSingh.net]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package net.aksingh.java.api.owm;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
/**
* Provides various tools, which help doing tasks in this application.
* For example, tool for downloading content from the Internet, tool for
* correcting and stricting web addresses, etc.
*
* <p>Note: This class directly do not provide any functions, but has
* <code>static</code> sub-classes which provide various
* related tools, i.e., this class only behaves as the container for those
* classes.
*
* @author Ashutosh Kumar Singh
* @version 2013-08-05
* @since 2.5.0.1
*/
public class Tools {
/**
* Provides methods to download data or files from the Internet.
*
* @author Ashutosh Kumar Singh
* @version 2013-07-24
* @since 2.5.0.1
*/
public static class Downloader {
/**
* Downloads a page/content from the Internet.
* This method gets the content of the web page, whose URL is given by
* the <code>pageAddress</code>.
*
* <p>NOTE: <code>pageAddress</code> should be a correct URL, else
* this method will throw {@link MalformedURLException}.
*
* @param pageAddress
* Address of the web page to get from the Internet.
*
* @return Content of the web page
*
* @throws MalformedURLException
* Address of the web page is not correct.
*
* @throws IOException
* Error while loading the page from the Internet or connection
* got disconnected.
*/
public static String downloadPage(String pageAddress)
throws MalformedURLException, IOException {
String webPage = null;
URL url = new URL(pageAddress);
InputStream iStream = url.openStream();
if (iStream != null) {
// converting InputStream to String (as we require String)
webPage = (new Scanner(iStream)).useDelimiter("\\A").next();
}
iStream.close();
return webPage;
}
}
/**
* Provides functions to handle HTML, e.g., correcting
* non-compliant or non-strict web addresses, etc.
*
* @author Ashutosh Kumar Singh
* @version 2013/08/05
* @since 2.5.0.1
*/
public static class HTML {
/**
* Stricts a non-strict or non-compliant web address
* to a proper URL.
*
* <p>Note: This function checks and stricts only some parts of the
* URL, which are necessary to be corrected for accessing data from
* the OpenWeatherMap.org
*
* @param inputString String to be converted to strict HTML address
* @return HTML standards-followed and stricted URL
*/
public static String strictAddress(String inputString) {
String outputString = "";
if (inputString != null) {
outputString = inputString.replace(" ", "%20").replace(".", "%2E");
}
return outputString;
}
}
/**
* Provides methods to do conversions.
* For example, converting degree to direction, etc.
*
* @author Ashutosh Kumar Singh
* @version 2013/08/05
* @since 2.5.0.1
*/
public static class Convertor {
/**
* Converts degree to direction.
* @return Direction
*
* @throws IllegalArgumentException
* Degree should be between 0 and 360.
*/
public String convertDegree2Direction(float degree)
throws IllegalArgumentException{
String direction;
// degree should be between 0 and 360
if ((degree < 0.0f) || (degree > 360.0f)) {
throw new IllegalArgumentException("Degree cannot be less than 0 or more than 360.");
}
if (degree <= 11.25f) {
direction = "N";
}
else if (degree <= 33.75f) {
direction = "NNE";
}
else if (degree <= 56.25f) {
direction = "NE";
}
else if (degree <= 78.75f) {
direction = "ENE";
}
else if (degree <= 101.25f) {
direction = "E";
}
else if (degree <= 123.75f) {
direction = "ESE";
}
else if (degree <= 146.25f) {
direction = "SE";
}
else if (degree <= 168.75f) {
direction = "SSE";
}
else if (degree <= 191.25f) {
direction = "S";
}
else if (degree <= 213.75f) {
direction = "SSW";
}
else if (degree <= 236.25f) {
direction = "SW";
}
else if (degree <= 258.75f) {
direction = "WSW";
}
else if (degree <= 281.25f) {
direction = "W";
}
else if (degree <= 303.75f) {
direction = "WNW";
}
else if (degree <= 326.25f) {
direction = "NW";
}
else if (degree <= 348.75f) {
direction = "NNW";
}
else {
direction = "N";
}
return direction;
}
}
}