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
@@ -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;
@@ -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);
@@ -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;
}
}
@@ -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
@@ -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 {
@@ -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);