/*
* Copyright (C)2013-2014 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 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 has
and get
methods
* to access the information.
*
*
has
methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
*
get
methods can be used to access the data, if the data
* exists, otherwise get
methods will give value as per
* following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*
* @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 has
and get
methods
* to access the information.
*
*
has
methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
*
get
methods can be used to access the data, if the data
* exists, otherwise get
methods will give value as per
* following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Clouds extends AbstractWeatherData.Clouds {
/**
* Non-parameterized constructor
*
*
Initializes variables as per following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*/
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 has
and get
methods
* to access the information.
*
*
has
methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
*
get
methods can be used to access the data, if the data
* exists, otherwise get
methods will give value as per
* following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Coord extends AbstractWeatherData.Coord {
/**
* Non-parameterized constructor
*
*
Initializes variables as per following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*/
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 has
and get
methods
* to access the information.
*
*
has
methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
*
get
methods can be used to access the data, if the data
* exists, otherwise get
methods will give value as per
* following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Main extends AbstractWeatherData.Main {
/**
* Non-parameterized constructor
*
*
Initializes variables as per following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*/
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 has
and get
methods
* to access the information.
*
*
has
methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
*
get
methods can be used to access the data, if the data
* exists, otherwise get
methods will give value as per
* following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Rain {
/** Key for JSON variable Rain -> Rain per 3 hours
*/
private final String JSON_RAIN_3HOURS = "3h";
/** Rain per 3 hours */
private final float rain3h;
/**
* Non-parameterized constructor
*
*
Initializes variables as per following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*/
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 has
and get
methods
* to access the information.
*
*
has
methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
*
get
methods can be used to access the data, if the data
* exists, otherwise get
methods will give value as per
* following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Sys {
/** Key for JSON variable Sys -> Country
*/
private final String JSON_SYS_COUNTRY_CODE = "country";
/** Key for JSON variable Sys -> Sunrise
*/
private final String JSON_SYS_SUNRISE = "sunrise";
/** Key for JSON variable Sys -> Sunset
*/
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
*
*
Initializes variables as per following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*/
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 has
and get
methods
* to access the information.
*
*
has
methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
*
get
methods can be used to access the data, if the data
* exists, otherwise get
methods will give value as per
* following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Weather extends AbstractWeatherData.Weather {
/**
* Non-parameterized constructor
*
*
Initializes variables as per following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*/
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 has
and get
methods
* to access the information.
*
*
has
methods can be used to check
* if the data exists, i.e., if the data was available (successfully
* downloaded) and was parsed correctly.
*
*
get
methods can be used to access the data, if the data
* exists, otherwise get
methods will give value as per
* following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*
* @author Ashutosh Kumar Singh
* @version 2013/08/07
* @since 2.5.0.1
*/
public static class Wind extends AbstractWeatherData.Wind {
/** Key for JSON variable Wind -> Gust
*/
private final String JSON_WIND_GUST = "gust";
/** Wind gust */
private final float gust;
/**
* Non-parameterized constructor
*
*
Initializes variables as per following basis:
* Boolean: false
* Integral: Minimum value (MIN_VALUE)
* Floating point: Not a number (NaN)
* Others: null
*/
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 Base
*/
private final String JSON_BASE = "base";
/** Key for JSON variable City code (ID)
*/
private final String JSON_CITY_ID = "id";
/** Key for JSON variable City name
*/
private final String JSON_CITY_NAME = "name";
/** Key for JSON variable Response 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