From 36432651fc9982387cfaa4fb649ea9e12bac0c63 Mon Sep 17 00:00:00 2001 From: Prominence Date: Tue, 17 Jul 2018 22:57:11 +0300 Subject: [PATCH] Response classes were renamed. Small adjustments for class/method names. Documentation was updated. --- README.md | 31 ++++++++--------- .../openweathermap/api/BasicRequester.java | 15 --------- .../openweathermap/api/ForecastRequester.java | 33 +++---------------- .../openweathermap/api/WeatherRequester.java | 16 ++++----- .../model/{Weather.java => WeatherState.java} | 12 +++---- ...ecastResponse.java => HourlyForecast.java} | 28 +++++++++------- .../{WeatherResponse.java => Weather.java} | 23 ++++++------- 7 files changed, 63 insertions(+), 95 deletions(-) rename src/main/java/by/prominence/openweathermap/api/model/{Weather.java => WeatherState.java} (89%) rename src/main/java/by/prominence/openweathermap/api/model/response/{ForecastResponse.java => HourlyForecast.java} (96%) rename src/main/java/by/prominence/openweathermap/api/model/response/{WeatherResponse.java => Weather.java} (96%) diff --git a/README.md b/README.md index 5a9a67c..a01963c 100644 --- a/README.md +++ b/README.md @@ -49,25 +49,25 @@ Available requests: * `getByCoordinates(Coordinates coordinates)` * `getByZIPCode(String zipCode, String countryCode)` -`WeatherResponse`'s useful public methods(setters are not listed): +`Weather`'s useful public methods(setters are not listed): | Method | Description | |---------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------| | `getCityId()` | Returns city ID. Example: `625144` for Minsk. | | `getCityName()` | Returns city name. Example: `Minsk`. | | `getCoordinates()` | Returns `Coordinates` instance that contains *latitude* and *longitude* information. | -| `getWeather()` | Returns list of `Weather` instances with the only `getDescription` useful method. | +| `getWeatherStates()` | Returns list of `WeatherState` instances with the only `getDescription` useful method. | | `getBase()` | Returns `String` with some internal information. Example: `cmc stations` - from official documentation. | -| `getWeatherInfo()` | Returns `WeatherResponse.WeatherInfo` instance that contains information about temperature, pressure and humidity. | +| `getWeatherInfo()` | Returns `Weather.WeatherInfo` instance that contains information about temperature, pressure and humidity. | | `getWind()` | Returns `Wind` instance that contains information about speed and degree. | | `getClouds()` | Returns `Clouds` instance that contains *cloudiness* percentage information. | | `getRain()` | Returns `Rain` instance that contains information about rain volume for the last 3 hours. | | `getSnow()` | Returns `Snow` instance that contains information about snow volume for the last 3 hours. | | `getDataCalculationTime()`| Returns `long` value that represents data calculation timestamp. | -| `getWeatherSystemInfo()` | Returns `WeatherResponse.WeatherSystemInfo` instance that contains internal information. There is also an information about country, sunrise and sunset times. | +| `getWeatherSystemInfo()` | Returns `Weather.WeatherSystemInfo` instance that contains internal information. There is also an information about country, sunrise and sunset times. | | `getResponseCode()` | Returns OpenWeatherMap response code. Internal information. | | `getCountry()` | An alias for `getWeatherSystemInfo().getCountry()`. | -| `getWeatherDescription()` | An alias for `getWeather.get(0).getDescription()`. | +| `getWeatherDescription()` | An alias for `getWeatherStates.get(0).getDescription()`. | | `getDataCalculationDate()`| Returns data calculation time in `Date` representation. | | `getTemperature()` | An alias for `getWeatherInfo().getTemperature()`. | | `getTemperatureUnit()` | An alias for `getWeatherInfo().getTemperatureUnit()`. | @@ -114,27 +114,27 @@ Available requests: * `getByCoordinates(Coordinates coordinates)` * `getByZIPCode(String zipCode, String countryCode)` -`ForecastResponse`'s useful public methods(setters are not listed): +`HourlyForecast`'s useful public methods(setters are not listed): | Method | Description | |-----------------------------|--------------------------------------------------------------------------------------------| | `getCityId()` | Returns city ID. Example: `625144` for Minsk. | | `getCityName()` | Returns city name. Example: `Minsk`. | | `getCoordinates()` | Returns `Coordinates` instance that contains *latitude* and *longitude* information. | -| `getCityInfo()` | Returns `ForecastResponse.CityInfo` instance that contains information about city. | +| `getCityInfo()` | Returns `HourlyForecast.CityInfo` instance that contains information about city. | | `getResponseCode()` | Returns OpenWeatherMap response code. Internal information. | | `getCountry()` | An alias for `getCityInfo().getCountry()`. | -| `getForecasts()` | Returns `List` collection with all forecast information. | +| `getForecasts()` | Returns `List` collection with all forecast information. | | `getAverageTemperature()` | Returns average temperature from forecasts. | | `getMinimumTemperature()` | Returns minimum temperature from forecasts. | | `getMaximumTemperature()` | Returns maximum temperature from forecasts. | -| `getByMinimumTemperature()` | Returns `ForecastResponse.ForecastInfo` for the time where temperature is minimal. | -| `getByMaximumTemperature()` | Returns `ForecastResponse.ForecastInfo` for the time where temperature is maximal. | +| `getByMinimumTemperature()` | Returns `HourlyForecast.ForecastInfo` for the time where temperature is minimal. | +| `getByMaximumTemperature()` | Returns `HourlyForecast.ForecastInfo` for the time where temperature is maximal. | | `getAveragePressure()` | Returns average pressure from forecasts. | | `getMinimumPressure()` | Returns minimum pressure from forecasts. | | `getMaximumPressure()` | Returns maximum pressure from forecasts. | -| `getByMinimumPressure()` | Returns `ForecastResponse.ForecastInfo` for the time where pressure is minimal. | -| `getByMaximumPressure()` | Returns `ForecastResponse.ForecastInfo` for the time where pressure is maximal. | +| `getByMinimumPressure()` | Returns `HourlyForecast.ForecastInfo` for the time where pressure is minimal. | +| `getByMaximumPressure()` | Returns `HourlyForecast.ForecastInfo` for the time where pressure is maximal. | | `toString()` | Returns pretty string for the whole available forecast information. | `toString()` output example: @@ -189,13 +189,14 @@ Forecasts: | Method | Description | |-----------------------------|------------------------------------------------------------------------------------------------------------------| | `getDataCalculationTime()` | Returns `long` value that represents data calculation timestamp. | -| `getMainInfo()` | Returns `ForecastResponse.MainInfo` instance that contains information about temperature, pressure and humidity. | -| `getWeathers()` | Returns list of `Weather` instances with the only `getDescription` useful method. | +| `getDataCalculationDate()` | Returns data calculation time in `Date` representation. | +| `getMainInfo()` | Returns `HourlyForecast.MainInfo` instance that contains information about temperature, pressure and humidity. | +| `getWeatherStates()` | Returns list of `WeatherState` instances with the only `getDescription` useful method. | | `getClouds()` | Returns `Clouds` instance that contains *cloudiness* percentage information. | | `getWind()` | Returns `Wind` instance that contains information about speed and degree. | | `getSnow()` | Returns `Snow` instance that contains information about snow volume for the last 3 hours. | | `getRain()` | Returns `Rain` instance that contains information about rain volume for the last 3 hours. | -| `getSystemInfo()` | Returns `ForecastResponse.ForecastSystemInfo` instance with internal information. | +| `getSystemInfo()` | Returns `HourlyForecast.ForecastSystemInfo` instance with internal information. | | `getDt_txt()` | Returns `String` value that represents data calculation time. | | `toString()` | Returns pretty string for the whole available weather information. | diff --git a/src/main/java/by/prominence/openweathermap/api/BasicRequester.java b/src/main/java/by/prominence/openweathermap/api/BasicRequester.java index 9c670f4..0fca97c 100644 --- a/src/main/java/by/prominence/openweathermap/api/BasicRequester.java +++ b/src/main/java/by/prominence/openweathermap/api/BasicRequester.java @@ -30,7 +30,6 @@ import by.prominence.openweathermap.api.model.Coordinates; import java.net.MalformedURLException; import java.net.URL; -import java.util.Map; abstract class BasicRequester extends AuthenticationTokenBasedRequester { @@ -87,23 +86,9 @@ abstract class BasicRequester extends AuthenticationTokenBasedRequester { urlBuilder.append(accuracy); } - Map additionalParameters = getAdditionalParameters(); - if (additionalParameters != null) { - additionalParameters.forEach((key, value) -> { - urlBuilder.append("&"); - urlBuilder.append(key); - urlBuilder.append("="); - urlBuilder.append(value); - }); - } - return new URL(urlBuilder.toString()); } - protected Map getAdditionalParameters() { - return null; - } - protected abstract String getRequestType(); protected abstract T executeRequest(String requestSpecificParamsString) throws InvalidAuthTokenException, DataNotFoundException; diff --git a/src/main/java/by/prominence/openweathermap/api/ForecastRequester.java b/src/main/java/by/prominence/openweathermap/api/ForecastRequester.java index 494ce9f..a89727d 100644 --- a/src/main/java/by/prominence/openweathermap/api/ForecastRequester.java +++ b/src/main/java/by/prominence/openweathermap/api/ForecastRequester.java @@ -25,28 +25,19 @@ package by.prominence.openweathermap.api; import by.prominence.openweathermap.api.constants.Unit; import by.prominence.openweathermap.api.exception.DataNotFoundException; import by.prominence.openweathermap.api.exception.InvalidAuthTokenException; -import by.prominence.openweathermap.api.model.response.ForecastResponse; +import by.prominence.openweathermap.api.model.response.HourlyForecast; import by.prominence.openweathermap.api.utils.JsonUtils; import by.prominence.openweathermap.api.utils.RequestUtils; import java.io.IOException; import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; -public class ForecastRequester extends BasicRequester { - - private int amountOfDays = -1; +public class ForecastRequester extends BasicRequester { ForecastRequester(String authToken) { super(authToken); } - public ForecastRequester setAmountOfDays(int amountOfDays) { - this.amountOfDays = amountOfDays; - return this; - } - public ForecastRequester setLanguage(String language) { this.language = language; return this; @@ -62,31 +53,17 @@ public class ForecastRequester extends BasicRequester { return this; } - @Override - protected Map getAdditionalParameters() { - Map additionalParameters = null; - if (amountOfDays != -1) { - additionalParameters = new HashMap<>(); - additionalParameters.put("cnt", String.valueOf(amountOfDays)); - } - - return additionalParameters; - } - protected String getRequestType() { - if (amountOfDays != -1) { - return "forecast/daily"; - } return "forecast"; } - protected ForecastResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { + protected HourlyForecast executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { - ForecastResponse forecastResponse = null; + HourlyForecast forecastResponse = null; try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters)); - forecastResponse = (ForecastResponse)JsonUtils.parseJson(requestResult, ForecastResponse.class); + forecastResponse = (HourlyForecast)JsonUtils.parseJson(requestResult, HourlyForecast.class); char temperatureUnit = Unit.getTemperatureUnit(unitSystem); String windUnit = Unit.getWindUnit(unitSystem); diff --git a/src/main/java/by/prominence/openweathermap/api/WeatherRequester.java b/src/main/java/by/prominence/openweathermap/api/WeatherRequester.java index 5381bf6..7ee42fe 100644 --- a/src/main/java/by/prominence/openweathermap/api/WeatherRequester.java +++ b/src/main/java/by/prominence/openweathermap/api/WeatherRequester.java @@ -25,14 +25,14 @@ package by.prominence.openweathermap.api; import by.prominence.openweathermap.api.constants.Unit; import by.prominence.openweathermap.api.exception.DataNotFoundException; import by.prominence.openweathermap.api.exception.InvalidAuthTokenException; -import by.prominence.openweathermap.api.model.response.WeatherResponse; +import by.prominence.openweathermap.api.model.response.Weather; import by.prominence.openweathermap.api.utils.JsonUtils; import by.prominence.openweathermap.api.utils.RequestUtils; import java.io.IOException; import java.io.InputStream; -public class WeatherRequester extends BasicRequester { +public class WeatherRequester extends BasicRequester { WeatherRequester(String authToken) { super(authToken); @@ -57,21 +57,21 @@ public class WeatherRequester extends BasicRequester { return "weather"; } - protected WeatherResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { + protected Weather executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { - WeatherResponse weatherResponse = null; + Weather weather = null; try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters)); - weatherResponse = (WeatherResponse)JsonUtils.parseJson(requestResult, WeatherResponse.class); + weather = (Weather)JsonUtils.parseJson(requestResult, Weather.class); - weatherResponse.getWind().setUnit(Unit.getWindUnit(unitSystem)); - weatherResponse.getWeatherInfo().setTemperatureUnit(Unit.getTemperatureUnit(unitSystem)); + weather.getWind().setUnit(Unit.getWindUnit(unitSystem)); + weather.getWeatherInfo().setTemperatureUnit(Unit.getTemperatureUnit(unitSystem)); } catch (IOException ex) { ex.printStackTrace(); } - return weatherResponse; + return weather; } } diff --git a/src/main/java/by/prominence/openweathermap/api/model/Weather.java b/src/main/java/by/prominence/openweathermap/api/model/WeatherState.java similarity index 89% rename from src/main/java/by/prominence/openweathermap/api/model/Weather.java rename to src/main/java/by/prominence/openweathermap/api/model/WeatherState.java index 7ff4189..8bb3272 100644 --- a/src/main/java/by/prominence/openweathermap/api/model/Weather.java +++ b/src/main/java/by/prominence/openweathermap/api/model/WeatherState.java @@ -28,7 +28,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Objects; -public class Weather { +public class WeatherState { @JSONField(name = "id") // Weather condition id @@ -97,11 +97,11 @@ public class Weather { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Weather weather = (Weather) o; - return conditionId == weather.conditionId && - Objects.equals(weatherGroup, weather.weatherGroup) && - Objects.equals(description, weather.description) && - Objects.equals(iconId, weather.iconId); + WeatherState weatherState = (WeatherState) o; + return conditionId == weatherState.conditionId && + Objects.equals(weatherGroup, weatherState.weatherGroup) && + Objects.equals(description, weatherState.description) && + Objects.equals(iconId, weatherState.iconId); } @Override diff --git a/src/main/java/by/prominence/openweathermap/api/model/response/ForecastResponse.java b/src/main/java/by/prominence/openweathermap/api/model/response/HourlyForecast.java similarity index 96% rename from src/main/java/by/prominence/openweathermap/api/model/response/ForecastResponse.java rename to src/main/java/by/prominence/openweathermap/api/model/response/HourlyForecast.java index bbe697e..6838d00 100644 --- a/src/main/java/by/prominence/openweathermap/api/model/response/ForecastResponse.java +++ b/src/main/java/by/prominence/openweathermap/api/model/response/HourlyForecast.java @@ -30,7 +30,7 @@ import java.util.Date; import java.util.List; import java.util.Objects; -public class ForecastResponse implements OpenWeatherResponse { +public class HourlyForecast implements OpenWeatherResponse { @JSONField(name = "cod") private short responseCode; @@ -158,7 +158,7 @@ public class ForecastResponse implements OpenWeatherResponse { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - ForecastResponse that = (ForecastResponse) o; + HourlyForecast that = (HourlyForecast) o; return responseCode == that.responseCode && Double.compare(that.message, message) == 0 && cnt == that.cnt && @@ -252,7 +252,7 @@ public class ForecastResponse implements OpenWeatherResponse { private MainInfo mainInfo; @JSONField(name = "weather") - private List weathers; + private List weatherStates; private Clouds clouds; @@ -276,6 +276,10 @@ public class ForecastResponse implements OpenWeatherResponse { this.dataCalculationTime = dataCalculationTime; } + public Date getDataCalculationDate() { + return new Date(dataCalculationTime * 1000); + } + public MainInfo getMainInfo() { return mainInfo; } @@ -284,12 +288,12 @@ public class ForecastResponse implements OpenWeatherResponse { this.mainInfo = mainInfo; } - public List getWeathers() { - return weathers; + public List getWeatherStates() { + return weatherStates; } - public void setWeathers(List weathers) { - this.weathers = weathers; + public void setWeatherStates(List weatherStates) { + this.weatherStates = weatherStates; } public Clouds getClouds() { @@ -346,10 +350,10 @@ public class ForecastResponse implements OpenWeatherResponse { stringBuilder.append("Time: "); stringBuilder.append(new Date(dataCalculationTime * 1000)); stringBuilder.append(". "); - if (weathers.size() == 1) { - stringBuilder.append(weathers.get(0)); + if (weatherStates.size() == 1) { + stringBuilder.append(weatherStates.get(0)); } else { - stringBuilder.append(weathers); + stringBuilder.append(weatherStates); } stringBuilder.append(". "); stringBuilder.append(mainInfo); @@ -380,7 +384,7 @@ public class ForecastResponse implements OpenWeatherResponse { ForecastInfo that = (ForecastInfo) o; return dataCalculationTime == that.dataCalculationTime && Objects.equals(mainInfo, that.mainInfo) && - Objects.equals(weathers, that.weathers) && + Objects.equals(weatherStates, that.weatherStates) && Objects.equals(clouds, that.clouds) && Objects.equals(wind, that.wind) && Objects.equals(snow, that.snow) && @@ -392,7 +396,7 @@ public class ForecastResponse implements OpenWeatherResponse { @Override public int hashCode() { - return Objects.hash(dataCalculationTime, mainInfo, weathers, clouds, wind, snow, rain, systemInfo, dt_txt); + return Objects.hash(dataCalculationTime, mainInfo, weatherStates, clouds, wind, snow, rain, systemInfo, dt_txt); } public static class ForecastSystemInfo { diff --git a/src/main/java/by/prominence/openweathermap/api/model/response/WeatherResponse.java b/src/main/java/by/prominence/openweathermap/api/model/response/Weather.java similarity index 96% rename from src/main/java/by/prominence/openweathermap/api/model/response/WeatherResponse.java rename to src/main/java/by/prominence/openweathermap/api/model/response/Weather.java index dab44a1..3dfec55 100644 --- a/src/main/java/by/prominence/openweathermap/api/model/response/WeatherResponse.java +++ b/src/main/java/by/prominence/openweathermap/api/model/response/Weather.java @@ -29,7 +29,7 @@ import java.util.Date; import java.util.List; import java.util.Objects; -public class WeatherResponse implements OpenWeatherResponse { +public class Weather implements OpenWeatherResponse { @JSONField(name = "id") private long cityId; @@ -40,7 +40,8 @@ public class WeatherResponse implements OpenWeatherResponse { @JSONField(name = "coord") private Coordinates coordinates; - private List weather; + @JSONField(name = "weather") + private List weatherStates; private String base; @@ -88,12 +89,12 @@ public class WeatherResponse implements OpenWeatherResponse { this.coordinates = coordinates; } - public List getWeather() { - return weather; + public List getWeatherStates() { + return weatherStates; } - public void setWeather(List weather) { - this.weather = weather; + public void setWeatherStates(List weatherStates) { + this.weatherStates = weatherStates; } public String getBase() { @@ -173,8 +174,8 @@ public class WeatherResponse implements OpenWeatherResponse { } public String getWeatherDescription() { - if (weather != null && weather.size() > 0) { - return weather.get(0).getDescription(); + if (weatherStates != null && weatherStates.size() > 0) { + return weatherStates.get(0).getDescription(); } return null; } @@ -216,10 +217,10 @@ public class WeatherResponse implements OpenWeatherResponse { stringBuilder.append('\n'); stringBuilder.append(weatherSystemInfo); stringBuilder.append('\n'); - if (weather.size() == 1) { - stringBuilder.append(weather.get(0)); + if (weatherStates.size() == 1) { + stringBuilder.append(weatherStates.get(0)); } else { - stringBuilder.append(weather); + stringBuilder.append(weatherStates); } stringBuilder.append('\n'); stringBuilder.append(weatherInfo);