Response classes were renamed. Small adjustments for class/method names. Documentation was updated.

This commit is contained in:
Prominence 2018-07-17 22:57:11 +03:00
parent f32a164e79
commit 36432651fc
7 changed files with 63 additions and 95 deletions

View File

@ -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<ForecastResponse.ForecastInfo>` collection with all forecast information. |
| `getForecasts()` | Returns `List<HourlyForecast.ForecastInfo>` 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. |

View File

@ -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<T> extends AuthenticationTokenBasedRequester {
@ -87,23 +86,9 @@ abstract class BasicRequester<T> extends AuthenticationTokenBasedRequester {
urlBuilder.append(accuracy);
}
Map<String, String> 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<String, String> getAdditionalParameters() {
return null;
}
protected abstract String getRequestType();
protected abstract T executeRequest(String requestSpecificParamsString) throws InvalidAuthTokenException, DataNotFoundException;

View File

@ -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<ForecastResponse> {
private int amountOfDays = -1;
public class ForecastRequester extends BasicRequester<HourlyForecast> {
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<ForecastResponse> {
return this;
}
@Override
protected Map<String, String> getAdditionalParameters() {
Map<String, String> 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);

View File

@ -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<WeatherResponse> {
public class WeatherRequester extends BasicRequester<Weather> {
WeatherRequester(String authToken) {
super(authToken);
@ -57,21 +57,21 @@ public class WeatherRequester extends BasicRequester<WeatherResponse> {
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;
}
}

View File

@ -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

View File

@ -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<Weather> weathers;
private List<WeatherState> 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<Weather> getWeathers() {
return weathers;
public List<WeatherState> getWeatherStates() {
return weatherStates;
}
public void setWeathers(List<Weather> weathers) {
this.weathers = weathers;
public void setWeatherStates(List<WeatherState> 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 {

View File

@ -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> weather;
@JSONField(name = "weather")
private List<WeatherState> weatherStates;
private String base;
@ -88,12 +89,12 @@ public class WeatherResponse implements OpenWeatherResponse {
this.coordinates = coordinates;
}
public List<Weather> getWeather() {
return weather;
public List<WeatherState> getWeatherStates() {
return weatherStates;
}
public void setWeather(List<Weather> weather) {
this.weather = weather;
public void setWeatherStates(List<WeatherState> 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);