From fdcbbc37926ebf108cc7408f9279f15d83f1c75c Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Thu, 9 Nov 2017 03:08:15 +0530 Subject: [PATCH] 2.5.1.0: Removed the old code in Java --- .../aksingh/owmjapis/AbstractForecast.java | 246 ------ .../net/aksingh/owmjapis/AbstractWeather.java | 517 ------------ .../net/aksingh/owmjapis/CurrentWeather.java | 610 -------------- .../net/aksingh/owmjapis/DailyForecast.java | 316 ------- .../net/aksingh/owmjapis/HourlyForecast.java | 375 --------- .../net/aksingh/owmjapis/OpenWeatherMap.java | 778 ------------------ src/main/java/net/aksingh/owmjapis/Tools.java | 91 -- 7 files changed, 2933 deletions(-) delete mode 100644 src/main/java/net/aksingh/owmjapis/AbstractForecast.java delete mode 100644 src/main/java/net/aksingh/owmjapis/AbstractWeather.java delete mode 100644 src/main/java/net/aksingh/owmjapis/CurrentWeather.java delete mode 100644 src/main/java/net/aksingh/owmjapis/DailyForecast.java delete mode 100644 src/main/java/net/aksingh/owmjapis/HourlyForecast.java delete mode 100644 src/main/java/net/aksingh/owmjapis/OpenWeatherMap.java delete mode 100644 src/main/java/net/aksingh/owmjapis/Tools.java diff --git a/src/main/java/net/aksingh/owmjapis/AbstractForecast.java b/src/main/java/net/aksingh/owmjapis/AbstractForecast.java deleted file mode 100644 index 6729717..0000000 --- a/src/main/java/net/aksingh/owmjapis/AbstractForecast.java +++ /dev/null @@ -1,246 +0,0 @@ -/************************************************************************************************** - * Copyright (c) 2013-2017 Ashutosh Kumar Singh * - * * - * 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.owmjapis; - -import org.json.JSONObject; - -import java.io.Serializable; - -/** - *

- * Provides default behaviours and implementations for: - * 1. {@link net.aksingh.owmjapis.HourlyForecast} - * 2. {@link net.aksingh.owmjapis.DailyForecast} - * It defines common methods like has, get and some others. - *

- * - * @author Ashutosh Kumar Singh - * @version 2014/12/27 - * @since 2.5.0.3 - */ -public abstract class AbstractForecast extends AbstractResponse { - /* - JSON Keys - */ - static final String JSON_FORECAST_LIST = "list"; - static final String JSON_MESSAGE = "message"; - static final String JSON_CITY = "city"; - static final String JSON_FORECAST_COUNT = "cnt"; - - /* - Instance variables - */ - private final double message; - - private final City city; - private final int forecastCount; - - /* - Constructors - */ - AbstractForecast() { - super(); - - this.message = Double.NaN; - this.forecastCount = 0; - this.city = null; - } - - AbstractForecast(JSONObject jsonObj) { - super(jsonObj); - - this.message = (jsonObj != null) ? jsonObj.optDouble(JSON_MESSAGE, Double.NaN) : Double.NaN; - - this.city = (jsonObj != null) ? new City(jsonObj.optJSONObject(JSON_CITY)) : null; - - this.forecastCount = (jsonObj != null) ? jsonObj.optInt(JSON_FORECAST_COUNT, 0) : 0; - } - - /** - * @return true if message is available, otherwise false. - */ - public boolean hasMessage() { - return (this.message != Double.NaN); - } - - /** - * @return true if count of forecasts is available, otherwise false. - */ - public boolean hasForecastCount() { - return (this.forecastCount != 0); - } - - /** - * @return true if message is available, otherwise false. - */ - public boolean hasCityInstance() { - return (this.city != null); - } - - /** - * @return Message if available, otherwise Double.NaN. - */ - public double getMessage() { - return this.message; - } - - /** - * @return Count of forecasts if available, otherwise 0. - */ - public int getForecastCount() { - return this.forecastCount; - } - - /** - * @return City's instance if available, otherwise null. - */ - public City getCityInstance() { - return this.city; - } - - /** - *

- * Provides default behaviours for City - *

- * - * @author Ashutosh Kumar Singh - */ - public static class City implements Serializable { - private static final String JSON_CITY_ID = "id"; - private static final String JSON_CITY_NAME = "name"; - private static final String JSON_CITY_COUNTRY_CODE = "country"; - private static final String JSON_CITY_POPULATION = "population"; - private static final String JSON_CITY_COORD = "coord"; - - private final long cityID; - private final String cityName; - private final String countryCode; - private final long population; - - private final Coord coord; - - City() { - this.cityID = Long.MIN_VALUE; - this.cityName = null; - this.countryCode = null; - this.population = Long.MIN_VALUE; - - this.coord = new Coord(); - } - - City(JSONObject jsonObj) { - this.cityID = (jsonObj != null) ? jsonObj.optLong(JSON_CITY_ID, Long.MIN_VALUE) : Long.MIN_VALUE; - this.cityName = (jsonObj != null) ? jsonObj.optString(JSON_CITY_NAME, null) : null; - this.countryCode = (jsonObj != null) ? jsonObj.optString(JSON_CITY_COUNTRY_CODE, null) : null; - this.population = (jsonObj != null) ? jsonObj.optLong(JSON_CITY_POPULATION, Long.MIN_VALUE) : Long.MIN_VALUE; - - JSONObject jsonObjCoord = (jsonObj != null) ? jsonObj.optJSONObject(JSON_CITY_COORD) : null; - this.coord = (jsonObjCoord != null) ? new Coord(jsonObjCoord) : null; - } - - 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; - } - - /** - * @return true if Coord instance is available, otherwise false. - */ - public boolean hasCoordInstance() { - return coord != null; - } - - public long getCityCode() { - return this.cityID; - } - - public String getCityName() { - return this.cityName; - } - - public String getCountryCode() { - return this.countryCode; - } - - public long getCityPopulation() { - return this.population; - } - - /** - * @return Coord instance if available, otherwise null. - */ - public Coord getCoordInstance() { - return this.coord; - } - - - public static class Coord extends AbstractWeather.Coord { - - Coord() { - super(); - } - - Coord(JSONObject jsonObj) { - super(jsonObj); - } - } - } - - /** - *

- * Parses forecast data (one element in the forecastList) and provides methods to get/access the same 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 2014/12/27 - * @since 2.5.0.3 - */ - public abstract static class Forecast extends AbstractWeather { - Forecast() { - super(); - } - - Forecast(JSONObject jsonObj) { - super(jsonObj); - } - } -} diff --git a/src/main/java/net/aksingh/owmjapis/AbstractWeather.java b/src/main/java/net/aksingh/owmjapis/AbstractWeather.java deleted file mode 100644 index 7d89a5b..0000000 --- a/src/main/java/net/aksingh/owmjapis/AbstractWeather.java +++ /dev/null @@ -1,517 +0,0 @@ -/************************************************************************************************** - * Copyright (c) 2013-2017 Ashutosh Kumar Singh * - * * - * 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.owmjapis; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; - -/** - *

- * Provides default behaviours and implementations for: - * 1. {@link net.aksingh.owmjapis.CurrentWeather} - * It defines common methods like has, get and some others. - *

- * - * @author Ashutosh Kumar Singh - * @version 2014/12/21 - * @since 2.5.0.1 - */ -public abstract class AbstractWeather extends AbstractResponse { - /* - JSON Keys - */ - static final String JSON_CLOUDS = "clouds"; - static final String JSON_COORD = "coord"; - static final String JSON_MAIN = "main"; - static final String JSON_WIND = "windData"; - private static final String JSON_WEATHER = "weather"; - private static final String JSON_DATE_TIME = "dt"; - - /* - Instance variables - */ - private final Date dateTime; - - private final int weatherCount; - private final List weatherList; - - /* - Constructors - */ - AbstractWeather() { - super(); - - this.weatherCount = 0; - this.weatherList = null; - this.dateTime = null; - } - - AbstractWeather(JSONObject jsonObj) { - super(jsonObj); - - long sec = (jsonObj != null) ? jsonObj.optLong(JSON_DATE_TIME, Long.MIN_VALUE) : Long.MIN_VALUE; - if (sec != Long.MIN_VALUE) { // converting seconds to Date object - this.dateTime = new Date(sec * 1000); - } else { - this.dateTime = null; - } - - JSONArray weatherArray = (jsonObj != null) ? jsonObj.optJSONArray(JSON_WEATHER) : new JSONArray(); - this.weatherList = (weatherArray != null) ? new ArrayList(weatherArray.length()) : Collections.EMPTY_LIST; - if (weatherArray != null && this.weatherList != Collections.EMPTY_LIST) { - for (int i = 0; i < weatherArray.length(); i++) { - JSONObject weatherObj = weatherArray.optJSONObject(i); - if (weatherObj != null) { - this.weatherList.add(new Weather(weatherObj)); - } - } - } - this.weatherCount = this.weatherList.size(); - } - - /** - * @return true if date/time is available, otherwise false. - */ - public boolean hasDateTime() { - return this.dateTime != null; - } - - /** - * @return true if Weather instance(s) is available, otherwise false. - */ - public boolean hasWeatherInstance() { - return weatherCount != 0; - } - - /** - * @return Date and time if available, otherwise null. - */ - public Date getDateTime() { - return this.dateTime; - } - - /** - * @return Count of Weather instance(s) if available, otherwise 0. - */ - public int getWeatherCount() { - return this.weatherCount; - } - - /** - * @param index Index of Weather instance in the list. - * @return Weather instance if available, otherwise null. - */ - public Weather getWeatherInstance(int index) { - return this.weatherList.get(index); - } - - /** - *

- * Provides default behaviours for Cloud - *

- * - * @author Ashutosh Kumar Singh - * @version 2013/12/23 - * @since 2.5.0.1 - */ - abstract public static class Clouds implements Serializable { - private static final String JSON_CLOUDS_ALL = "all"; - - private final float percentOfClouds; - - Clouds() { - this.percentOfClouds = Float.NaN; - } - - Clouds(JSONObject jsonObj) { - this.percentOfClouds = (float) jsonObj.optDouble(JSON_CLOUDS_ALL, Double.NaN); - } - - /** - * Tells if percentage of clouds is available or not. - * - * @return true if data available, otherwise false - */ - public boolean hasPercentageOfClouds() { - return !Float.isNaN(this.percentOfClouds); - } - - /** - * @return Percentage of all clouds if available, otherwise Float.NaN. - */ - public float getPercentageOfClouds() { - return this.percentOfClouds; - } - } - - /** - *

- * Provides default behaviours for Coord, i.e., coordinates. - *

- * - * @author Ashutosh Kumar Singh - * @version 2013/12/23 - * @since 2.5.0.1 - */ - abstract public static class Coord implements Serializable { - private static final String JSON_COORD_LATITUDE = "lat"; - private static final String JSON_COORD_LONGITUDE = "lon"; - - private final float lat; - private final float lon; - - Coord() { - this.lat = Float.NaN; - this.lon = Float.NaN; - } - - Coord(JSONObject jsonObj) { - this.lat = (float) jsonObj.optDouble(JSON_COORD_LATITUDE, Double.NaN); - this.lon = (float) jsonObj.optDouble(JSON_COORD_LONGITUDE, Double.NaN); - } - - /** - * Tells if the latitude of the city is available or not. - * - * @return true if data available, otherwise false - */ - public boolean hasLatitude() { - return !Float.isNaN(this.lat); - } - - /** - * Tells if the longitude of the city is available or not. - * - * @return true if data available, otherwise false - */ - public boolean hasLongitude() { - return !Float.isNaN(this.lon); - } - - /** - * @return Latitude of the city if available, otherwise Float.NaN. - */ - public float getLatitude() { - return this.lat; - } - - /** - * @return Longitude of the city if available, otherwise Float.NaN. - */ - public float getLongitude() { - return this.lon; - } - } - - /** - *

- * Provides default behaviours for Main, i.e., main weather elements like temperature, humidity, etc. - *

- * - * @author Ashutosh Kumar Singh - * @version 2013/12/23 - * @since 2.5.0.1 - */ - abstract public static class Main implements Serializable { - - private static final String JSON_MAIN_TEMP = "temp"; - private static final String JSON_MAIN_TEMP_MIN = "temp_min"; - private static final String JSON_MAIN_TEMP_MAX = "temp_max"; - private static final String JSON_MAIN_PRESSURE = "pressure"; - private static final String JSON_MAIN_HUMIDITY = "humidity"; - - private final float temp; - private final float minTemp; - private final float maxTemp; - private final float pressure; - private final float humidity; - - Main() { - this.temp = Float.NaN; - this.minTemp = Float.NaN; - this.maxTemp = Float.NaN; - this.pressure = Float.NaN; - this.humidity = Float.NaN; - } - - Main(JSONObject jsonObj) { - this.temp = (float) jsonObj.optDouble(JSON_MAIN_TEMP, Double.NaN); - this.minTemp = (float) jsonObj.optDouble(JSON_MAIN_TEMP_MIN, Double.NaN); - this.maxTemp = (float) jsonObj.optDouble(JSON_MAIN_TEMP_MAX, Double.NaN); - this.pressure = (float) jsonObj.optDouble(JSON_MAIN_PRESSURE, Double.NaN); - this.humidity = (float) jsonObj.optDouble(JSON_MAIN_HUMIDITY, Double.NaN); - } - - /** - * Tells if the temperature of the city is available or not. - * - * @return true if data available, otherwise false - */ - public boolean hasTemperature() { - return !Float.isNaN(this.temp); - } - - /** - * Tells if the minimum temperature of the city is available or not. - * - * @return true if data available, otherwise false - */ - public boolean hasMinTemperature() { - return !Float.isNaN(this.minTemp); - } - - /** - * Tells if the maximum temperature of the city is available or not. - * - * @return true if data available, otherwise false - */ - public boolean hasMaxTemperature() { - return !Float.isNaN(this.maxTemp); - } - - /** - * Tells if pressure of the city is available or not. - * - * @return true if data available, otherwise false - */ - public boolean hasPressure() { - return !Float.isNaN(this.pressure); - } - - /** - * Tells if humidity of the city is available or not. - * - * @return true if data available, otherwise false - */ - public boolean hasHumidity() { - return !Float.isNaN(this.humidity); - } - - /** - * @return Temperature of the city if available, otherwise Float.NaN. - */ - public float getTemperature() { - return this.temp; - } - - /** - * @return Minimum temperature of the city if available, otherwise Float.NaN. - */ - public float getMinTemperature() { - return this.minTemp; - } - - /** - * @return Maximum temperature of the city if available, otherwise Float.NaN. - */ - public float getMaxTemperature() { - return this.maxTemp; - } - - /** - * @return Pressure of the city if available, otherwise Float.NaN. - */ - public float getPressure() { - return this.pressure; - } - - /** - * @return Humidity of the city if available, otherwise Float.NaN. - */ - public float getHumidity() { - return this.humidity; - } - } - - /** - *

- * Parses weather data and provides methods to get/access the same 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 2014/12/27 - * @since 2.5.0.3 - */ - public static class Weather implements Serializable { - private static final String JSON_WEATHER_ID = "id"; - private static final String JSON_WEATHER_MAIN = "main"; - private static final String JSON_WEATHER_DESCRIPTION = "description"; - private static final String JSON_WEATHER_ICON = "icon"; - - private final int id; - private final String name; - private final String description; - private final String icon; - - Weather() { - this.id = Integer.MIN_VALUE; - this.name = null; - this.description = null; - this.icon = null; - } - - Weather(JSONObject jsonObj) { - this.id = jsonObj.optInt(JSON_WEATHER_ID, Integer.MIN_VALUE); - this.name = jsonObj.optString(JSON_WEATHER_MAIN, null); - this.description = jsonObj.optString(JSON_WEATHER_DESCRIPTION, null); - this.icon = jsonObj.optString(JSON_WEATHER_ICON, null); - } - - /** - * Tells if weather's code is available or not. - * - * @return true if data available, otherwise false. - */ - public boolean hasWeatherCode() { - return this.id != Integer.MIN_VALUE; - } - - /** - * Tells if weather's name is available or not. - * - * @return true if data available, otherwise false. - */ - public boolean hasWeatherName() { - return this.name != null && (!"".equals(this.name)); - } - - /** - * Tells if weather's description is available or not. - * - * @return true if data available, otherwise false. - */ - public boolean hasWeatherDescription() { - return this.description != null && (!"".equals(this.description)); - } - - /** - * Tells if name of weather's icon is available or not. - * - * @return true if data available, otherwise false. - */ - public boolean hasWeatherIconName() { - return this.icon != null && (!"".equals(this.icon)); - } - - /** - * @return Code for weather of the city if available, otherwise Integer.MIN_VALUE. - */ - public int getWeatherCode() { - return this.id; - } - - /** - * @return Name for weather of the city if available, otherwise null. - */ - public String getWeatherName() { - return this.name; - } - - /** - * @return Description for weather of the city if available, otherwise null. - */ - public String getWeatherDescription() { - return this.description; - } - - /** - * @return Name of icon for weather of the city if available, otherwise null. - */ - public String getWeatherIconName() { - return this.icon; - } - } - - /** - *

- * Provides default behaviours for Wind. - *

- * - * @author Ashutosh Kumar Singh - * @version 2013/12/23 - * @since 2.5.0.1 - */ - abstract public static class Wind implements Serializable { - private static final String JSON_WIND_SPEED = "speed"; - private static final String JSON_WIND_DEGREE = "deg"; - - private final float speed; - private final float degree; - - Wind() { - this.speed = Float.NaN; - this.degree = Float.NaN; - } - - Wind(JSONObject jsonObj) { - this.speed = (float) jsonObj.optDouble(JSON_WIND_SPEED, Double.NaN); - this.degree = (float) jsonObj.optDouble(JSON_WIND_DEGREE, Double.NaN); - } - - /** - * Tells if speed of windData in the city is available or not. - * - * @return true if data available, otherwise false. - */ - public boolean hasWindSpeed() { - return !Float.isNaN(this.speed); - } - - /** - * Tells if degree (degree gives direction) of windData in the city is available or not. - * - * @return true if data available, otherwise false. - */ - public boolean hasWindDegree() { - return this.hasWindSpeed() && (!Float.isNaN(this.degree)); - } - - /** - * @return Speed of windData in the city if available, otherwise Float.NaN. - */ - public float getWindSpeed() { - return this.speed; - } - - /** - * @return Degree of windData in the city if available, otherwise Float.NaN. - */ - public float getWindDegree() { - return this.degree; - } - } -} diff --git a/src/main/java/net/aksingh/owmjapis/CurrentWeather.java b/src/main/java/net/aksingh/owmjapis/CurrentWeather.java deleted file mode 100644 index f54366f..0000000 --- a/src/main/java/net/aksingh/owmjapis/CurrentWeather.java +++ /dev/null @@ -1,610 +0,0 @@ -/************************************************************************************************** - * Copyright (c) 2013-2017 Ashutosh Kumar Singh * - * * - * 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.owmjapis; - -import org.json.JSONObject; - -import java.io.Serializable; -import java.util.Date; - -/** - *

- * Parses current weather data and provides methods to get/access the same 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 2014/12/26 - * @see OWM's Current Weather API - * @since 2.5.0.1 - */ -public class CurrentWeather extends AbstractWeather { - /* - JSON Keys - */ - private static final String JSON_RAIN = "rain"; - private static final String JSON_SNOW = "snow"; - private static final String JSON_SYS = "sys"; - private static final String JSON_BASE = "base"; - private static final String JSON_CITY_ID = "id"; - private static final String JSON_CITY_NAME = "name"; - - /* - Instance variables - */ - private final String base; - private final long cityId; - private final String cityName; - - private final Clouds clouds; - private final Coord coord; - private final Main main; - private final Rain rain; - private final Snow snow; - private final Sys sys; - private final Wind wind; - - /* - Constructor - */ - CurrentWeather(JSONObject jsonObj) { - super(jsonObj); - - this.base = (jsonObj != null) ? jsonObj.optString(JSON_BASE, null) : null; - this.cityId = (jsonObj != null) ? jsonObj.optLong(JSON_CITY_ID, Long.MIN_VALUE) : Long.MIN_VALUE; - this.cityName = (jsonObj != null) ? jsonObj.optString(JSON_CITY_NAME, null) : null; - - JSONObject cloudsObj = (jsonObj != null) ? jsonObj.optJSONObject(JSON_CLOUDS) : null; - this.clouds = (cloudsObj != null) ? new Clouds(cloudsObj) : null; - - JSONObject coordObj = (jsonObj != null) ? jsonObj.optJSONObject(JSON_COORD) : null; - this.coord = (coordObj != null) ? new Coord(coordObj) : null; - - JSONObject mainObj = (jsonObj != null) ? jsonObj.optJSONObject(JSON_MAIN) : null; - this.main = (mainObj != null) ? new Main(mainObj) : null; - - JSONObject rainObj = (jsonObj != null) ? jsonObj.optJSONObject(JSON_RAIN) : null; - this.rain = (rainObj != null) ? new Rain(rainObj) : null; - - JSONObject snowObj = (jsonObj != null) ? jsonObj.optJSONObject(JSON_SNOW) : null; - this.snow = (snowObj != null) ? new Snow(snowObj) : null; - - JSONObject sysObj = (jsonObj != null) ? jsonObj.optJSONObject(JSON_SYS) : null; - this.sys = (sysObj != null) ? new Sys(sysObj) : null; - - JSONObject windObj = (jsonObj != null) ? jsonObj.optJSONObject(JSON_WIND) : null; - this.wind = (windObj != null) ? new Wind(windObj) : null; - } - - /** - * @return true if base station is available, otherwise false. - */ - public boolean hasBaseStation() { - return this.base != null && (!"".equals(this.base)); - } - - /** - * @return true if city code is available, otherwise false. - */ - public boolean hasCityCode() { - return this.cityId != Long.MIN_VALUE; - } - - /** - * @return true if city name is available, otherwise false. - */ - public boolean hasCityName() { - return this.cityName != null && (!"".equals(this.cityName)); - } - - /** - * @return true if Cloud instance is available, otherwise false. - */ - public boolean hasCloudsInstance() { - return clouds != null; - } - - /** - * @return true if Coord instance is available, otherwise false. - */ - public boolean hasCoordInstance() { - return coord != null; - } - - /** - * @return true if Main instance is available, otherwise false. - */ - public boolean hasMainInstance() { - return main != null; - } - - /** - * @return true if Rain instance is available, otherwise false. - */ - public boolean hasRainInstance() { - return rain != null; - } - - /** - * @return true if Snow instance is available, otherwise false. - */ - public boolean hasSnowInstance() { - return snow != null; - } - - /** - * @return true if System instance is available, otherwise false. - */ - public boolean hasSysInstance() { - return sys != null; - } - - /** - * @return true if Wind instance is available, otherwise false. - */ - public boolean hasWindInstance() { - return wind != null; - } - - /** - * @return Base station if available, otherwise null. - */ - public String getBaseStation() { - return this.base; - } - - /** - * @return City code if available, otherwise Long.MIN_VALUE. - */ - public long getCityCode() { - return this.cityId; - } - - /** - * @return City name if available, otherwise null. - */ - public String getCityName() { - return this.cityName; - } - - /** - * @return Cloud instance if available, otherwise null. - */ - public Clouds getCloudsInstance() { - return this.clouds; - } - - /** - * @return Coord instance if available, otherwise null. - */ - public Coord getCoordInstance() { - return this.coord; - } - - /** - * @return Main instance if available, otherwise null. - */ - public Main getMainInstance() { - return this.main; - } - - /** - * @return Rain instance if available, otherwise null. - */ - public Rain getRainInstance() { - return this.rain; - } - - /** - * @return Snow instance if available, otherwise null. - */ - public Snow getSnowInstance() { - return this.snow; - } - - /** - * @return System instance if available, otherwise null. - */ - public Sys getSysInstance() { - return this.sys; - } - - /** - * @return Wind instance if available, otherwise null. - */ - public Wind getWindInstance() { - return this.wind; - } - - /** - *

- * Parses clouds data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Clouds extends AbstractWeather.Clouds { - - Clouds() { - super(); - } - - Clouds(JSONObject jsonObj) { - super(jsonObj); - } - } - - /** - *

- * Parses coordination data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Coord extends AbstractWeather.Coord { - - Coord() { - super(); - } - - Coord(JSONObject jsonObj) { - super(jsonObj); - } - } - - /** - *

- * Parses main data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Main extends AbstractWeather.Main { - - Main() { - super(); - } - - Main(JSONObject jsonObj) { - super(jsonObj); - } - } - - /** - *

- * Parses rain data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Rain implements Serializable { - - private static final String JSON_RAIN_1HOUR = "1h"; - private static final String JSON_RAIN_3HOUR = "3h"; - - private final float rain1h; - private final float rain3h; - - Rain() { - this.rain1h = Float.NaN; - this.rain3h = Float.NaN; - } - - Rain(JSONObject jsonObj) { - this.rain1h = (float) jsonObj.optDouble(JSON_RAIN_1HOUR, Double.NaN); - this.rain3h = (float) jsonObj.optDouble(JSON_RAIN_3HOUR, Double.NaN); - } - - public boolean hasRain1h() { - return !Float.isNaN(this.rain1h); - } - - public boolean hasRain3h() { - return !Float.isNaN(this.rain3h); - } - - public float getRain1h() { - return this.rain1h; - } - - public float getRain3h() { - return this.rain3h; - } - } - - /** - *

- * Parses snow data and provides methods to get/access the same 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 2015/01/28 - * @since 2.5.0.4 - */ - public static class Snow implements Serializable { - - private static final String JSON_SNOW_1HOUR = "1h"; - private static final String JSON_SNOW_3HOUR = "3h"; - - private final float snow1h; - private final float snow3h; - - Snow() { - this.snow1h = Float.NaN; - this.snow3h = Float.NaN; - } - - Snow(JSONObject jsonObj) { - this.snow1h = (float) jsonObj.optDouble(JSON_SNOW_1HOUR, Double.NaN); - this.snow3h = (float) jsonObj.optDouble(JSON_SNOW_3HOUR, Double.NaN); - } - - public boolean hasSnow1h() { - return !Float.isNaN(this.snow1h); - } - - public boolean hasSnow3h() { - return !Float.isNaN(this.snow3h); - } - - public float getSnow1h() { - return this.snow1h; - } - - public float getSnow3h() { - return this.snow3h; - } - } - - /** - *

- * Parses sys data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Sys implements Serializable { - - private static final String JSON_SYS_TYPE = "type"; - private static final String JSON_SYS_ID = "id"; - private static final String JSON_SYS_MESSAGE = "message"; - private static final String JSON_SYS_COUNTRY_CODE = "country"; - private static final String JSON_SYS_SUNRISE = "sunrise"; - private static final String JSON_SYS_SUNSET = "sunset"; - - private final int type; - private final int id; - private final double message; - private final String countryCode; - private final Date sunrise; - private final Date sunset; - - Sys() { - this.type = Integer.MIN_VALUE; - this.id = Integer.MIN_VALUE; - this.message = Double.NaN; - this.countryCode = null; - this.sunrise = null; - this.sunset = null; - } - - Sys(JSONObject jsonObj) { - this.type = jsonObj.optInt(JSON_SYS_TYPE, Integer.MIN_VALUE); - this.id = jsonObj.optInt(JSON_SYS_ID, Integer.MIN_VALUE); - this.message = jsonObj.optDouble(JSON_SYS_MESSAGE, Double.NaN); - this.countryCode = jsonObj.optString(JSON_SYS_COUNTRY_CODE, null); - - long sr_secs = jsonObj.optLong(JSON_SYS_SUNRISE, Long.MIN_VALUE); - if (sr_secs != Long.MIN_VALUE) { - this.sunrise = new Date(sr_secs * 1000); - } else { - this.sunrise = null; - } - - long ss_secs = jsonObj.optLong(JSON_SYS_SUNSET, Long.MIN_VALUE); - if (ss_secs != Long.MIN_VALUE) { - this.sunset = new Date(ss_secs * 1000); - } else { - this.sunset = null; - } - } - - public boolean hasType() { - return this.type != Integer.MIN_VALUE; - } - - public boolean hasId() { - return this.id != Integer.MIN_VALUE; - } - - public boolean hasMessage() { - return !Double.isNaN(this.message); - } - - public boolean hasCountryCode() { - return this.countryCode != null && (!"".equals(this.countryCode)); - } - - public boolean hasSunriseTime() { - return this.sunrise != null; - } - - public boolean hasSunsetTime() { - return this.sunset != null; - } - - public int getType() { - return this.type; - } - - public int getId() { - return this.id; - } - - public double getMessage() { - return this.message; - } - - public String getCountryCode() { - return this.countryCode; - } - - public Date getSunriseTime() { - return this.sunrise; - } - - public Date getSunsetTime() { - return this.sunset; - } - } - - /** - *

- * Parses windData data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Wind extends AbstractWeather.Wind { - - private static final String JSON_WIND_GUST = "gust"; - - private final float gust; - - Wind() { - super(); - - this.gust = Float.NaN; - } - - Wind(JSONObject jsonObj) { - super(jsonObj); - - this.gust = (float) jsonObj.optDouble(JSON_WIND_GUST, Double.NaN); - } - - public boolean hasWindGust() { - return !Float.isNaN(this.gust); - } - - public float getWindGust() { - return this.gust; - } - } -} diff --git a/src/main/java/net/aksingh/owmjapis/DailyForecast.java b/src/main/java/net/aksingh/owmjapis/DailyForecast.java deleted file mode 100644 index 5bb928e..0000000 --- a/src/main/java/net/aksingh/owmjapis/DailyForecast.java +++ /dev/null @@ -1,316 +0,0 @@ -/************************************************************************************************** - * Copyright (c) 2013-2017 Ashutosh Kumar Singh * - * * - * 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.owmjapis; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - *

- * Parses daily forecast data and provides methods to get/access the same 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 2014/12/27 - * @see OWM's Weather Data API - * @since 2.5.0.3 - */ -public class DailyForecast extends AbstractForecast { - /* - Instance variables - */ - private final List forecastList; - - /* - Constructors - */ - DailyForecast(JSONObject jsonObj) { - super(jsonObj); - - JSONArray dataArray = (jsonObj != null) ? jsonObj.optJSONArray(JSON_FORECAST_LIST) : new JSONArray(); - this.forecastList = (dataArray != null) ? new ArrayList(dataArray.length()) : Collections.EMPTY_LIST; - if (dataArray != null && this.forecastList != Collections.EMPTY_LIST) { - for (int i = 0; i < dataArray.length(); i++) { - JSONObject forecastObj = dataArray.optJSONObject(i); - if (forecastObj != null) { - this.forecastList.add(new Forecast(forecastObj)); - } - } - } - } - - /** - * @param index Index of Data instance in the list. - * @return Data instance if available, otherwise null. - */ - public Forecast getForecastInstance(int index) { - return this.forecastList.get(index); - } - - /** - *

- * Parses forecast data (one element in the forecastList) and provides methods to get/access the same 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 - *

- */ - public static class Forecast extends AbstractForecast.Forecast { - /* - JSON Keys - */ - public static final String JSON_TEMP = "temp"; - - private static final String JSON_FORECAST_PRESSURE = "pressure"; - private static final String JSON_FORECAST_HUMIDITY = "humidity"; - private static final String JSON_FORECAST_WIND_SPEED = "speed"; - private static final String JSON_FORECAST_WIND_DEGREE = "deg"; - private static final String JSON_FORECAST_CLOUDS = "clouds"; - private static final String JSON_FORECAST_RAIN = "rain"; - private static final String JSON_FORECAST_SNOW = "snow"; - - /* - Instance Variables - */ - private final float pressure; - private final float humidity; - private final float windSpeed; - private final float windDegree; - private final float cloudsPercent; - private final float rain; - private final float snow; - - private final Temperature temp; - - /* - Constructors - */ - Forecast() { - super(); - - this.pressure = Float.NaN; - this.humidity = Float.NaN; - this.windSpeed = Float.NaN; - this.windDegree = Float.NaN; - this.cloudsPercent = Float.NaN; - this.rain = Float.NaN; - this.snow = Float.NaN; - - this.temp = new Temperature(); - } - - Forecast(JSONObject jsonObj) { - super(jsonObj); - - JSONObject jsonObjTemp = (jsonObj != null) ? jsonObj.optJSONObject(JSON_TEMP) : null; - this.temp = (jsonObjTemp != null) ? new Temperature(jsonObjTemp) : new Temperature(); - - this.humidity = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_FORECAST_HUMIDITY, Double.NaN) : Float.NaN; - this.pressure = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_FORECAST_PRESSURE, Double.NaN) : Float.NaN; - this.windSpeed = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_FORECAST_WIND_SPEED, Double.NaN) : Float.NaN; - this.windDegree = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_FORECAST_WIND_DEGREE, Double.NaN) : Float.NaN; - this.cloudsPercent = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_FORECAST_CLOUDS, Double.NaN) : Float.NaN; - this.rain = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_FORECAST_RAIN, Double.NaN) : Float.NaN; - this.snow = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_FORECAST_SNOW, Double.NaN) : Float.NaN; - } - - public boolean hasHumidity() { - return !Float.isNaN(this.humidity); - } - - public boolean hasPressure() { - return !Float.isNaN(this.pressure); - } - - public boolean hasWindSpeed() { - return !Float.isNaN(this.windSpeed); - } - - public boolean hasWindDegree() { - return !Float.isNaN(this.windDegree); - } - - public boolean hasPercentageOfClouds() { - return !Float.isNaN(this.cloudsPercent); - } - - public boolean hasRain() { - return !Float.isNaN(this.rain); - } - - public boolean hasSnow() { - return !Float.isNaN(this.snow); - } - - 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; - } - - public float getRain() { - return this.rain; - } - - public float getSnow() { - return this.snow; - } - - public Temperature getTemperatureInstance() { - return this.temp; - } - - /** - *

- * Parses temperature data and provides methods to get/access the same 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 - *

- */ - public static class Temperature implements Serializable { - private static final String JSON_TEMP_DAY = "day"; - private static final String JSON_TEMP_MIN = "min"; - private static final String JSON_TEMP_MAX = "max"; - private static final String JSON_TEMP_NIGHT = "night"; - private static final String JSON_TEMP_EVENING = "eve"; - private static final String JSON_TEMP_MORNING = "morn"; - - private final float dayTemp; - private final float minTemp; - private final float maxTemp; - private final float nightTemp; - private final float eveTemp; - private final float mornTemp; - - 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; - } - - Temperature(JSONObject jsonObj) { - this.dayTemp = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_TEMP_DAY, Double.NaN) : Float.NaN; - this.minTemp = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_TEMP_MIN, Double.NaN) : Float.NaN; - this.maxTemp = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_TEMP_MAX, Double.NaN) : Float.NaN; - this.nightTemp = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_TEMP_NIGHT, Double.NaN) : Float.NaN; - this.eveTemp = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_TEMP_EVENING, Double.NaN) : Float.NaN; - this.mornTemp = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_TEMP_MORNING, Double.NaN) : Float.NaN; - } - - public boolean hasDayTemperature() { - return !Float.isNaN(this.dayTemp); - } - - public boolean hasMinimumTemperature() { - return !Float.isNaN(this.minTemp); - } - - public boolean hasMaximumTemperature() { - return !Float.isNaN(this.maxTemp); - } - - public boolean hasNightTemperature() { - return !Float.isNaN(this.nightTemp); - } - - public boolean hasEveningTemperature() { - return !Float.isNaN(this.eveTemp); - } - - public boolean hasMorningTemperature() { - return !Float.isNaN(this.mornTemp); - } - - 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; - } - } - } -} diff --git a/src/main/java/net/aksingh/owmjapis/HourlyForecast.java b/src/main/java/net/aksingh/owmjapis/HourlyForecast.java deleted file mode 100644 index ec37721..0000000 --- a/src/main/java/net/aksingh/owmjapis/HourlyForecast.java +++ /dev/null @@ -1,375 +0,0 @@ -/************************************************************************************************** - * Copyright (c) 2013-2017 Ashutosh Kumar Singh * - * * - * 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.owmjapis; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - *

- * Parses hourly forecast data and provides methods to get/access the same 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 2014/12/27 - * @see OWM's Weather Data API - * @since 2.5.0.3 - */ -public class HourlyForecast extends AbstractForecast { - /* - Instance variables - */ - private final List forecastList; - - /* - Constructor - */ - HourlyForecast(JSONObject jsonObj) { - super(jsonObj); - - JSONArray forecastArr = (jsonObj != null) ? jsonObj.optJSONArray(this.JSON_FORECAST_LIST) : new JSONArray(); - this.forecastList = (forecastArr != null) ? new ArrayList(forecastArr.length()) : Collections.EMPTY_LIST; - if (forecastArr != null && this.forecastList != Collections.EMPTY_LIST) { - for (int i = 0; i < forecastArr.length(); i++) { - JSONObject forecastObj = forecastArr.optJSONObject(i); - if (forecastObj != null) { - this.forecastList.add(new Forecast(forecastObj)); - } - } - } - } - - /** - * @param index Index of Data instance in the list. - * @return Data instance if available, otherwise null. - */ - public Forecast getForecastInstance(int index) { - return this.forecastList.get(index); - } - - /** - *

- * Parses forecast data (one element in the forecastList) and provides methods to get/access the same 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 - *

- */ - public static class Forecast extends AbstractForecast.Forecast { - /* - JSON Keys - */ - private static final String JSON_SYS = "sys"; - private static final String JSON_DT_TEXT = "dt_txt"; - - /* - Instance Variables - */ - private final String dateTimeText; - - private final Clouds clouds; - private final Main main; - private final Sys sys; - private final Wind wind; - - /* - Constructor - */ - Forecast(JSONObject jsonObj) { - super(jsonObj); - - this.dateTimeText = (jsonObj != null) ? jsonObj.optString(JSON_DT_TEXT, null) : null; - - JSONObject jsonObjClouds = (jsonObj != null) ? jsonObj.optJSONObject(JSON_CLOUDS) : null; - this.clouds = (jsonObjClouds != null) ? new Clouds(jsonObjClouds) : null; - - JSONObject jsonObjMain = (jsonObj != null) ? jsonObj.optJSONObject(JSON_MAIN) : null; - this.main = (jsonObjMain != null) ? new Main(jsonObjMain) : null; - - JSONObject jsonObjSys = (jsonObj != null) ? jsonObj.optJSONObject(JSON_SYS) : null; - this.sys = (jsonObjSys != null) ? new Sys(jsonObjSys) : null; - - JSONObject jsonObjWind = (jsonObj != null) ? jsonObj.optJSONObject(JSON_WIND) : null; - this.wind = (jsonObjWind != null) ? new Wind(jsonObjWind) : null; - } - - public boolean hasDateTimeText() { - return this.dateTimeText != null; - } - - /** - * @return true if Cloud instance is available, otherwise false. - */ - public boolean hasCloudsInstance() { - return clouds != null; - } - - /** - * @return true if Main instance is available, otherwise false. - */ - public boolean hasMainInstance() { - return main != null; - } - - /** - * @return true if System instance is available, otherwise false. - */ - public boolean hasSysInstance() { - return sys != null; - } - - /** - * @return true if Wind instance is available, otherwise false. - */ - public boolean hasWindInstance() { - return wind != null; - } - - public String getDateTimeText() { - return this.dateTimeText; - } - - /** - * @return Cloud instance if available, otherwise null. - */ - public Clouds getCloudsInstance() { - return this.clouds; - } - - /** - * @return Main instance if available, otherwise null. - */ - public Main getMainInstance() { - return this.main; - } - - /** - * @return System instance if available, otherwise null. - */ - public Sys getSysInstance() { - return this.sys; - } - - /** - * @return Wind instance if available, otherwise null. - */ - public Wind getWindInstance() { - return this.wind; - } - - /** - *

- * Parses clouds data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Clouds extends AbstractForecast.Forecast.Clouds { - - Clouds() { - super(); - } - - Clouds(JSONObject jsonObj) { - super(jsonObj); - } - } - - /** - *

- * Parses main data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Main extends AbstractForecast.Forecast.Main { - private static final String JSON_MAIN_SEA_LEVEL = "sea_level"; - private static final String JSON_MAIN_GRND_LEVEL = "grnd_level"; - private static final String JSON_MAIN_TMP_KF = "temp_kf"; - - private final float seaLevel; - private final float groundLevel; - private final float tempKF; - - Main() { - super(); - - this.seaLevel = Float.NaN; - this.groundLevel = Float.NaN; - this.tempKF = Float.NaN; - } - - Main(JSONObject jsonObj) { - super(jsonObj); - - this.seaLevel = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_MAIN_SEA_LEVEL, Float.NaN) : Float.NaN; - this.groundLevel = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_MAIN_GRND_LEVEL, Float.NaN) : Float.NaN; - this.tempKF = (jsonObj != null) ? (float) jsonObj.optDouble(JSON_MAIN_TMP_KF, Float.NaN) : Float.NaN; - } - - public boolean hasSeaLevel() { - return !Float.isNaN(this.seaLevel); - } - - public boolean hasGroundLevel() { - return !Float.isNaN(this.groundLevel); - } - - public boolean hasTempKF() { - return !Float.isNaN(this.tempKF); - } - - public float getSeaLevel() { - return this.seaLevel; - } - - public float getGroundLevel() { - return this.groundLevel; - } - - public float getTempKF() { - return this.tempKF; - } - } - - /** - *

- * Parses sys data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Sys implements Serializable { - private static final String JSON_SYS_POD = "pod"; - - private final String pod; - - Sys() { - this.pod = null; - } - - Sys(JSONObject jsonObj) { - this.pod = (jsonObj != null) ? jsonObj.optString(JSON_SYS_POD, null) : null; - } - - public boolean hasPod() { - return this.pod != null && (!"".equals(this.pod)); - } - - public String getPod() { - return this.pod; - } - } - - /** - *

- * Parses windData data and provides methods to get/access the same 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 2014/12/26 - * @since 2.5.0.1 - */ - public static class Wind extends AbstractWeather.Wind { - - Wind() { - super(); - } - - Wind(JSONObject jsonObj) { - super(jsonObj); - } - } - } -} diff --git a/src/main/java/net/aksingh/owmjapis/OpenWeatherMap.java b/src/main/java/net/aksingh/owmjapis/OpenWeatherMap.java deleted file mode 100644 index 8bee740..0000000 --- a/src/main/java/net/aksingh/owmjapis/OpenWeatherMap.java +++ /dev/null @@ -1,778 +0,0 @@ -/************************************************************************************************** - * Copyright (c) 2013-2017 Ashutosh Kumar Singh * - * * - * 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.owmjapis; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.*; -import java.util.zip.GZIPInputStream; -import java.util.zip.Inflater; -import java.util.zip.InflaterInputStream; - -/** - *

- * The starting point for all API operations. - * If you're new to this API, read the docs for this class first. - *

- *

- * Lets you access data from OpenWeatherMap.org using its Weather APIs. - * Henceforth, it's shortened as OpenWeatherMap.org to ease commenting. - *

- *

- * Sample code:
- * OpenWeatherMap owm = new OpenWeatherMap("your-api-key");
- * OpenWeatherMap owm = new OpenWeatherMap(your-units, "your-api-key");
- * OpenWeatherMap owm = new OpenWeatherMap(your-units, your-language, "your-api-key"); - *

- * - * @author Ashutosh Kumar Singh - * @version 2015-01-17 - * @see OpenWeatherMap.org - * @see OpenWeatherMap.org API - * @since 2.5.0.1 - */ -public class OpenWeatherMap { - /* - URLs and parameters for OpenWeatherMap.org - */ - private static final String URL_API = "https://api.openweathermap.org/data/2.5/"; - private static final String URL_CURRENT = "weather?"; - private static final String URL_HOURLY_FORECAST = "forecast?"; - private static final String URL_DAILY_FORECAST = "forecast/daily?"; - - private static final String PARAM_COUNT = "cnt="; - private static final String PARAM_CITY_NAME = "q="; - private static final String PARAM_CITY_ID = "id="; - private static final String PARAM_LATITUDE = "lat="; - private static final String PARAM_LONGITUDE = "lon="; - private static final String PARAM_MODE = "mode="; - private static final String PARAM_UNITS = "units="; - private static final String PARAM_APPID = "appId="; - private static final String PARAM_LANG = "lang="; - - /* - Instance Variables - */ - private final OWMAddress owmAddress; - private final OWMResponse owmResponse; - private final OWMProxy owmProxy; - - /** - * Constructor - * - * @param apiKey API key from OpenWeatherMap.org - * @see OpenWeatherMap.org API Key - */ - public OpenWeatherMap(String apiKey) { - this(Units.IMPERIAL, Language.ENGLISH, apiKey); - } - - /** - * Constructor - * - * @param units Any constant from Units - * @param apiKey API key from OpenWeatherMap.org - * @see net.aksingh.owmjapis.OpenWeatherMap.Units - * @see OpenWeatherMap.org API Key - */ - public OpenWeatherMap(Units units, String apiKey) { - this(units, Language.ENGLISH, apiKey); - } - - /** - * Constructor - * - * @param units Any constant from Units - * @param lang Any constant from Language - * @param apiKey API key from OpenWeatherMap.org - * @see net.aksingh.owmjapis.OpenWeatherMap.Units - * @see net.aksingh.owmjapis.OpenWeatherMap.Language - * @see [OpenWeatherMap.org's Multilingual support][https://openweathermap.org/current#multi] - * @see OpenWeatherMap.org's API Key - */ - public OpenWeatherMap(Units units, Language lang, String apiKey) { - this.owmAddress = new OWMAddress(units, lang, apiKey); - this.owmProxy = new OWMProxy(null, Integer.MIN_VALUE, null, null); - this.owmResponse = new OWMResponse(owmAddress, owmProxy); - } - - /* - Getters - */ - public OWMAddress getOwmAddressInstance() { - return owmAddress; - } - - public String getApiKey() { - return owmAddress.getAppId(); - } - - public Units getUnits() { - return owmAddress.getUnits(); - } - - public String getMode() { - return owmAddress.getMode(); - } - - public Language getLang() { - return owmAddress.getLang(); - } - - /* - Setters - */ - - /** - * Set units for getting data from OpenWeatherMap.org - * - * @param units Any constant from Units - * @see net.aksingh.owmjapis.OpenWeatherMap.Units - */ - public void setUnits(Units units) { - owmAddress.setUnits(units); - } - - /** - * Set API key for getting data from OpenWeatherMap.org - * - * @param appId API key from OpenWeatherMap.org - * @see OpenWeatherMap.org's API Key - */ - public void setApiKey(String appId) { - owmAddress.setAppId(appId); - } - - /** - * Set language for getting data from OpenWeatherMap.org - * - * @param lang Any constant from Language - * @see net.aksingh.owmjapis.OpenWeatherMap.Language - * @see [OpenWeatherMap.org's Multilingual support][https://openweathermap.org/current#multi] - */ - public void setLang(Language lang) { - owmAddress.setLang(lang); - } - - /** - * Set proxy for getting data from OpenWeatherMap.org - * - * @param ip IP address of the proxy - * @param port Port address of the proxy - */ - public void setProxy(String ip, int port) { - owmProxy.setIp(ip); - owmProxy.setPort(port); - owmProxy.setUser(null); - owmProxy.setPass(null); - } - - /** - * Set proxy and authentication details for getting data from OpenWeatherMap.org - * - * @param ip IP address of the proxy - * @param port Port address of the proxy - * @param user User name for the proxy if required - * @param pass Password for the proxy if required - */ - public void setProxy(String ip, int port, String user, String pass) { - owmProxy.setIp(ip); - owmProxy.setPort(port); - owmProxy.setUser(user); - owmProxy.setPass(pass); - } - - public CurrentWeather currentWeatherByCityName(String cityName) - throws IOException, JSONException { - String response = owmResponse.currentWeatherByCityName(cityName); - return this.currentWeatherFromRawResponse(response); - } - - public CurrentWeather currentWeatherByCityName(String cityName, String countryCode) - throws IOException, JSONException { - String response = owmResponse.currentWeatherByCityName(cityName, countryCode); - return this.currentWeatherFromRawResponse(response); - } - - public CurrentWeather currentWeatherByCityCode(long cityCode) - throws JSONException { - String response = owmResponse.currentWeatherByCityCode(cityCode); - return this.currentWeatherFromRawResponse(response); - } - - public CurrentWeather currentWeatherByCoordinates(float latitude, float longitude) - throws JSONException { - String response = owmResponse.currentWeatherByCoordinates(latitude, longitude); - return this.currentWeatherFromRawResponse(response); - } - - public CurrentWeather currentWeatherFromRawResponse(String response) - throws JSONException { - JSONObject jsonObj = (response != null) ? new JSONObject(response) : null; - return new CurrentWeather(jsonObj); - } - - public HourlyForecast hourlyForecastByCityName(String cityName) - throws IOException, JSONException { - String response = owmResponse.hourlyForecastByCityName(cityName); - return this.hourlyForecastFromRawResponse(response); - } - - public HourlyForecast hourlyForecastByCityName(String cityName, String countryCode) - throws IOException, JSONException { - String response = owmResponse.hourlyForecastByCityName(cityName, countryCode); - return this.hourlyForecastFromRawResponse(response); - } - - public HourlyForecast hourlyForecastByCityCode(long cityCode) - throws JSONException { - String response = owmResponse.hourlyForecastByCityCode(cityCode); - return this.hourlyForecastFromRawResponse(response); - } - - public HourlyForecast hourlyForecastByCoordinates(float latitude, float longitude) - throws JSONException { - String response = owmResponse.hourlyForecastByCoordinates(latitude, longitude); - return this.hourlyForecastFromRawResponse(response); - } - - public HourlyForecast hourlyForecastFromRawResponse(String response) - throws JSONException { - JSONObject jsonObj = (response != null) ? new JSONObject(response) : null; - return new HourlyForecast(jsonObj); - } - - public DailyForecast dailyForecastByCityName(String cityName, byte count) - throws IOException, JSONException { - String response = owmResponse.dailyForecastByCityName(cityName, count); - return this.dailyForecastFromRawResponse(response); - } - - public DailyForecast dailyForecastByCityName(String cityName, String countryCode, byte count) - throws IOException, JSONException { - String response = owmResponse.dailyForecastByCityName(cityName, countryCode, count); - return this.dailyForecastFromRawResponse(response); - } - - public DailyForecast dailyForecastByCityCode(long cityCode, byte count) - throws JSONException { - String response = owmResponse.dailyForecastByCityCode(cityCode, count); - return this.dailyForecastFromRawResponse(response); - } - - public DailyForecast dailyForecastByCoordinates(float latitude, float longitude, byte count) - throws JSONException { - String response = owmResponse.dailyForecastByCoordinates(latitude, longitude, count); - return this.dailyForecastFromRawResponse(response); - } - - public DailyForecast dailyForecastFromRawResponse(String response) - throws JSONException { - JSONObject jsonObj = (response != null) ? new JSONObject(response) : null; - return new DailyForecast(jsonObj); - } - - /** - * Units that can be set for getting data from OpenWeatherMap.org - * - * @since 2.5.0.3 - */ - public static enum Units { - METRIC("metric"), - IMPERIAL("imperial"); - - private final String unit; - - Units(String unit) { - this.unit = unit; - } - } - - /** - * Languages that can be set for getting data from OpenWeatherMap.org - * - * @since 2.5.0.3 - */ - public static enum Language { - ENGLISH("en"), - RUSSIAN("ru"), - ITALIAN("it"), - SPANISH("es"), - UKRAINIAN("uk"), - GERMAN("de"), - PORTUGUESE("pt"), - ROMANIAN("ro"), - POLISH("pl"), - FINNISH("fi"), - DUTCH("nl"), - FRENCH("FR"), - BULGARIAN("bg"), - SWEDISH("sv"), - CHINESE_TRADITIONAL("zh_tw"), - CHINESE_SIMPLIFIED("zh"), - TURKISH("tr"), - CROATIAN("hr"), - CATALAN("ca"); - - private final String lang; - - Language(String lang) { - this.lang = lang; - } - } - - /** - * Proxifies the default HTTP requests - * - * @since 2.5.0.5 - */ - private static class OWMProxy { - private String ip; - private int port; - private String user; - private String pass; - - private OWMProxy(String ip, int port, String user, String pass) { - this.ip = ip; - this.port = port; - this.user = user; - this.pass = pass; - } - - public Proxy getProxy() { - Proxy proxy = null; - - if (ip != null && (!"".equals(ip)) && port != Integer.MIN_VALUE) { - proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port)); - } - - if (user != null && (!"".equals(user)) && pass != null && (!"".equals(pass))) { - Authenticator.setDefault(getAuthenticatorInstance(user, pass)); - } - - return proxy; - } - - private Authenticator getAuthenticatorInstance(final String user, final String pass) { - Authenticator authenticator = new Authenticator() { - - public PasswordAuthentication getPasswordAuthentication() { - return (new PasswordAuthentication(user, pass.toCharArray())); - } - }; - - return authenticator; - } - - public void setIp(String ip) { - this.ip = ip; - } - - public void setPort(int port) { - this.port = port; - } - - public void setUser(String user) { - this.user = user; - } - - public void setPass(String pass) { - this.pass = pass; - } - } - - /** - * Generates addresses for accessing the information from OpenWeatherMap.org - * - * @since 2.5.0.3 - */ - public static class OWMAddress { - private static final String MODE = "json"; - private static final String ENCODING = "UTF-8"; - - private String mode; - private Units units; - private String appId; - private Language lang; - - /* - Constructors - */ - private OWMAddress(String appId) { - this(Units.IMPERIAL, Language.ENGLISH, appId); - } - - private OWMAddress(Units units, String appId) { - this(units, Language.ENGLISH, appId); - } - - private OWMAddress(Units units, Language lang, String appId) { - this.mode = MODE; - this.units = units; - this.lang = lang; - this.appId = appId; - } - - /* - Getters - */ - private String getAppId() { - return this.appId; - } - - private Units getUnits() { - return this.units; - } - - private String getMode() { - return this.mode; - } - - private Language getLang() { - return this.lang; - } - - /* - Setters - */ - private void setUnits(Units units) { - this.units = units; - } - - private void setAppId(String appId) { - this.appId = appId; - } - - private void setLang(Language lang) { - this.lang = lang; - } - - /* - Addresses for current weather - */ - public String currentWeatherByCityName(String cityName) throws UnsupportedEncodingException { - return new StringBuilder() - .append(URL_API).append(URL_CURRENT) - .append(PARAM_CITY_NAME).append(URLEncoder.encode(cityName, ENCODING)).append("&") - .append(PARAM_MODE).append(this.mode).append("&") - .append(PARAM_UNITS).append(this.units).append("&") - .append(PARAM_LANG).append(this.lang).append("&") - .append(PARAM_APPID).append(this.appId) - .toString(); - } - - public String currentWeatherByCityName(String cityName, String countryCode) throws UnsupportedEncodingException { - return currentWeatherByCityName(new StringBuilder() - .append(cityName).append(",").append(countryCode) - .toString()); - } - - public String currentWeatherByCityCode(long cityCode) { - return new StringBuilder() - .append(URL_API).append(URL_CURRENT) - .append(PARAM_CITY_ID).append(Long.toString(cityCode)).append("&") - .append(PARAM_MODE).append(this.mode).append("&") - .append(PARAM_UNITS).append(this.units).append("&") - .append(PARAM_LANG).append(this.lang).append("&") - .append(PARAM_APPID).append(this.appId) - .toString(); - } - - public String currentWeatherByCoordinates(float latitude, float longitude) { - return new StringBuilder() - .append(URL_API).append(URL_CURRENT) - .append(PARAM_LATITUDE).append(Float.toString(latitude)).append("&") - .append(PARAM_LONGITUDE).append(Float.toString(longitude)).append("&") - .append(PARAM_MODE).append(this.mode).append("&") - .append(PARAM_UNITS).append(this.units).append("&") - .append(PARAM_APPID).append(this.appId) - .toString(); - } - - /* - Addresses for hourly forecasts - */ - public String hourlyForecastByCityName(String cityName) throws UnsupportedEncodingException { - return new StringBuilder() - .append(URL_API).append(URL_HOURLY_FORECAST) - .append(PARAM_CITY_NAME).append(URLEncoder.encode(cityName, ENCODING)).append("&") - .append(PARAM_MODE).append(this.mode).append("&") - .append(PARAM_UNITS).append(this.units).append("&") - .append(PARAM_LANG).append(this.lang).append("&") - .append(PARAM_APPID).append(this.appId) - .toString(); - } - - public String hourlyForecastByCityName(String cityName, String countryCode) throws UnsupportedEncodingException { - return hourlyForecastByCityName(new StringBuilder() - .append(cityName).append(",").append(countryCode) - .toString()); - } - - public String hourlyForecastByCityCode(long cityCode) { - return new StringBuilder() - .append(URL_API).append(URL_HOURLY_FORECAST) - .append(PARAM_CITY_ID).append(Long.toString(cityCode)).append("&") - .append(PARAM_MODE).append(this.mode).append("&") - .append(PARAM_UNITS).append(this.units).append("&") - .append(PARAM_LANG).append(this.lang).append("&") - .append(PARAM_APPID).append(this.appId) - .toString(); - } - - public String hourlyForecastByCoordinates(float latitude, float longitude) { - return new StringBuilder() - .append(URL_API).append(URL_HOURLY_FORECAST) - .append(PARAM_LATITUDE).append(Float.toString(latitude)).append("&") - .append(PARAM_LONGITUDE).append(Float.toString(longitude)).append("&") - .append(PARAM_MODE).append(this.mode).append("&") - .append(PARAM_UNITS).append(this.units).append("&") - .append(PARAM_LANG).append(this.lang).append("&") - .append(PARAM_APPID).append(this.appId) - .toString(); - } - - /* - Addresses for daily forecasts - */ - public String dailyForecastByCityName(String cityName, byte count) throws UnsupportedEncodingException { - return new StringBuilder() - .append(URL_API).append(URL_DAILY_FORECAST) - .append(PARAM_CITY_NAME).append(URLEncoder.encode(cityName, ENCODING)).append("&") - .append(PARAM_COUNT).append(Byte.toString(count)).append("&") - .append(PARAM_MODE).append(this.mode).append("&") - .append(PARAM_UNITS).append(this.units).append("&") - .append(PARAM_LANG).append(this.lang).append("&") - .append(PARAM_APPID).append(this.appId) - .toString(); - } - - public String dailyForecastByCityName(String cityName, String countryCode, byte count) throws UnsupportedEncodingException { - return dailyForecastByCityName(new StringBuilder() - .append(cityName).append(",").append(countryCode) - .toString(), count); - } - - public String dailyForecastByCityCode(long cityCode, byte count) { - return new StringBuilder() - .append(URL_API).append(URL_DAILY_FORECAST) - .append(PARAM_CITY_ID).append(Long.toString(cityCode)).append("&") - .append(PARAM_COUNT).append(Byte.toString(count)).append("&") - .append(PARAM_MODE).append(this.mode).append("&") - .append(PARAM_UNITS).append(this.units).append("&") - .append(PARAM_LANG).append(this.lang).append("&") - .append(PARAM_APPID).append(this.appId) - .toString(); - } - - public String dailyForecastByCoordinates(float latitude, float longitude, byte count) { - return new StringBuilder() - .append(URL_API).append(URL_DAILY_FORECAST) - .append(PARAM_LATITUDE).append(Float.toString(latitude)).append("&") - .append(PARAM_LONGITUDE).append(Float.toString(longitude)).append("&") - .append(PARAM_COUNT).append(Byte.toString(count)).append("&") - .append(PARAM_MODE).append(this.mode).append("&") - .append(PARAM_UNITS).append(this.units).append("&") - .append(PARAM_LANG).append(this.lang).append("&") - .append(PARAM_APPID).append(this.appId) - .toString(); - } - } - - /** - * Requests OpenWeatherMap.org for data and provides back the incoming response. - * - * @since 2.5.0.3 - */ - private static class OWMResponse { - private final OWMAddress owmAddress; - private final OWMProxy owmProxy; - - public OWMResponse(OWMAddress owmAddress, OWMProxy owmProxy) { - this.owmAddress = owmAddress; - this.owmProxy = owmProxy; - } - - /* - Responses for current weather - */ - public String currentWeatherByCityName(String cityName) throws UnsupportedEncodingException { - String address = owmAddress.currentWeatherByCityName(cityName); - return httpGET(address); - } - - public String currentWeatherByCityName(String cityName, String countryCode) throws UnsupportedEncodingException { - String address = owmAddress.currentWeatherByCityName(cityName, countryCode); - return httpGET(address); - } - - public String currentWeatherByCityCode(long cityCode) { - String address = owmAddress.currentWeatherByCityCode(cityCode); - return httpGET(address); - } - - public String currentWeatherByCoordinates(float latitude, float longitude) { - String address = owmAddress.currentWeatherByCoordinates(latitude, longitude); - return httpGET(address); - } - - /* - Responses for hourly forecasts - */ - public String hourlyForecastByCityName(String cityName) throws UnsupportedEncodingException { - String address = owmAddress.hourlyForecastByCityName(cityName); - return httpGET(address); - } - - public String hourlyForecastByCityName(String cityName, String countryCode) throws UnsupportedEncodingException { - String address = owmAddress.hourlyForecastByCityName(cityName, countryCode); - return httpGET(address); - } - - public String hourlyForecastByCityCode(long cityCode) { - String address = owmAddress.hourlyForecastByCityCode(cityCode); - return httpGET(address); - } - - public String hourlyForecastByCoordinates(float latitude, float longitude) { - String address = owmAddress.hourlyForecastByCoordinates(latitude, longitude); - return httpGET(address); - } - - /* - Responses for daily forecasts - */ - public String dailyForecastByCityName(String cityName, byte count) throws UnsupportedEncodingException { - String address = owmAddress.dailyForecastByCityName(cityName, count); - return httpGET(address); - } - - public String dailyForecastByCityName(String cityName, String countryCode, byte count) throws UnsupportedEncodingException { - String address = owmAddress.dailyForecastByCityName(cityName, countryCode, count); - return httpGET(address); - } - - public String dailyForecastByCityCode(long cityCode, byte count) { - String address = owmAddress.dailyForecastByCityCode(cityCode, count); - return httpGET(address); - } - - public String dailyForecastByCoordinates(float latitude, float longitude, byte count) { - String address = owmAddress.dailyForecastByCoordinates(latitude, longitude, count); - return httpGET(address); - } - - /** - * Implements HTTP's GET method - * - * @param requestAddress Address to be loaded - * @return Response if successful, else null - * @see HTTP - (9.3) GET - */ - private String httpGET(String requestAddress) { - URL request; - HttpURLConnection connection = null; - BufferedReader reader = null; - - String tmpStr; - String response = null; - - try { - request = new URL(requestAddress); - - if (owmProxy.getProxy() != null) { - connection = (HttpURLConnection) request.openConnection(owmProxy.getProxy()); - } else { - connection = (HttpURLConnection) request.openConnection(); - } - - connection.setRequestMethod("GET"); - connection.setUseCaches(false); - connection.setDoInput(true); - connection.setDoOutput(false); - connection.setRequestProperty("Accept-Encoding", "gzip, deflate"); - connection.connect(); - - if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { - String encoding = connection.getContentEncoding(); - - try { - if (encoding != null && "gzip".equalsIgnoreCase(encoding)) { - reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(connection.getInputStream()))); - } else if (encoding != null && "deflate".equalsIgnoreCase(encoding)) { - reader = new BufferedReader(new InputStreamReader(new InflaterInputStream(connection.getInputStream(), new Inflater(true)))); - } else { - reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - } - - while ((tmpStr = reader.readLine()) != null) { - response = tmpStr; - } - } catch (IOException e) { - System.err.println("Error: " + e.getMessage()); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - System.err.println("Error: " + e.getMessage()); - } - } - } - } else { // if HttpURLConnection is not okay - try { - reader = new BufferedReader(new InputStreamReader(connection.getErrorStream())); - while ((tmpStr = reader.readLine()) != null) { - response = tmpStr; - } - } catch (IOException e) { - System.err.println("Error: " + e.getMessage()); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - System.err.println("Error: " + e.getMessage()); - } - } - } - - // if response is bad - System.err.println("Bad Response: " + response + "\n"); - return null; - } - } catch (IOException e) { - System.err.println("Error: " + e.getMessage()); - response = null; - } finally { - if (connection != null) { - connection.disconnect(); - } - } - - return response; - } - } -} diff --git a/src/main/java/net/aksingh/owmjapis/Tools.java b/src/main/java/net/aksingh/owmjapis/Tools.java deleted file mode 100644 index 90c0f9d..0000000 --- a/src/main/java/net/aksingh/owmjapis/Tools.java +++ /dev/null @@ -1,91 +0,0 @@ -/************************************************************************************************** - * Copyright (c) 2013-2017 Ashutosh Kumar Singh * - * * - * 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.owmjapis; - -/** - *

- * Provides methods for conversions, etc. - *

- * - * @author Ashutosh Kumar Singh - * @version 2014-12-27 - * @since 2.5.0.1 - */ -public class Tools { - /** - *

- * Converts degree to direction. - *

- * - * @param degree Degree of wind as received from OpenWeatherMap.org - * @return Direction - * @throws IllegalArgumentException Degree should be between 0 and 360. - * @deprecated As of version 2.5.1 and will be removed in version 2.5.2, - * use {@link net.aksingh.owmjapis.util.ConversionTools.convertDegree2DirectionCode} instead. - */ - @Deprecated - 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; - } -}