Usefull methods were added for responses.

This commit is contained in:
Prominence 2018-07-15 20:05:18 +03:00
parent c1fd32dbbb
commit 2aef540231
3 changed files with 98 additions and 8 deletions

View File

@ -24,5 +24,9 @@ package by.prominence.openweathermap.api.model;
public interface OpenWeatherResponse {
String getCityName();
long getCityId();
String getCountry();
Coordinates getCoordinates();
short getResponseCode();
}

View File

@ -25,6 +25,7 @@ package by.prominence.openweathermap.api.model.response;
import by.prominence.openweathermap.api.model.*;
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@ -85,6 +86,62 @@ public class ForecastResponse implements OpenWeatherResponse {
this.cityInfo = cityInfo;
}
public String getCityName() {
return cityInfo.name;
}
public long getCityId() {
return cityInfo.id;
}
public String getCountry() {
return cityInfo.country;
}
public Coordinates getCoordinates() {
return cityInfo.coordinates;
}
public float getAverageTemperature() {
return (float)forecasts.stream().mapToDouble(forecast -> forecast.mainInfo.temperature).average().orElse(0f);
}
public float getMinimumTemperature() {
return (float)forecasts.stream().mapToDouble(forecast -> forecast.mainInfo.temperature).min().orElse(0f);
}
public float getMaximumTemperature() {
return (float)forecasts.stream().mapToDouble(forecast -> forecast.mainInfo.temperature).max().orElse(0f);
}
public ForecastInfo getByMinimumTemperature() {
return forecasts.stream().min(Comparator.comparing(forecastInfo -> forecastInfo.mainInfo.minimumTemperature)).orElse(null);
}
public ForecastInfo getByMaximumTemperature() {
return forecasts.stream().max(Comparator.comparing(forecastInfo -> forecastInfo.mainInfo.maximumTemperature)).orElse(null);
}
public float getAveragePressure() {
return (float)forecasts.stream().mapToDouble(forecast -> forecast.mainInfo.pressure).average().orElse(0f);
}
public float getMinimumPressure() {
return (float)forecasts.stream().mapToDouble(forecast -> forecast.mainInfo.pressure).min().orElse(0f);
}
public float getMaximumPressure() {
return (float)forecasts.stream().mapToDouble(forecast -> forecast.mainInfo.pressure).max().orElse(0f);
}
public ForecastInfo getByMinimumPressure() {
return forecasts.stream().min(Comparator.comparing(forecastInfo -> forecastInfo.mainInfo.pressure)).orElse(null);
}
public ForecastInfo getByMaximumPressure() {
return forecasts.stream().max(Comparator.comparing(forecastInfo -> forecastInfo.mainInfo.pressure)).orElse(null);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
@ -288,7 +345,7 @@ public class ForecastResponse implements OpenWeatherResponse {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Time: ");
stringBuilder.append(new Date(dataCalculationTime * 1000));
stringBuilder.append(" ");
stringBuilder.append(". ");
if (weathers.size() == 1) {
stringBuilder.append(weathers.get(0));
} else {

View File

@ -40,25 +40,19 @@ public class WeatherResponse implements OpenWeatherResponse {
@JSONField(name = "coord")
private Coordinates coordinates;
@JSONField(name = "weather")
private List<Weather> weather;
@JSONField(name = "base")
private String base;
@JSONField(name = "main")
private WeatherInfo weatherInfo;
@JSONField(name = "wind")
private Wind wind;
@JSONField(name = "clouds")
private Clouds clouds;
@JSONField(name = "rain")
private Rain rain;
@JSONField(name = "snow")
private Snow snow;
@JSONField(name = "dt")
@ -174,6 +168,41 @@ public class WeatherResponse implements OpenWeatherResponse {
this.responseCode = responseCode;
}
public String getCountry() {
return weatherSystemInfo.country;
}
public String getWeatherDescription() {
if (weather != null && weather.size() > 0) {
return weather.get(0).getDescription();
}
return null;
}
public Date getDataCalculationDate() {
return new Date(dataCalculationTime * 1000);
}
public float getTemperature() {
return weatherInfo.temperature;
}
public char getTemperatureUnit() {
return weatherInfo.temperatureUnit;
}
public short getPressure() {
return weatherInfo.pressure;
}
public String getPressureUnit() {
return weatherInfo.getPressureUnit();
}
public byte getHumidityPercentage() {
return weatherInfo.humidity;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
@ -208,7 +237,7 @@ public class WeatherResponse implements OpenWeatherResponse {
stringBuilder.append('\n');
}
stringBuilder.append("Data calculation time: ");
stringBuilder.append(new Date(dataCalculationTime * 1000));
stringBuilder.append(getDataCalculationDate());
return stringBuilder.toString();
}