New mapping approach (#41)

* New mapping approach
- Refreshes license information in file headers
- Rewrites API models to eliminate most of the custom mapping code
- Redefines API configuration options
- Simplifies fetching and mapping logic
- Updates dependency versions
- Reduces Java source level to 8 everywhere to eliminate JavaDoc warnings related to generated code
- Moves some in-line JSONs to class path resources to make tests more clean
- Adds assumptions to skip integration tests if API key is not set
- Adds assumptions to skip One Call API tests unless RUN_ONE_CALL env var is set
- Solves issue around UnsupportedOperationExceptions in certain terminators
- Adds option to use secure channel for picture URLs
- Adds new tests

Signed-off-by: Esta Nagy <nagyesta@gmail.com>

* New mapping approach - Code review
- Minor fixes
- Adds new tests

Signed-off-by: Esta Nagy <nagyesta@gmail.com>

* New mapping approach - Code review
- Add more tests

Signed-off-by: Esta Nagy <nagyesta@gmail.com>

* New mapping approach - Code review
- Unified coordinate and time period usage
- Fixed local system dependent test

Signed-off-by: Esta Nagy <nagyesta@gmail.com>

* New mapping approach - Code review
- Fixed line separator issue in response processing
- Changed how unitSystem can be defined by moving this parameter to the JSON/XML/HTML terminator methods

Signed-off-by: Esta Nagy <nagyesta@gmail.com>

Signed-off-by: Esta Nagy <nagyesta@gmail.com>
This commit is contained in:
Esta Nagy 2022-09-30 23:57:01 +02:00 committed by GitHub
parent b83b121e90
commit a2b0360e4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
433 changed files with 15057 additions and 22885 deletions

View File

@ -1,5 +1,5 @@
container:
image: gradle:jdk17
image: gradle:jdk8
testCoverage_task:
gradle_cache:

View File

@ -3,22 +3,23 @@ plugins {
id 'maven-publish'
id 'signing'
id 'jacoco'
id 'io.freefair.lombok' version '6.5.0.3'
}
repositories {
mavenLocal()
maven {
url = uri('https://repo.maven.apache.org/maven2/')
}
mavenCentral()
}
dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.2.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4'
implementation 'org.slf4j:slf4j-api:1.7.36'
testImplementation 'org.junit.platform:junit-platform-runner:1.8.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
annotationProcessor 'com.github.bsideup.jabel:jabel-javac-plugin:0.4.2'
testImplementation 'org.junit.platform:junit-platform-runner:1.9.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.0'
testImplementation 'ch.qos.logback:logback-classic:1.2.11'
testImplementation 'commons-io:commons-io:2.11.0'
testImplementation 'org.mockito:mockito-core:4.8.0'
}
group = 'com.github.prominence'
@ -26,12 +27,7 @@ version = '3.0.0-SNAPSHOT'
description = 'Java OpenWeatherMap API'
configure([tasks.compileJava]) {
sourceCompatibility = 17 // for the IDE support
options.release = 8
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(17)
}
sourceCompatibility = 8
}
ext {

5
lombok.config Normal file
View File

@ -0,0 +1,5 @@
# This file is generated by the 'io.freefair.lombok' Gradle plugin
config.stopBubbling = true
lombok.addSuppressWarnings = true
lombok.addLombokGeneratedAnnotation = true
lombok.nonNull.exceptionType = IllegalArgumentException

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -23,9 +23,8 @@
package com.github.prominence.openweathermap.api;
import com.github.prominence.openweathermap.api.annotation.SubscriptionAvailability;
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
import com.github.prominence.openweathermap.api.core.net.HttpClient;
import com.github.prominence.openweathermap.api.core.net.HttpURLConnectionBasedHttpClient;
import com.github.prominence.openweathermap.api.context.ApiConfiguration;
import com.github.prominence.openweathermap.api.context.ApiConfigurationHolder;
import com.github.prominence.openweathermap.api.request.RequestSettings;
import com.github.prominence.openweathermap.api.request.air.pollution.AirPollutionRequester;
import com.github.prominence.openweathermap.api.request.forecast.climatic.ClimaticForecastRequester;
@ -37,47 +36,38 @@ import com.github.prominence.openweathermap.api.request.onecall.OneCallWeatherRe
import com.github.prominence.openweathermap.api.request.radiation.SolarRadiationRequester;
import com.github.prominence.openweathermap.api.request.roadrisk.RoadRiskRequester;
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherRequester;
import lombok.NonNull;
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.*;
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.ALL;
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.DEVELOPER;
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.ENTERPRISE;
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.PAID;
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.PROFESSIONAL;
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.SPECIAL;
/**
* The main public API client to communicate with OpenWeatherMap services.
* Requires API key for usage. More info on the website <a href="https://openweathermap.org/api">https://openweathermap.org/api</a>.
*/
public class OpenWeatherMapClient {
private final String apiKey;
private final TimeoutSettings timeoutSettings = new TimeoutSettings();
private final ApiConfiguration apiConfiguration;
private boolean useInsecureConnection = false;
private HttpClient httpClient = new HttpURLConnectionBasedHttpClient();
public OpenWeatherMapClient() {
this(ApiConfigurationHolder.getConfiguration());
}
/**
* Created OpenWeatherMap client object.
* @param apiKey API key obtained on <a href="https://home.openweathermap.org/api_keys">OpenWeatherMap site</a>.
*
* @param apiConfiguration configuration options.
*/
public OpenWeatherMapClient(String apiKey) {
this.apiKey = apiKey;
}
public void setConnectionTimeout(int connectionTimeout) {
timeoutSettings.setConnectionTimeout(connectionTimeout);
}
public void setReadTimeout(int readTimeout) {
timeoutSettings.setReadTimeout(readTimeout);
}
public void useInsecureConnection(boolean value) {
this.useInsecureConnection = value;
}
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
public OpenWeatherMapClient(@NonNull ApiConfiguration apiConfiguration) {
this.apiConfiguration = apiConfiguration;
}
/**
* Current Weather <a href="https://openweathermap.org/current">API</a>.
*
* @return requester for retrieving current weather information.
*/
@SubscriptionAvailability(plans = ALL)
@ -87,9 +77,10 @@ public class OpenWeatherMapClient {
/**
* Hourly forecast <a href="https://openweathermap.org/api/hourly-forecast">API</a>.
*
* @return requester for retrieving hourly weather forecast information for 4 days.
*/
@SubscriptionAvailability(plans = { DEVELOPER, PROFESSIONAL, ENTERPRISE })
@SubscriptionAvailability(plans = {DEVELOPER, PROFESSIONAL, ENTERPRISE})
public FourDaysHourlyForecastRequester forecastHourly4Days() {
return new FourDaysHourlyForecastRequester(getRequestSettings());
}
@ -97,6 +88,7 @@ public class OpenWeatherMapClient {
/**
* One Call <a href="https://openweathermap.org/api/one-call-api">API</a>.
* To get information about current weather, minute forecast for 1 hour, hourly forecast for 48 hours, daily forecast for 7 days and government weather alerts.
*
* @return requester for retrieving one call weather information.
*/
@SubscriptionAvailability(plans = ALL)
@ -106,6 +98,7 @@ public class OpenWeatherMapClient {
/**
* Daily forecast <a href="https://openweathermap.org/api/hourly-forecast">API</a>.
*
* @return requester for retrieving daily weather forecast information for 16 days.
*/
@SubscriptionAvailability(plans = PAID)
@ -115,15 +108,17 @@ public class OpenWeatherMapClient {
/**
* Climatic forecast <a href="https://openweathermap.org/api/forecast30">API</a>.
*
* @return requester for retrieving climatic weather forecast information for 30 days.
*/
@SubscriptionAvailability(plans = { DEVELOPER, PROFESSIONAL, ENTERPRISE })
@SubscriptionAvailability(plans = {DEVELOPER, PROFESSIONAL, ENTERPRISE})
public ClimaticForecastRequester climaticForecast30Days() {
return new ClimaticForecastRequester(getRequestSettings());
}
/**
* Solar Radiation <a href="https://openweathermap.org/api/solar-radiation">API</a>.
*
* @return requester for retrieving solar radiation information.
*/
@SubscriptionAvailability(plans = SPECIAL)
@ -133,6 +128,7 @@ public class OpenWeatherMapClient {
/**
* 5 Day / 3 Hour Forecast <a href="https://openweathermap.org/forecast5">API</a>.
*
* @return requester for retrieving 5 day/3-hour weather forecast information.
*/
@SubscriptionAvailability(plans = ALL)
@ -142,6 +138,7 @@ public class OpenWeatherMapClient {
/**
* Road Risk <a href="https://openweathermap.org/api/road-risk">API</a>.
*
* @return requester for retrieving road risk information.
*/
@SubscriptionAvailability(plans = SPECIAL)
@ -152,6 +149,7 @@ public class OpenWeatherMapClient {
/**
* Air Pollution <a href="https://openweathermap.org/api/air-pollution">API</a>.
* Air Pollution API provides current, forecast and historical air pollution data for any coordinates on the globe.
*
* @return requester for air pollution information retrieval.
*/
@SubscriptionAvailability(plans = ALL)
@ -165,8 +163,6 @@ public class OpenWeatherMapClient {
}
private RequestSettings getRequestSettings() {
final RequestSettings requestSettings = new RequestSettings(apiKey, timeoutSettings, useInsecureConnection);
requestSettings.setHttpClient(httpClient);
return requestSettings;
return new RequestSettings(apiConfiguration);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -22,9 +22,12 @@
package com.github.prominence.openweathermap.api.conf;
public class TimeoutSettings {
private Integer connectionTimeout;
private Integer readTimeout;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode
public final class TimeoutSettings {
private final Integer connectionTimeout;
private final Integer readTimeout;
public TimeoutSettings() {
this(2000, 2000);
@ -44,15 +47,7 @@ public class TimeoutSettings {
return connectionTimeout;
}
public void setConnectionTimeout(Integer connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public Integer getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(Integer readTimeout) {
this.readTimeout = readTimeout;
}
}
}

View File

@ -0,0 +1,121 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.context;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
import com.github.prominence.openweathermap.api.core.net.HttpClient;
import com.github.prominence.openweathermap.api.core.net.HttpURLConnectionBasedHttpClient;
import com.github.prominence.openweathermap.api.enums.ApiVariant;
import lombok.Getter;
import lombok.NonNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Getter
public class ApiConfiguration {
@NonNull
private final String apiKey;
@NonNull
private final Map<ApiVariant, String> baseUrls;
@NonNull
private final HttpClient httpClient;
@NonNull
private final TimeoutSettings defaultTimeoutSettings;
private final ObjectReader objectReader;
private final ObjectWriter objectWriter;
private ApiConfiguration(String apiKey, Map<ApiVariant, String> baseUrls, HttpClient httpClient,
ObjectMapper objectMapper, TimeoutSettings defaultTimeoutSettings) {
this.apiKey = apiKey;
this.baseUrls = Collections.unmodifiableMap(baseUrls);
this.httpClient = httpClient;
this.defaultTimeoutSettings = defaultTimeoutSettings;
this.objectReader = objectMapper.reader();
this.objectWriter = objectMapper.writer();
}
public static ApiConfigurationBuilder builder() {
return new ApiConfigurationBuilder();
}
public static class ApiConfigurationBuilder {
private String apiKey;
private Map<ApiVariant, String> baseUrls;
private HttpClient httpClient = new HttpURLConnectionBasedHttpClient();
private TimeoutSettings defaultTimeoutSettings = new TimeoutSettings();
private ObjectMapper objectMapper = new ObjectMapper();
public ApiConfigurationBuilder() {
baseUrls = Arrays.stream(ApiVariant.values())
.collect(Collectors.toMap(Function.identity(), ApiVariant::getBaseUrl));
}
public ApiConfigurationBuilder apiKey(@NonNull String apiKey) {
this.apiKey = apiKey;
return this;
}
public ApiConfigurationBuilder baseUrls(@NonNull Map<ApiVariant, String> baseUrls) {
final List<ApiVariant> variants = Arrays.stream(ApiVariant.values()).collect(Collectors.toList());
if (!baseUrls.keySet().containsAll(variants)) {
throw new IllegalArgumentException("Not all API variants were found: " + baseUrls.keySet() + " , expected: " + variants);
}
this.baseUrls = new HashMap<>(baseUrls);
return this;
}
public ApiConfigurationBuilder httpClient(@NonNull HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}
public ApiConfigurationBuilder objectMapper(@NonNull ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}
public ApiConfigurationBuilder defaultTimeoutSettings(@NonNull TimeoutSettings defaultTimeoutSettings) {
this.defaultTimeoutSettings = new TimeoutSettings(defaultTimeoutSettings);
return this;
}
public ApiConfiguration build() {
return new ApiConfiguration(apiKey, baseUrls, httpClient, objectMapper, defaultTimeoutSettings);
}
public String toString() {
return "ApiConfiguration.ApiConfigurationBuilder(apiKey=" + this.apiKey + ")";
}
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.context;
import lombok.NonNull;
public class ApiConfigurationHolder {
private static ApiConfiguration configuration = ApiConfiguration.builder()
.apiKey(System.getenv("OPENWEATHER_API_KEY"))
.build();
public static ApiConfiguration getConfiguration() {
return configuration;
}
public static void setConfiguration(@NonNull ApiConfiguration configuration) {
ApiConfigurationHolder.configuration = configuration;
}
}

View File

@ -1,3 +1,25 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.core.net;
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;

View File

@ -1,3 +1,25 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.core.net;
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
@ -6,10 +28,16 @@ import com.github.prominence.openweathermap.api.exception.NoDataFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.stream.Collectors;
public class HttpURLConnectionBasedHttpClient implements HttpClient {
private static final Logger logger = LoggerFactory.getLogger(HttpURLConnectionBasedHttpClient.class);
@ -23,82 +51,86 @@ public class HttpURLConnectionBasedHttpClient implements HttpClient {
@Override
public String executeGetRequest(String url) {
InputStream resultStream;
return doExecute(url, RequestExecutor.Method.GET, null);
}
private String doExecute(String url, RequestExecutor.Method method, String body) {
InputStream resultStream = null;
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
HttpURLConnection connection = getConnection(url);
configureTimeouts(connection);
configureConnection(connection, method, body);
if (timeoutSettings != null) {
if (timeoutSettings.getConnectionTimeout() != null) {
connection.setConnectTimeout(timeoutSettings.getConnectionTimeout());
}
if (timeoutSettings.getReadTimeout() != null) {
connection.setReadTimeout(timeoutSettings.getReadTimeout());
}
}
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
resultStream = switch (connection.getResponseCode()) {
case HttpURLConnection.HTTP_OK -> connection.getInputStream();
case HttpURLConnection.HTTP_UNAUTHORIZED -> throw new InvalidAuthTokenException();
case HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST ->
throw new NoDataFoundException();
default -> throw new IllegalStateException("Unexpected value: " + connection.getResponseCode());
};
resultStream = evaluateResponse(connection);
logger.debug("Executing OpenWeatherMap API request: " + url);
return convertInputStreamToString(resultStream);
} catch (IllegalStateException | IOException ex) {
logger.error("An error occurred during OpenWeatherMap API response parsing: ", ex);
throw new NoDataFoundException(ex);
} finally {
closeQuietly(resultStream);
}
logger.debug("Executing OpenWeatherMap API request: " + url);
}
return convertInputStreamToString(resultStream);
HttpURLConnection getConnection(String url) throws IOException {
return (HttpURLConnection) new URL(url).openConnection();
}
private void closeQuietly(InputStream resultStream) {
if (resultStream != null) {
try {
resultStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private InputStream evaluateResponse(HttpURLConnection connection) throws IOException {
final int responseCode = connection.getResponseCode();
switch (responseCode) {
case HttpURLConnection.HTTP_OK:
return connection.getInputStream();
case HttpURLConnection.HTTP_UNAUTHORIZED:
throw new InvalidAuthTokenException();
case HttpURLConnection.HTTP_NOT_FOUND:
case HttpURLConnection.HTTP_BAD_REQUEST:
throw new NoDataFoundException();
default:
throw new IllegalStateException("Unexpected value: " + responseCode);
}
}
private void configureTimeouts(HttpURLConnection connection) {
Optional.ofNullable(timeoutSettings)
.ifPresent(ts -> {
Optional.ofNullable(ts.getConnectionTimeout())
.ifPresent(connection::setConnectTimeout);
Optional.ofNullable(ts.getReadTimeout())
.ifPresent(connection::setReadTimeout);
});
}
private void configureConnection(HttpURLConnection connection, RequestExecutor.Method method, String body) throws IOException {
connection.setRequestMethod(method.name());
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
addOptionalBodyContent(connection, body);
}
@Override
public String executePostRequest(String url, String body) {
InputStream resultStream;
return doExecute(url, RequestExecutor.Method.POST, body);
}
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
if (timeoutSettings != null) {
if (timeoutSettings.getConnectionTimeout() != null) {
connection.setConnectTimeout(timeoutSettings.getConnectionTimeout());
}
if (timeoutSettings.getReadTimeout() != null) {
connection.setReadTimeout(timeoutSettings.getReadTimeout());
}
}
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
private void addOptionalBodyContent(HttpURLConnection connection, String body) throws IOException {
if (body != null) {
connection.setDoOutput(true);
try(OutputStream os = connection.getOutputStream()) {
try (OutputStream os = connection.getOutputStream()) {
byte[] input = body.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
resultStream = switch (connection.getResponseCode()) {
case HttpURLConnection.HTTP_OK -> connection.getInputStream();
case HttpURLConnection.HTTP_UNAUTHORIZED -> throw new InvalidAuthTokenException();
case HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST ->
throw new NoDataFoundException();
default -> throw new IllegalStateException("Unexpected value: " + connection.getResponseCode());
};
} catch (IllegalStateException | IOException ex) {
logger.error("An error occurred during OpenWeatherMap API response parsing: ", ex);
throw new NoDataFoundException(ex);
}
logger.debug("Executing OpenWeatherMap API request: " + url);
return convertInputStreamToString(resultStream);
}
/**
@ -108,19 +140,12 @@ public class HttpURLConnectionBasedHttpClient implements HttpClient {
* @return converted <code>InputStream</code> content.
* @throws IllegalArgumentException in case if input stream is unable to be read.
*/
private static String convertInputStreamToString(InputStream inputStream) {
StringBuilder result = new StringBuilder();
private String convertInputStreamToString(InputStream inputStream) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException ex) {
logger.error("Error during response reading: ", ex);
throw new IllegalArgumentException(ex);
}
return result.toString();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -23,15 +23,14 @@
package com.github.prominence.openweathermap.api.core.net;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.github.prominence.openweathermap.api.enums.ApiVariant;
import com.github.prominence.openweathermap.api.request.RequestSettings;
import java.net.URL;
import java.util.stream.Collectors;
public final class RequestExecutor {
private static final String OWM_URL_BASE = "https://SUBDOMAIN.openweathermap.org/";
private final RequestSettings requestSettings;
@ -39,12 +38,12 @@ public final class RequestExecutor {
this.requestSettings = requestSettings;
}
public String getResponse() {
return getResponse(Method.GET);
public String getResponse(ApiVariant variant) {
return getResponse(variant, Method.GET);
}
public String getResponse(Method httpMethod) {
return getResponse(buildRequestUrl(), httpMethod);
public String getResponse(ApiVariant variant, Method httpMethod) {
return getResponse(selectRequestUrl(variant), httpMethod);
}
/**
@ -56,7 +55,7 @@ public final class RequestExecutor {
* @throws IllegalArgumentException in case if provided parameter isn't a valid url for {@link URL} instance.
*/
private String getResponse(String url, Method httpMethod) {
final HttpClient httpClient = requestSettings.getHttpClient();
final HttpClient httpClient = requestSettings.getApiConfiguration().getHttpClient();
httpClient.setTimeoutSettings(requestSettings.getTimeoutSettings());
if (httpMethod == Method.GET) {
@ -66,12 +65,8 @@ public final class RequestExecutor {
}
}
private String buildRequestUrl() {
String baseUrl = OWM_URL_BASE.replace("SUBDOMAIN", requestSettings.getSubdomain());
if (requestSettings.isUseInsecureConnection()) {
baseUrl = baseUrl.replace("https", "http");
}
StringBuilder requestUrlBuilder = new StringBuilder(baseUrl);
private String selectRequestUrl(ApiVariant variant) {
StringBuilder requestUrlBuilder = new StringBuilder(requestSettings.getApiConfiguration().getBaseUrls().get(variant));
requestUrlBuilder.append(requestSettings.getUrlAppender());
requestUrlBuilder.append('?');
String parameters = requestSettings.getRequestParameters().entrySet().stream()
@ -82,13 +77,9 @@ public final class RequestExecutor {
}
private String getSerializedPayload() {
final ObjectMapper objectMapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
module.addSerializer(requestSettings.getPayloadClass(), requestSettings.getPayloadSerializer());
objectMapper.registerModule(module);
final ObjectWriter objectWriter = requestSettings.getApiConfiguration().getObjectWriter();
try {
return objectMapper.writeValueAsString(requestSettings.getPayloadObject());
return objectWriter.writeValueAsString(requestSettings.getPayloadObject());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}

View File

@ -1,52 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.AtmosphericPressure;
import java.io.IOException;
public class AtmosphericPressureDeserializer extends JsonDeserializer<AtmosphericPressure> {
@Override
public AtmosphericPressure deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode mainNode = p.getCodec().readTree(p);
final AtmosphericPressure atmosphericPressure = AtmosphericPressure.withValue(mainNode.get("pressure").asDouble());
final JsonNode seaLevelNode = mainNode.get("sea_level");
final JsonNode groundLevelNode = mainNode.get("grnd_level");
if (seaLevelNode != null) {
atmosphericPressure.setSeaLevelValue(seaLevelNode.asDouble());
}
if (groundLevelNode != null) {
atmosphericPressure.setGroundLevelValue(groundLevelNode.asDouble());
}
return atmosphericPressure;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -22,20 +22,21 @@
package com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.Humidity;
import com.github.prominence.openweathermap.api.model.air.pollution.Concentration;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Optional;
public class ConcentrationDeserializer extends JsonDeserializer<Concentration> {
public class HumidityDeserializer extends JsonDeserializer<Humidity> {
@Override
public Humidity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
return Humidity.withValue((byte) (rootNode.get("humidity").asInt()));
public Concentration deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(BigDecimal.class))
.map(Concentration::new)
.orElse(null);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -22,22 +22,23 @@
package com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.Coordinates;
import java.io.IOException;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Optional;
public class EpochSecondsDeserializer extends JsonDeserializer<OffsetDateTime> {
public class CoordinatesDeserializer extends JsonDeserializer<Coordinates> {
@Override
public Coordinates deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JacksonException {
JsonNode rootNode = jp.getCodec().readTree(jp);
if (rootNode.has("lat") && rootNode.has("lon")) {
return Coordinates.of(rootNode.get("lat").asDouble(), rootNode.get("lon").asDouble());
}
return null;
public OffsetDateTime deserialize(final JsonParser parser, final DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(Long.class))
.map(Instant::ofEpochSecond)
.map((Instant instant) -> OffsetDateTime.ofInstant(instant, ZoneOffset.UTC))
.orElse(null);
}
}

View File

@ -1,57 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.geocoding.GeocodingRecord;
import java.io.IOException;
import java.util.Map;
public class GeocodingRecordDeserializer extends JsonDeserializer<GeocodingRecord> {
private static final ObjectMapper objectMapper = new ObjectMapper();
public GeocodingRecordDeserializer() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(Coordinates.class, new CoordinatesDeserializer());
objectMapper.registerModule(module);
}
@Override
public GeocodingRecord deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JacksonException {
JsonNode rootNode = jp.getCodec().readTree(jp);
String name = rootNode.get("name").asText();
String country = rootNode.get("country").asText();
Map<String, String> localNames = objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("local_names")), new TypeReference<Map<String, String>>() {});
Coordinates coordinates = objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Coordinates.class);
return new GeocodingRecord(name, localNames, coordinates, country);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -20,21 +20,24 @@
* SOFTWARE.
*/
package com.github.prominence.openweathermap.api.deserializer.onecall;
package com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.onecall.AtmosphericPressure;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Optional;
public class OneCallAtmosphericPressureDeserializer extends JsonDeserializer<AtmosphericPressure> {
public class PercentageZeroToOneDeserializer extends JsonDeserializer<Integer> {
@Override
public AtmosphericPressure deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
return AtmosphericPressure.withValue(rootNode.get("pressure").asDouble());
public Integer deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(BigDecimal.class))
.map(v -> v.multiply(BigDecimal.valueOf(100)))
.map(v -> v.setScale(0, RoundingMode.HALF_EVEN))
.map(BigDecimal::intValue)
.orElse(null);
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.github.prominence.openweathermap.api.model.generic.precipitation.PrecipitationIntensity;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Optional;
public class PrecipitationIntensityDeserializer extends JsonDeserializer<PrecipitationIntensity> {
@Override
public PrecipitationIntensity deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(BigDecimal.class))
.map(PrecipitationIntensity::new)
.orElse(null);
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.util.Optional;
public class RequiredPercentageDeserializer extends JsonDeserializer<Integer> {
@Override
public Integer deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(Integer.class))
.filter(v -> v <= 100)
.filter(v -> v >= 0)
.orElseThrow(() -> new IllegalArgumentException("Invalid data found for percentage"));
}
}

View File

@ -1,59 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.Temperature;
import java.io.IOException;
public class TemperatureDeserializer extends JsonDeserializer<Temperature> {
@Override
public Temperature deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode mainNode = p.getCodec().readTree(p);
final UnitSystem unitSystem = (UnitSystem) ctxt.findInjectableValue("unitSystem", null, null);
final double tempValue = mainNode.get("temp").asDouble();
final Temperature temperature = Temperature.withValue(tempValue, unitSystem.getTemperatureUnit());
final JsonNode feelsLikeNode = mainNode.get("feels_like");
if (feelsLikeNode != null) {
temperature.setFeelsLike(feelsLikeNode.asDouble());
}
final JsonNode tempMaxNode = mainNode.get("temp_max");
if (tempMaxNode != null) {
temperature.setMaxTemperature(tempMaxNode.asDouble());
}
final JsonNode tempMinNode = mainNode.get("temp_min");
if (tempMinNode != null) {
temperature.setMinTemperature(tempMinNode.asDouble());
}
return temperature;
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.github.prominence.openweathermap.api.model.generic.temperature.TemperatureValue;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Optional;
public class TemperatureValueDeserializer extends JsonDeserializer<TemperatureValue> {
@Override
public TemperatureValue deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(BigDecimal.class))
.map(TemperatureValue::new)
.orElse(null);
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.github.prominence.openweathermap.api.model.generic.visibility.Visibility;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Optional;
public class VisibilityDeserializer extends JsonDeserializer<Visibility> {
@Override
public Visibility deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(BigDecimal.class))
.map(Visibility::new)
.orElse(null);
}
}

View File

@ -1,46 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.WeatherState;
import java.io.IOException;
public class WeatherStateDeserializer extends JsonDeserializer<WeatherState> {
@Override
public WeatherState deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
JsonNode weatherNode = p.getCodec().readTree(p);
final WeatherState weatherState = new WeatherState(
weatherNode.get("id").asInt(),
weatherNode.get("main").asText(),
weatherNode.get("description").asText()
);
weatherState.setIconId(weatherNode.get("icon").asText());
return weatherState;
}
}

View File

@ -1,66 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.Wind;
import java.io.IOException;
public class WindDeserializer extends JsonDeserializer<Wind> {
@Override
public Wind deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode windNode = p.getCodec().readTree(p);
final UnitSystem unitSystem = (UnitSystem) ctxt.findInjectableValue("unitSystem", null, null);
JsonNode speedNode = windNode.get("speed");
if (speedNode == null) {
speedNode = windNode.get("wind_speed");
}
double speed = speedNode.asDouble();
final Wind wind = Wind.withValue(speed, unitSystem.getWindUnit());
JsonNode degNode = windNode.get("deg");
if (degNode == null) {
degNode = windNode.get("wind_deg");
}
if (degNode != null) {
wind.setDegrees(degNode.asDouble());
}
JsonNode gustNode = windNode.get("gust");
if (gustNode == null) {
gustNode = windNode.get("wind_gust");
}
if (gustNode != null) {
wind.setGust(gustNode.asDouble());
}
return wind;
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.github.prominence.openweathermap.api.model.generic.wind.WindSpeed;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Optional;
public class WindSpeedDeserializer extends JsonDeserializer<WindSpeed> {
@Override
public WindSpeed deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(BigDecimal.class))
.map(WindSpeed::new)
.orElse(null);
}
}

View File

@ -1,57 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.geocoding.ZipCodeGeocodingRecord;
import java.io.IOException;
public class ZipCodeGeocodingDeserializer extends JsonDeserializer<ZipCodeGeocodingRecord> {
private static final ObjectMapper objectMapper = new ObjectMapper();
public ZipCodeGeocodingDeserializer() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(Coordinates.class, new CoordinatesDeserializer());
objectMapper.registerModule(module);
}
@Override
public ZipCodeGeocodingRecord deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
JsonNode rootNode = p.getCodec().readTree(p);
String zip = rootNode.get("zip").asText();
String name = rootNode.get("name").asText();
String country = rootNode.get("country").asText();
Coordinates coordinates = objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Coordinates.class);
return new ZipCodeGeocodingRecord(zip, name, coordinates, country);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -22,24 +22,20 @@
package com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.Clouds;
import java.io.IOException;
import java.time.ZoneId;
import java.util.Optional;
public class ZoneIdDeserializer extends JsonDeserializer<ZoneId> {
public class CloudsDeserializer extends JsonDeserializer<Clouds> {
@Override
public Clouds deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode cloudsNode = p.getCodec().readTree(p);
final JsonNode allValueNode = cloudsNode.get("all");
if (allValueNode != null) {
return Clouds.withValue((byte) allValueNode.asInt());
}
return null;
public ZoneId deserialize(final JsonParser parser, final DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(String.class))
.map(ZoneId::of)
.orElse(null);
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.ZoneOffset;
import java.util.Optional;
public class ZoneOffsetDeserializer extends JsonDeserializer<ZoneOffset> {
@Override
public ZoneOffset deserialize(final JsonParser parser, final DeserializationContext context) throws IOException {
return Optional.ofNullable(parser.readValueAs(Integer.class))
.map(ZoneOffset::ofTotalSeconds)
.orElse(null);
}
}

View File

@ -1,18 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.climatic;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.climatic.AtmosphericPressure;
import java.io.IOException;
public class ClimaticForecastAtmosphericPressureDeserializer extends JsonDeserializer<AtmosphericPressure> {
@Override
public AtmosphericPressure deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return (AtmosphericPressure) AtmosphericPressure.withValue(jsonNode.get("pressure").asDouble());
}
}

View File

@ -1,23 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.climatic;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.Clouds;
import java.io.IOException;
public class ClimaticForecastCloudsDeserializer extends JsonDeserializer<Clouds> {
@Override
public Clouds deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final JsonNode cloudsNode = rootNode.get("clouds");
if (cloudsNode != null) {
return Clouds.withValue((byte) cloudsNode.asInt());
}
return null;
}
}

View File

@ -1,54 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.climatic;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.CoordinatesDeserializer;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.forecast.climatic.Location;
import java.io.IOException;
import static com.github.prominence.openweathermap.api.utils.JsonDeserializationUtils.parseZoneOffset;
public class ClimaticForecastLocationDeserializer extends JsonDeserializer<Location> {
private final ObjectMapper objectMapper = new ObjectMapper();
public ClimaticForecastLocationDeserializer() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(Coordinates.class, new CoordinatesDeserializer());
objectMapper.registerModule(module);
}
@Override
public Location deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final Location location = (Location) Location.withValues(rootNode.get("id").asInt(), rootNode.get("name").asText());
final JsonNode timezoneNode = rootNode.get("timezone");
if (timezoneNode != null) {
location.setZoneOffset(parseZoneOffset(timezoneNode));
}
final JsonNode countryNode = rootNode.get("country");
if (countryNode != null) {
location.setCountryCode(countryNode.asText());
}
final JsonNode coordNode = rootNode.get("coord");
if (coordNode != null) {
location.setCoordinates(objectMapper.readValue(objectMapper.treeAsTokens(coordNode), Coordinates.class));
}
final JsonNode populationNode = rootNode.get("population");
if (populationNode != null) {
location.setPopulation(populationNode.asLong());
}
return location;
}
}

View File

@ -1,22 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.climatic;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.climatic.Rain;
import java.io.IOException;
public class ClimaticForecastRainDeserializer extends JsonDeserializer<Rain> {
@Override
public Rain deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final JsonNode rainNode = rootNode.get("rain");
if (rainNode != null) {
return (Rain) Rain.withValue(rainNode.asDouble());
}
return null;
}
}

View File

@ -1,22 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.climatic;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.climatic.Snow;
import java.io.IOException;
public class ClimaticForecastSnowDeserializer extends JsonDeserializer<Snow> {
@Override
public Snow deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final JsonNode snowNode = rootNode.get("snow");
if (snowNode != null) {
return (Snow) Snow.withValue(snowNode.asDouble());
}
return null;
}
}

View File

@ -1,34 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.climatic;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.climatic.Temperature;
import java.io.IOException;
public class ClimaticForecastTemperatureDeserializer extends JsonDeserializer<Temperature> {
@Override
public Temperature deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final Temperature temperature = new Temperature();
final JsonNode tempNode = rootNode.get("temp");
temperature.setMorning(tempNode.get("morn").asDouble());
temperature.setDay(tempNode.get("day").asDouble());
temperature.setEve(tempNode.get("eve").asDouble());
temperature.setNight(tempNode.get("night").asDouble());
temperature.setMin(tempNode.get("min").asDouble());
temperature.setMax(tempNode.get("max").asDouble());
final JsonNode feelsLikeNode = rootNode.get("feels_like");
temperature.setMorningFeelsLike(feelsLikeNode.get("morn").asDouble());
temperature.setDayFeelsLike(feelsLikeNode.get("day").asDouble());
temperature.setEveFeelsLike(feelsLikeNode.get("eve").asDouble());
temperature.setNightFeelsLike(feelsLikeNode.get("night").asDouble());
return temperature;
}
}

View File

@ -1,58 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.forecast.climatic;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.Wind;
import java.io.IOException;
public class ClimaticForecastWindDeserializer extends JsonDeserializer<Wind> {
@Override
public Wind deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode windNode = p.getCodec().readTree(p);
final UnitSystem unitSystem = (UnitSystem) ctxt.findInjectableValue("unitSystem", null, null);
JsonNode speedNode = windNode.get("speed");
if (speedNode == null) {
speedNode = windNode.get("wind_speed");
}
double speed = speedNode.asDouble();
final Wind wind = Wind.withValue(speed, unitSystem.getWindUnit());
JsonNode degNode = windNode.get("deg");
if (degNode == null) {
degNode = windNode.get("wind_deg");
}
if (degNode != null) {
wind.setDegrees(degNode.asDouble());
}
return wind;
}
}

View File

@ -1,18 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.daily;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.daily.AtmosphericPressure;
import java.io.IOException;
public class DailyForecastAtmosphericPressureDeserializer extends JsonDeserializer<AtmosphericPressure> {
@Override
public AtmosphericPressure deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return (AtmosphericPressure) AtmosphericPressure.withValue(jsonNode.get("pressure").asDouble());
}
}

View File

@ -1,23 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.daily;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.Clouds;
import java.io.IOException;
public class DailyForecastCloudsDeserializer extends JsonDeserializer<Clouds> {
@Override
public Clouds deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final JsonNode cloudsNode = rootNode.get("clouds");
if (cloudsNode != null) {
return Clouds.withValue((byte) cloudsNode.asInt());
}
return null;
}
}

View File

@ -1,54 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.daily;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.CoordinatesDeserializer;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.forecast.daily.Location;
import java.io.IOException;
import static com.github.prominence.openweathermap.api.utils.JsonDeserializationUtils.parseZoneOffset;
public class DailyForecastLocationDeserializer extends JsonDeserializer<Location> {
private final ObjectMapper objectMapper = new ObjectMapper();
public DailyForecastLocationDeserializer() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(Coordinates.class, new CoordinatesDeserializer());
objectMapper.registerModule(module);
}
@Override
public Location deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final Location location = (Location) Location.withValues(rootNode.get("id").asInt(), rootNode.get("name").asText());
final JsonNode timezoneNode = rootNode.get("timezone");
if (timezoneNode != null) {
location.setZoneOffset(parseZoneOffset(timezoneNode));
}
final JsonNode countryNode = rootNode.get("country");
if (countryNode != null) {
location.setCountryCode(countryNode.asText());
}
final JsonNode coordNode = rootNode.get("coord");
if (coordNode != null) {
location.setCoordinates(objectMapper.readValue(objectMapper.treeAsTokens(coordNode), Coordinates.class));
}
final JsonNode populationNode = rootNode.get("population");
if (populationNode != null) {
location.setPopulation(populationNode.asLong());
}
return location;
}
}

View File

@ -1,22 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.daily;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.daily.Rain;
import java.io.IOException;
public class DailyForecastRainDeserializer extends JsonDeserializer<Rain> {
@Override
public Rain deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final JsonNode rainNode = rootNode.get("rain");
if (rainNode != null) {
return (Rain) Rain.withValue(rainNode.asDouble());
}
return null;
}
}

View File

@ -1,22 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.daily;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.daily.Snow;
import java.io.IOException;
public class DailyForecastSnowDeserializer extends JsonDeserializer<Snow> {
@Override
public Snow deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final JsonNode snowNode = rootNode.get("snow");
if (snowNode != null) {
return (Snow) Snow.withValue(snowNode.asDouble());
}
return null;
}
}

View File

@ -1,34 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.forecast.daily;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.daily.Temperature;
import java.io.IOException;
public class DailyForecastTemperatureDeserializer extends JsonDeserializer<Temperature> {
@Override
public Temperature deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final Temperature temperature = new Temperature();
final JsonNode tempNode = rootNode.get("temp");
temperature.setMorning(tempNode.get("morn").asDouble());
temperature.setDay(tempNode.get("day").asDouble());
temperature.setEve(tempNode.get("eve").asDouble());
temperature.setNight(tempNode.get("night").asDouble());
temperature.setMin(tempNode.get("min").asDouble());
temperature.setMax(tempNode.get("max").asDouble());
final JsonNode feelsLikeNode = rootNode.get("feels_like");
temperature.setMorningFeelsLike(feelsLikeNode.get("morn").asDouble());
temperature.setDayFeelsLike(feelsLikeNode.get("day").asDouble());
temperature.setEveFeelsLike(feelsLikeNode.get("eve").asDouble());
temperature.setNightFeelsLike(feelsLikeNode.get("night").asDouble());
return temperature;
}
}

View File

@ -1,87 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.forecast.free;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.CoordinatesDeserializer;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.forecast.free.Location;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.TimeZone;
public class FreeForecastLocationDeserializer extends JsonDeserializer<Location> {
private final ObjectMapper objectMapper = new ObjectMapper();
public FreeForecastLocationDeserializer() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(Coordinates.class, new CoordinatesDeserializer());
objectMapper.registerModule(module);
}
@Override
public Location deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
final Location location = Location.withValues(rootNode.get("id").asInt(), rootNode.get("name").asText());
final JsonNode timezoneNode = rootNode.get("timezone");
if (timezoneNode != null) {
location.setZoneOffset(ZoneOffset.ofTotalSeconds(timezoneNode.asInt()));
}
final JsonNode countryNode = rootNode.get("country");
if (countryNode != null) {
location.setCountryCode(countryNode.asText());
}
final JsonNode sunriseNode = rootNode.get("sunrise");
final JsonNode sunsetNode = rootNode.get("sunset");
if (sunriseNode != null) {
location.setSunriseTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(sunriseNode.asLong()), TimeZone.getDefault().toZoneId()));
}
if (sunsetNode != null) {
location.setSunsetTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(sunsetNode.asLong()), TimeZone.getDefault().toZoneId()));
}
final JsonNode coordNode = rootNode.get("coord");
if (coordNode != null) {
location.setCoordinates(objectMapper.readValue(objectMapper.treeAsTokens(coordNode), Coordinates.class));
}
final JsonNode populationNode = rootNode.get("population");
if (populationNode != null) {
location.setPopulation(populationNode.asLong());
}
return location;
}
}

View File

@ -1,82 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.forecast.hourly;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.CoordinatesDeserializer;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.forecast.hourly.Location;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.TimeZone;
public class HourlyForecastLocationDeserializer extends JsonDeserializer<Location> {
private final ObjectMapper objectMapper = new ObjectMapper();
public HourlyForecastLocationDeserializer() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(Coordinates.class, new CoordinatesDeserializer());
objectMapper.registerModule(module);
}
@Override
public Location deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
final Location location = Location.withValues(rootNode.get("id").asInt(), rootNode.get("name").asText());
final JsonNode timezoneNode = rootNode.get("timezone");
if (timezoneNode != null) {
location.setZoneOffset(ZoneOffset.ofTotalSeconds(timezoneNode.asInt()));
}
final JsonNode countryNode = rootNode.get("country");
if (countryNode != null) {
location.setCountryCode(countryNode.asText());
}
final JsonNode sunriseNode = rootNode.get("sunrise");
final JsonNode sunsetNode = rootNode.get("sunset");
if (sunriseNode != null) {
location.setSunriseTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(sunriseNode.asLong()), TimeZone.getDefault().toZoneId()));
}
if (sunsetNode != null) {
location.setSunsetTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(sunsetNode.asLong()), TimeZone.getDefault().toZoneId()));
}
final JsonNode coordNode = rootNode.get("coord");
if (coordNode != null) {
location.setCoordinates(objectMapper.readValue(objectMapper.treeAsTokens(coordNode), Coordinates.class));
}
return location;
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.forecast.hourly;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.hourly.Rain;
import java.io.IOException;
public class HourlyForecastRainDeserializer extends JsonDeserializer<Rain> {
@Override
public Rain deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rainNode = p.getCodec().readTree(p);
final JsonNode oneHourNode = rainNode.get("1h");
if (oneHourNode != null) {
return Rain.withOneHourLevelValue(oneHourNode.asDouble());
}
return null;
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.forecast.hourly;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.forecast.hourly.Snow;
import java.io.IOException;
public class HourlyForecastSnowDeserializer extends JsonDeserializer<Snow> {
@Override
public Snow deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode snowNode = p.getCodec().readTree(p);
final JsonNode oneHourNode = snowNode.get("1h");
if (oneHourNode != null) {
return Snow.withOneHourLevelValue(oneHourNode.asDouble());
}
return null;
}
}

View File

@ -1,60 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.onecall;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.onecall.current.Alert;
import com.github.prominence.openweathermap.api.utils.JsonDeserializationUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class AlertDeserializer extends JsonDeserializer<Alert> {
@Override
public Alert deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode alertNode = p.getCodec().readTree(p);
Alert alert = new Alert();
alert.setSenderName(alertNode.get("sender_name").asText());
alert.setEventName(alertNode.get("event").asText());
alert.setStartTime(JsonDeserializationUtils.parseDateTime(alertNode.get("start")));
alert.setEndTime(JsonDeserializationUtils.parseDateTime(alertNode.get("end")));
alert.setDescription(alertNode.get("description").asText());
final JsonNode tagsNode = alertNode.get("tags");
if (tagsNode != null) {
List<String> tags = new ArrayList<>();
for (JsonNode tagNode : tagsNode) {
tags.add(tagNode.asText());
}
alert.setTags(tags);
}
return alert;
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.onecall;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.onecall.current.DailyRain;
import java.io.IOException;
public class OneCallDailyRainDeserializer extends JsonDeserializer<DailyRain> {
@Override
public DailyRain deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
final JsonNode valueNode = rootNode.get("rain");
if (valueNode != null) {
return DailyRain.withValue(valueNode.asDouble());
}
return null;
}
}

View File

@ -1,56 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.onecall;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.onecall.current.DailyTemperature;
import java.io.IOException;
public class OneCallDailyTemperatureDeserializer extends JsonDeserializer<DailyTemperature> {
@Override
public DailyTemperature deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
final DailyTemperature temperature = new DailyTemperature();
final JsonNode tempNode = rootNode.get("temp");
temperature.setMorning(tempNode.get("morn").asDouble());
temperature.setDay(tempNode.get("day").asDouble());
temperature.setEve(tempNode.get("eve").asDouble());
temperature.setNight(tempNode.get("night").asDouble());
temperature.setMin(tempNode.get("min").asDouble());
temperature.setMax(tempNode.get("max").asDouble());
final JsonNode feelsLikeNode = rootNode.get("feels_like");
temperature.setMorningFeelsLike(feelsLikeNode.get("morn").asDouble());
temperature.setDayFeelsLike(feelsLikeNode.get("day").asDouble());
temperature.setEveFeelsLike(feelsLikeNode.get("eve").asDouble());
temperature.setNightFeelsLike(feelsLikeNode.get("night").asDouble());
return temperature;
}
}

View File

@ -1,47 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.onecall;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.onecall.Rain;
import java.io.IOException;
public class OneCallRainDeserializer extends JsonDeserializer<Rain> {
@Override
public Rain deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
final JsonNode rainNode = rootNode.get("rain");
if (rainNode != null) {
final JsonNode oneHourNode = rainNode.get("1h");
if (oneHourNode != null) {
return Rain.withOneHourLevelValue(oneHourNode.asDouble());
}
}
return null;
}
}

View File

@ -1,48 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.onecall;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.onecall.Rain;
import com.github.prominence.openweathermap.api.model.onecall.Snow;
import java.io.IOException;
public class OneCallSnowDeserializer extends JsonDeserializer<Snow> {
@Override
public Snow deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
final JsonNode snowNode = rootNode.get("snow");
if (snowNode != null) {
final JsonNode OneHourNode = snowNode.get("1h");
if (OneHourNode != null) {
Rain.withOneHourLevelValue(OneHourNode.asDouble());
}
}
return null;
}
}

View File

@ -1,55 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.onecall;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.onecall.Temperature;
import java.io.IOException;
public class OneCallTemperatureDeserializer extends JsonDeserializer<Temperature> {
@Override
public Temperature deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
final UnitSystem unitSystem = (UnitSystem) ctxt.findInjectableValue("unitSystem", null, null);
final double tempValue = rootNode.get("temp").asDouble();
final Temperature temperature = Temperature.withValue(tempValue, unitSystem.getTemperatureUnit());
final JsonNode tempFeelsLike = rootNode.get("feels_like");
if (tempFeelsLike != null) {
temperature.setFeelsLike(tempFeelsLike.asDouble());
}
final JsonNode dewPoint = rootNode.get("dew_point");
if (dewPoint != null) {
temperature.setDewPoint(dewPoint.asDouble());
}
return temperature;
}
}

View File

@ -1,29 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.radiation;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.radiation.SolarRadiationRecord;
import com.github.prominence.openweathermap.api.utils.JsonDeserializationUtils;
import java.io.IOException;
public class SolarRadiationRecordDeserializer extends JsonDeserializer<SolarRadiationRecord> {
@Override
public SolarRadiationRecord deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
SolarRadiationRecord record = new SolarRadiationRecord();
final JsonNode radiationNode = rootNode.get("radiation");
record.setMeasurementTime(JsonDeserializationUtils.parseDateTime(rootNode.get("dt")));
record.setCloudSkyGlobalHorizontalIrradiance(radiationNode.get("ghi").asDouble());
record.setCloudSkyDirectNormalIrradiance(radiationNode.get("dni").asDouble());
record.setCloudSkyDiffuseHorizontalIrradiance(radiationNode.get("dhi").asDouble());
record.setClearSkyGlobalHorizontalIrradiance(radiationNode.get("ghi_cs").asDouble());
record.setClearSkyDirectNormalIrradiance(radiationNode.get("dni_cs").asDouble());
record.setClearSkyDiffuseHorizontalIrradiance(radiationNode.get("dhi_cs").asDouble());
return record;
}
}

View File

@ -1,23 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.roadrisk;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.enums.EventLevel;
import com.github.prominence.openweathermap.api.model.roadrisk.Alert;
import java.io.IOException;
public class RoadRiskAlertDeserializer extends JsonDeserializer<Alert> {
@Override
public Alert deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final Alert alert = new Alert();
alert.setEvent(rootNode.get("event").asText());
alert.setSenderName(rootNode.get("sender_name").asText());
alert.setEventLevel(EventLevel.findByValue(rootNode.get("event_level").asInt()));
return alert;
}
}

View File

@ -1,23 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.roadrisk;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.enums.RoadState;
import com.github.prominence.openweathermap.api.model.roadrisk.RoadDetails;
import java.io.IOException;
public class RoadRiskRoadDetailsDeserializer extends JsonDeserializer<RoadDetails> {
@Override
public RoadDetails deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final RoadDetails roadDetails = new RoadDetails();
roadDetails.setSurfaceTemperature(rootNode.get("temp").asDouble());
roadDetails.setRoadState(RoadState.findByValue(rootNode.get("state").asInt()));
return roadDetails;
}
}

View File

@ -1,31 +0,0 @@
package com.github.prominence.openweathermap.api.deserializer.roadrisk;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.roadrisk.Weather;
import java.io.IOException;
public class RoadRiskWeatherDeserializer extends JsonDeserializer<Weather> {
@Override
public Weather deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode rootNode = jsonParser.getCodec().readTree(jsonParser);
final Weather weather = new Weather();
weather.setTemperature(rootNode.get("temp").asDouble());
weather.setDewPoint(rootNode.get("dew_point").asDouble());
weather.setWindSpeed(rootNode.get("wind_speed").asDouble());
weather.setWindDegrees(rootNode.get("wind_deg").asDouble());
if (rootNode.has("precipitation_intensity")) {
weather.setPrecipitationIntensity(rootNode.get("precipitation_intensity").asDouble());
}
if (rootNode.has("visibility")) {
weather.setVisibilityInMetres(rootNode.get("visibility").asDouble());
}
return weather;
}
}

View File

@ -1,85 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.weather;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.CoordinatesDeserializer;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.weather.Location;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.TimeZone;
public class WeatherLocationDeserializer extends JsonDeserializer<Location> {
private final ObjectMapper objectMapper = new ObjectMapper();
public WeatherLocationDeserializer() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(Coordinates.class, new CoordinatesDeserializer());
objectMapper.registerModule(module);
}
@Override
public Location deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rootNode = p.getCodec().readTree(p);
final Location location = Location.withValues(rootNode.get("id").asInt(), rootNode.get("name").asText());
final JsonNode timezoneNode = rootNode.get("timezone");
if (timezoneNode != null) {
location.setZoneOffset(ZoneOffset.ofTotalSeconds(timezoneNode.asInt()));
}
final JsonNode sysNode = rootNode.get("sys");
if (sysNode != null) {
final JsonNode countryNode = sysNode.get("country");
if (countryNode != null) {
location.setCountryCode(countryNode.asText());
}
final JsonNode sunriseNode = sysNode.get("sunrise");
final JsonNode sunsetNode = sysNode.get("sunset");
if (sunriseNode != null) {
location.setSunriseTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(sunriseNode.asInt()), TimeZone.getDefault().toZoneId()));
}
if (sunsetNode != null) {
location.setSunsetTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(sunsetNode.asInt()), TimeZone.getDefault().toZoneId()));
}
}
final JsonNode coordNode = rootNode.get("coord");
if (coordNode != null) {
location.setCoordinates(objectMapper.readValue(objectMapper.treeAsTokens(coordNode), Coordinates.class));
}
return location;
}
}

View File

@ -1,51 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.weather;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.weather.Rain;
import java.io.IOException;
public class WeatherRainDeserializer extends JsonDeserializer<Rain> {
@Override
public Rain deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode rainNode = p.getCodec().readTree(p);
if (rainNode != null) {
final JsonNode oneHourNode = rainNode.get("1h");
final JsonNode threeHourNode = rainNode.get("3h");
if (oneHourNode != null && threeHourNode != null) {
return Rain.withValues(oneHourNode.asDouble(), threeHourNode.asDouble());
} else if (oneHourNode != null) {
return Rain.withOneHourLevelValue(oneHourNode.asDouble());
} else if (threeHourNode != null) {
return Rain.withThreeHourLevelValue(threeHourNode.asDouble());
}
}
return null;
}
}

View File

@ -1,51 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.deserializer.weather;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.prominence.openweathermap.api.model.weather.Snow;
import java.io.IOException;
public class WeatherSnowDeserializer extends JsonDeserializer<Snow> {
@Override
public Snow deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
final JsonNode snowNode = p.getCodec().readTree(p);
if (snowNode != null) {
final JsonNode oneHourNode = snowNode.get("1h");
final JsonNode threeHourNode = snowNode.get("3h");
if (oneHourNode != null && threeHourNode != null) {
return Snow.withValues(oneHourNode.asDouble(), threeHourNode.asDouble());
} else if (oneHourNode != null) {
return Snow.withOneHourLevelValue(oneHourNode.asDouble());
} else if (threeHourNode != null) {
return Snow.withThreeHourLevelValue(threeHourNode.asDouble());
}
}
return null;
}
}

View File

@ -1,31 +1,31 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* * Copyright (c) 2021 Alexey Zinchenko
* *
* * 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.
* 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 com.github.prominence.openweathermap.api.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Arrays;
import java.util.Optional;
/**
* The enum Air quality index.
@ -73,8 +73,11 @@ public enum AirQualityIndex {
* @param index the index
* @return the by index
*/
public static AirQualityIndex getByIndex(int index) {
final Optional<AirQualityIndex> optionalAirQualityIndex = Arrays.stream(values()).filter(airQualityIndex -> airQualityIndex.getValue() == index).findFirst();
return optionalAirQualityIndex.orElse(null);
@JsonCreator
public static AirQualityIndex getByIndex(@JsonProperty("aqi") int index) {
return Arrays.stream(values())
.filter(airQualityIndex -> airQualityIndex.getValue() == index)
.findFirst()
.orElse(null);
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.enums;
public enum ApiVariant {
BASE("https://api.openweathermap.org/"),
PRO("https://pro.openweathermap.org/");
private final String baseUrl;
ApiVariant(String baseUrl) {
this.baseUrl = baseUrl;
}
public String getBaseUrl() {
return baseUrl;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -20,7 +20,11 @@
* SOFTWARE.
*/
package com.github.prominence.openweathermap.api.model;
package com.github.prominence.openweathermap.api.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Arrays;
/**
* Enumeration for time of a day representation.
@ -44,9 +48,24 @@ public enum DayTime {
/**
* Returns time of a day value.
*
* @return string value
*/
public String getValue() {
return value;
}
/**
* Finds the appropriate day time based on the short form value.
*
* @param value the short form value (d/n).
* @return day time
*/
@JsonCreator
public static DayTime findByValue(String value) {
return Arrays.stream(values())
.filter(dayTime -> dayTime.getValue().equals(value))
.findFirst()
.orElse(null);
}
}

View File

@ -1,12 +1,51 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Arrays;
public enum EventLevel {
/**
* Unknown event severity.
*/
UNKNOWN(0),
/**
* Green alert.
*/
GREEN(1),
/**
* Yellow alert.
*/
YELLOW(2),
/**
* Orange alert.
*/
ORANGE(3),
/**
* Red alert.
*/
RED(4);
private final int value;
@ -19,7 +58,17 @@ public enum EventLevel {
return value;
}
/**
* Finds the appropriate event level based on the numerical level.
*
* @param value the numerical level.
* @return event level
*/
@JsonCreator
public static EventLevel findByValue(int value) {
return Arrays.stream(values()).filter(eventLevel -> eventLevel.getValue() == value).findFirst().orElse(null);
return Arrays.stream(values())
.filter(eventLevel -> eventLevel.getValue() == value)
.findFirst()
.orElse(null);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -52,8 +52,9 @@ public enum MoonType {
return WANING_GIBBOUS;
} else if (numericValue > 0.75 && numericValue < 1) {
return WANING_CRESCENT;
} else {
return INVALID;
}
return INVALID;
}
private static boolean equals(double d1, double d2) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -23,6 +23,7 @@
package com.github.prominence.openweathermap.api.enums;
public enum ResponseType {
JSON("json"),
HTML("html"),
XML("xml");

View File

@ -1,5 +1,29 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Arrays;
public enum RoadState {
@ -33,6 +57,13 @@ public enum RoadState {
return value;
}
/**
* Finds the appropriate road state based on the numerical code.
*
* @param value the numerical code.
* @return road state
*/
@JsonCreator
public static RoadState findByValue(int value) {
return Arrays.stream(values()).filter(roadState -> roadState.getValue() == value).findFirst().orElse(null);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -47,29 +47,6 @@ public enum UnitSystem {
this.value = value;
}
/**
* Returns wind unit for current unit system.
* @return wind unit.
*/
public String getWindUnit() {
return switch (this) {
case IMPERIAL -> "miles/hour";
case STANDARD, METRIC -> "meter/sec";
};
}
/**
* Returns temperature unit for current unit system.
* @return temperature unit.
*/
public String getTemperatureUnit() {
return switch (this) {
case METRIC -> "°C";
case IMPERIAL -> "°F";
case STANDARD -> "";
};
}
/**
* Returns unit system value.
* @return value unit system.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -22,6 +22,9 @@
package com.github.prominence.openweathermap.api.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Arrays;
import java.util.Optional;
@ -277,7 +280,7 @@ public enum WeatherCondition {
private final String description;
private final String iconId;
private WeatherCondition(int id, String name, String description, String iconId) {
WeatherCondition(int id, String name, String description, String iconId) {
this.id = id;
this.name = name;
this.description = description;
@ -311,13 +314,24 @@ public enum WeatherCondition {
return description;
}
/**
* Gets icon id based on part of day.
*
* @param partOfDay The part of day we need the icon for.
* @return the icon id
*/
public String getIconId(DayTime partOfDay) {
return iconId + partOfDay.getValue();
}
/**
* Gets day icon id.
*
* @return the day icon id
*/
public String getDayIconId() {
return iconId + 'd';
return getIconId(DayTime.DAY);
}
/**
@ -326,35 +340,42 @@ public enum WeatherCondition {
* @return the night icon id
*/
public String getNightIconId() {
return iconId + 'n';
return getIconId(DayTime.NIGHT);
}
/**
* Gets day icon url.
*
* @param secure Determines whether we need to use secure channel (HTTPS) for loading the image.
* @return the day icon url
*/
public String getDayIconUrl() {
return getIconUrl(getDayIconId());
public String getDayIconUrl(boolean secure) {
return getIconUrl(getDayIconId(), secure);
}
/**
* Gets night icon url.
*
* @param secure Determines whether we need to use secure channel (HTTPS) for loading the image.
* @return the night icon url
*/
public String getNightIconUrl() {
return getIconUrl(getNightIconId());
public String getNightIconUrl(boolean secure) {
return getIconUrl(getNightIconId(), secure);
}
/**
* Gets icon url.
*
* @param iconId the icon id
* @param secure Determines whether we need to use secure channel (HTTPS) for loading the image.
* @return the icon url
*/
public static String getIconUrl(String iconId) {
return "https://openweathermap.org/img/w/" + iconId + ".png";
public static String getIconUrl(String iconId, boolean secure) {
String scheme = "http";
if (secure) {
scheme = "https";
}
return scheme + "://openweathermap.org/img/w/" + iconId + ".png";
}
/**
@ -363,8 +384,10 @@ public enum WeatherCondition {
* @param id the id
* @return the by id
*/
public static WeatherCondition getById(int id) {
final Optional<WeatherCondition> optionalWeatherCondition = Arrays.stream(values()).filter(weatherCondition -> weatherCondition.getId() == id).findFirst();
@JsonCreator
public static WeatherCondition getById(@JsonProperty("id") int id) {
final Optional<WeatherCondition> optionalWeatherCondition =
Arrays.stream(values()).filter(weatherCondition -> weatherCondition.getId() == id).findFirst();
return optionalWeatherCondition.orElse(null);
}

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.exception;
public class ApiPayloadParseException extends RuntimeException {
public ApiPayloadParseException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
* Copyright (c) 2021-present Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,61 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.prominence.openweathermap.api.model.WeatherState;
import com.github.prominence.openweathermap.api.utils.JsonDeserializationUtils;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractMapper {
protected final ObjectMapper objectMapper = new ObjectMapper();
protected LocalDateTime parseDateTime(JsonNode dateTimeNode) {
return JsonDeserializationUtils.parseDateTime(dateTimeNode);
}
protected ZoneId parseZoneId(JsonNode zoneIdNode) {
return JsonDeserializationUtils.parseZoneId(zoneIdNode);
}
protected ZoneOffset parseZoneOffset(JsonNode zoneOffsetNode) {
return JsonDeserializationUtils.parseZoneOffset(zoneOffsetNode);
}
protected List<WeatherState> parseWeatherStates(JsonNode weatherArrayNode) throws IOException {
List<WeatherState> weatherStateList = new ArrayList<>();
if (weatherArrayNode != null && weatherArrayNode.isArray()) {
for (JsonNode weatherNode : weatherArrayNode) {
weatherStateList.add(objectMapper.readValue(objectMapper.treeAsTokens(weatherNode), WeatherState.class));
}
}
return weatherStateList;
}
}

View File

@ -1,102 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.prominence.openweathermap.api.enums.AirQualityIndex;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.air.pollution.AirPollutionDetails;
import com.github.prominence.openweathermap.api.model.air.pollution.AirPollutionRecord;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
/**
* Official API response documentation: <a href="https://openweathermap.org/api/air-pollution#fields">https://openweathermap.org/api/air-pollution#fields</a>.
*/
public class AirPollutionResponseMapper {
/**
* Map to air pollution air pollution.
*
* @param json the json
* @return the air pollution
*/
public AirPollutionDetails mapToAirPollution(String json) {
final ObjectMapper objectMapper = new ObjectMapper();
AirPollutionDetails airPollutionDetails;
try {
final JsonNode root = objectMapper.readTree(json);
airPollutionDetails = mapToAirPollution(root);
} catch (JsonProcessingException e) {
throw new RuntimeException("Cannot parse Air Pollution response");
}
return airPollutionDetails;
}
private AirPollutionDetails mapToAirPollution(JsonNode rootNode) {
final AirPollutionDetails airPollutionDetails = new AirPollutionDetails();
airPollutionDetails.setCoordinates(parseCoordinate(rootNode.get("coord")));
final List<AirPollutionRecord> sampleList = new ArrayList<>();
final JsonNode sampleListNode = rootNode.get("list");
sampleListNode.forEach(sampleNode -> {
sampleList.add(parseAirPollutionSample(sampleNode));
});
airPollutionDetails.setAirPollutionRecords(sampleList);
return airPollutionDetails;
}
private AirPollutionRecord parseAirPollutionSample(JsonNode sampleNode) {
AirPollutionRecord airPollutionRecord = new AirPollutionRecord();
airPollutionRecord.setForecastTime(LocalDateTime.ofInstant(Instant.ofEpochSecond(sampleNode.get("dt").asInt()), TimeZone.getDefault().toZoneId()));
airPollutionRecord.setAirQualityIndex(AirQualityIndex.getByIndex(sampleNode.get("main").get("aqi").asInt()));
final JsonNode componentsNode = sampleNode.get("components");
airPollutionRecord.setCO(componentsNode.get("co").asDouble());
airPollutionRecord.setNO(componentsNode.get("no").asDouble());
airPollutionRecord.setNO2(componentsNode.get("no2").asDouble());
airPollutionRecord.setO3(componentsNode.get("o3").asDouble());
airPollutionRecord.setSO2(componentsNode.get("so2").asDouble());
airPollutionRecord.setPM2_5(componentsNode.get("pm2_5").asDouble());
airPollutionRecord.setPM10(componentsNode.get("pm10").asDouble());
airPollutionRecord.setNH3(componentsNode.get("nh3").asDouble());
return airPollutionRecord;
}
private Coordinates parseCoordinate(JsonNode rootNode) {
final JsonNode latitudeNode = rootNode.get("lat");
final JsonNode longitudeNode = rootNode.get("lon");
if (latitudeNode != null && longitudeNode != null) {
return Coordinates.of(latitudeNode.asDouble(), longitudeNode.asDouble());
}
return null;
}
}

View File

@ -1,84 +0,0 @@
package com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.HumidityDeserializer;
import com.github.prominence.openweathermap.api.deserializer.WeatherStateDeserializer;
import com.github.prominence.openweathermap.api.deserializer.forecast.climatic.*;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.Clouds;
import com.github.prominence.openweathermap.api.model.Humidity;
import com.github.prominence.openweathermap.api.model.WeatherState;
import com.github.prominence.openweathermap.api.model.Wind;
import com.github.prominence.openweathermap.api.model.forecast.climatic.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ClimaticForecastResponseMapper extends AbstractMapper {
public ClimaticForecastResponseMapper(UnitSystem unitSystem) {
objectMapper.setInjectableValues(new InjectableValues.Std().addValue("unitSystem", unitSystem != null ? unitSystem : UnitSystem.STANDARD));
final SimpleModule module = new SimpleModule();
module.addDeserializer(WeatherState.class, new WeatherStateDeserializer());
module.addDeserializer(Temperature.class, new ClimaticForecastTemperatureDeserializer());
module.addDeserializer(AtmosphericPressure.class, new ClimaticForecastAtmosphericPressureDeserializer());
module.addDeserializer(Humidity.class, new HumidityDeserializer());
module.addDeserializer(Clouds.class, new ClimaticForecastCloudsDeserializer());
module.addDeserializer(Rain.class, new ClimaticForecastRainDeserializer());
module.addDeserializer(Snow.class, new ClimaticForecastSnowDeserializer());
module.addDeserializer(Wind.class, new ClimaticForecastWindDeserializer());
module.addDeserializer(Location.class, new ClimaticForecastLocationDeserializer());
objectMapper.registerModule(module);
}
public Forecast mapToForecast(String json) {
Forecast forecast;
try {
final JsonNode root = objectMapper.readTree(json);
forecast = mapToForecast(root);
} catch (IOException e) {
throw new RuntimeException("Cannot parse Forecast response", e);
}
return forecast;
}
private Forecast mapToForecast(JsonNode root) throws IOException {
final Forecast forecast = new Forecast();
forecast.setLocation(objectMapper.readValue(objectMapper.treeAsTokens(root.get("city")), Location.class));
final List<WeatherForecast> forecasts = new ArrayList<>();
final JsonNode forecastListNode = root.get("list");
for (JsonNode forecastNode : forecastListNode) {
forecasts.add(parseWeatherForecast(forecastNode));
}
forecast.setWeatherForecasts(forecasts);
return forecast;
}
private WeatherForecast parseWeatherForecast(JsonNode rootNode) throws IOException {
final WeatherForecast weatherForecast = new WeatherForecast();
weatherForecast.setForecastTime(parseDateTime(rootNode.get("dt")));
weatherForecast.setSunriseTime(parseDateTime(rootNode.get("sunrise")));
weatherForecast.setSunsetTime(parseDateTime(rootNode.get("sunset")));
weatherForecast.setWeatherStates(parseWeatherStates(rootNode.get("weather")));
weatherForecast.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Temperature.class));
weatherForecast.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), AtmosphericPressure.class));
weatherForecast.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Humidity.class));
weatherForecast.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Clouds.class));
weatherForecast.setWind(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Wind.class));
weatherForecast.setRain(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Rain.class));
weatherForecast.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Snow.class));
return weatherForecast;
}
}

View File

@ -1,112 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.*;
import com.github.prominence.openweathermap.api.deserializer.weather.WeatherLocationDeserializer;
import com.github.prominence.openweathermap.api.deserializer.weather.WeatherRainDeserializer;
import com.github.prominence.openweathermap.api.deserializer.weather.WeatherSnowDeserializer;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.*;
import com.github.prominence.openweathermap.api.model.weather.Location;
import com.github.prominence.openweathermap.api.model.weather.Rain;
import com.github.prominence.openweathermap.api.model.weather.Snow;
import com.github.prominence.openweathermap.api.model.weather.Weather;
import java.io.IOException;
/**
* Official API response documentation: <a href="https://openweathermap.org/current#current_JSON">https://openweathermap.org/current#current_JSON</a>.
* Ignored internal parameters: "root.cod", "sys.type", "sys.id", "sys.message".
*/
public class CurrentWeatherResponseMapper extends AbstractMapper {
/**
* Instantiates a new Current weather response mapper.
*
* @param unitSystem the unit system
*/
public CurrentWeatherResponseMapper(UnitSystem unitSystem) {
objectMapper.setInjectableValues(new InjectableValues.Std().addValue("unitSystem", unitSystem != null ? unitSystem : UnitSystem.STANDARD));
final SimpleModule module = new SimpleModule();
module.addDeserializer(WeatherState.class, new WeatherStateDeserializer());
module.addDeserializer(Temperature.class, new TemperatureDeserializer());
module.addDeserializer(AtmosphericPressure.class, new AtmosphericPressureDeserializer());
module.addDeserializer(Humidity.class, new HumidityDeserializer());
module.addDeserializer(Wind.class, new WindDeserializer());
module.addDeserializer(Rain.class, new WeatherRainDeserializer());
module.addDeserializer(Snow.class, new WeatherSnowDeserializer());
module.addDeserializer(Clouds.class, new CloudsDeserializer());
module.addDeserializer(Location.class, new WeatherLocationDeserializer());
objectMapper.registerModule(module);
}
/**
* Gets single result.
*
* @param json the json string
* @return the weather object
*/
public Weather mapToWeather(String json) {
Weather weather;
try {
final JsonNode root = objectMapper.readTree(json);
weather = mapToWeather(root);
} catch (IOException e) {
throw new RuntimeException("Cannot parse Weather response", e);
}
return weather;
}
private Weather mapToWeather(JsonNode rootNode) throws IOException {
final JsonNode weatherArrayNode = rootNode.get("weather");
final Weather weather = new Weather();
weather.setWeatherStates(parseWeatherStates(weatherArrayNode));
weather.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("main")), Temperature.class));
weather.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("main")), AtmosphericPressure.class));
weather.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("main")), Humidity.class));
weather.setWind(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("wind")), Wind.class));
if (rootNode.has("rain")) {
weather.setRain(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("rain")), Rain.class));
}
if (rootNode.has("snow")) {
weather.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("snow")), Snow.class));
}
weather.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("clouds")), Clouds.class));
weather.setLocation(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Location.class));
final JsonNode dtNode = rootNode.get("dt");
if (dtNode != null) {
weather.setCalculationTime(parseDateTime(dtNode));
}
weather.setBase(rootNode.get("base").asText());
return weather;
}
}

View File

@ -1,119 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.HumidityDeserializer;
import com.github.prominence.openweathermap.api.deserializer.WeatherStateDeserializer;
import com.github.prominence.openweathermap.api.deserializer.WindDeserializer;
import com.github.prominence.openweathermap.api.deserializer.forecast.daily.*;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.Clouds;
import com.github.prominence.openweathermap.api.model.Humidity;
import com.github.prominence.openweathermap.api.model.WeatherState;
import com.github.prominence.openweathermap.api.model.Wind;
import com.github.prominence.openweathermap.api.model.forecast.daily.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Official API response documentation: <a href="https://openweathermap.org/forecast16#JSON">https://openweathermap.org/forecast16#JSON</a>.
*/
public class DailyForecastResponseMapper extends AbstractMapper {
public DailyForecastResponseMapper(UnitSystem unitSystem) {
objectMapper.setInjectableValues(new InjectableValues.Std().addValue("unitSystem", unitSystem != null ? unitSystem : UnitSystem.STANDARD));
final SimpleModule module = new SimpleModule();
module.addDeserializer(WeatherState.class, new WeatherStateDeserializer());
module.addDeserializer(Temperature.class, new DailyForecastTemperatureDeserializer());
module.addDeserializer(AtmosphericPressure.class, new DailyForecastAtmosphericPressureDeserializer());
module.addDeserializer(Humidity.class, new HumidityDeserializer());
module.addDeserializer(Clouds.class, new DailyForecastCloudsDeserializer());
module.addDeserializer(Rain.class, new DailyForecastRainDeserializer());
module.addDeserializer(Snow.class, new DailyForecastSnowDeserializer());
module.addDeserializer(Wind.class, new WindDeserializer());
module.addDeserializer(Location.class, new DailyForecastLocationDeserializer());
objectMapper.registerModule(module);
}
/**
* Maps forecast response into java object.
*
* @param json the json string
* @return the forecast
*/
public Forecast mapToForecast(String json) {
Forecast forecast;
try {
final JsonNode root = objectMapper.readTree(json);
forecast = mapToForecast(root);
} catch (IOException e) {
throw new RuntimeException("Cannot parse Forecast response", e);
}
return forecast;
}
private Forecast mapToForecast(JsonNode root) throws IOException {
final Forecast forecast = new Forecast();
forecast.setLocation(objectMapper.readValue(objectMapper.treeAsTokens(root.get("city")), Location.class));
final List<WeatherForecast> forecasts = new ArrayList<>(root.get("cnt").asInt());
final JsonNode forecastListNode = root.get("list");
for (JsonNode forecastNode : forecastListNode) {
forecasts.add(parseWeatherForecast(forecastNode));
}
forecast.setWeatherForecasts(forecasts);
return forecast;
}
private WeatherForecast parseWeatherForecast(JsonNode rootNode) throws IOException {
final WeatherForecast weatherForecast = new WeatherForecast();
weatherForecast.setForecastTime(parseDateTime(rootNode.get("dt")));
weatherForecast.setSunriseTime(parseDateTime(rootNode.get("sunrise")));
weatherForecast.setSunsetTime(parseDateTime(rootNode.get("sunset")));
weatherForecast.setWeatherStates(parseWeatherStates(rootNode.get("weather")));
weatherForecast.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Temperature.class));
weatherForecast.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), AtmosphericPressure.class));
weatherForecast.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Humidity.class));
weatherForecast.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Clouds.class));
weatherForecast.setWind(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Wind.class));
weatherForecast.setRain(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Rain.class));
weatherForecast.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Snow.class));
if (rootNode.has("pop")) {
weatherForecast.setProbabilityOfPrecipitation(rootNode.get("pop").asDouble());
}
return weatherForecast;
}
}

View File

@ -1,137 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.*;
import com.github.prominence.openweathermap.api.deserializer.forecast.free.FreeForecastLocationDeserializer;
import com.github.prominence.openweathermap.api.deserializer.forecast.free.FreeForecastRainDeserializer;
import com.github.prominence.openweathermap.api.deserializer.forecast.free.FreeForecastSnowDeserializer;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.*;
import com.github.prominence.openweathermap.api.model.forecast.free.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Official API response documentation: <a href="https://openweathermap.org/forecast5#JSON">https://openweathermap.org/forecast5#JSON</a>.
*/
public class FiveDayThreeHourStepForecastResponseMapper extends AbstractMapper {
/**
* Instantiates a new forecast response mapper.
*
* @param unitSystem the unit system
*/
public FiveDayThreeHourStepForecastResponseMapper(UnitSystem unitSystem) {
objectMapper.setInjectableValues(new InjectableValues.Std().addValue("unitSystem", unitSystem != null ? unitSystem : UnitSystem.STANDARD));
final SimpleModule module = new SimpleModule();
module.addDeserializer(WeatherState.class, new WeatherStateDeserializer());
module.addDeserializer(Temperature.class, new TemperatureDeserializer());
module.addDeserializer(AtmosphericPressure.class, new AtmosphericPressureDeserializer());
module.addDeserializer(Humidity.class, new HumidityDeserializer());
module.addDeserializer(Clouds.class, new CloudsDeserializer());
module.addDeserializer(Wind.class, new WindDeserializer());
module.addDeserializer(Rain.class, new FreeForecastRainDeserializer());
module.addDeserializer(Snow.class, new FreeForecastSnowDeserializer());
module.addDeserializer(Location.class, new FreeForecastLocationDeserializer());
objectMapper.registerModule(module);
}
/**
* Maps forecast response into java object.
*
* @param json the json string
* @return the forecast
*/
public Forecast mapToForecast(String json) {
Forecast forecast;
try {
final JsonNode root = objectMapper.readTree(json);
forecast = mapToForecast(root);
} catch (IOException e) {
throw new RuntimeException("Cannot parse Forecast response", e);
}
return forecast;
}
private Forecast mapToForecast(JsonNode rootNode) throws IOException {
final Forecast forecast = new Forecast();
forecast.setLocation(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("city")), Location.class));
final List<WeatherForecast> forecasts = new ArrayList<>(rootNode.get("cnt").asInt());
final JsonNode forecastListNode = rootNode.get("list");
for (JsonNode forecastNode : forecastListNode) {
forecasts.add(parseWeatherForecast(forecastNode));
}
forecast.setWeatherForecasts(forecasts);
return forecast;
}
private WeatherForecast parseWeatherForecast(JsonNode rootNode) throws IOException {
final WeatherForecast weatherForecast = new WeatherForecast();
final JsonNode weatherArrayNode = rootNode.get("weather");
weatherForecast.setWeatherStates(parseWeatherStates(weatherArrayNode));
final JsonNode mainNode = rootNode.get("main");
weatherForecast.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(mainNode), Temperature.class));
weatherForecast.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(mainNode), AtmosphericPressure.class));
weatherForecast.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(mainNode), Humidity.class));
weatherForecast.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("clouds")), Clouds.class));
weatherForecast.setWind(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("wind")), Wind.class));
if (rootNode.has("rain")) {
weatherForecast.setRain(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("rain")), Rain.class));
}
if (rootNode.has("snow")) {
weatherForecast.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("snow")), Snow.class));
}
final JsonNode sysNode = rootNode.get("sys");
if (sysNode != null) {
weatherForecast.setDayTime("d".equals(sysNode.get("pod").asText()) ? DayTime.DAY : DayTime.NIGHT);
}
final JsonNode visibilityNode = rootNode.get("visibility");
if (visibilityNode != null) {
weatherForecast.setVisibilityInMetres(visibilityNode.asDouble());
}
final JsonNode popNode = rootNode.get("pop");
if (popNode != null) {
weatherForecast.setProbabilityOfPrecipitation(popNode.asDouble());
}
weatherForecast.setForecastTime(parseDateTime(rootNode.get("dt")));
weatherForecast.setForecastTimeISO(rootNode.get("dt_txt").asText());
return weatherForecast;
}
}

View File

@ -1,64 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.GeocodingRecordDeserializer;
import com.github.prominence.openweathermap.api.deserializer.ZipCodeGeocodingDeserializer;
import com.github.prominence.openweathermap.api.model.geocoding.GeocodingRecord;
import com.github.prominence.openweathermap.api.model.geocoding.ZipCodeGeocodingRecord;
import java.util.List;
/**
* Official API response documentation: <a href="https://openweathermap.org/api/geocoding-api">https://openweathermap.org/api/geocoding-api</a>.
*/
public class GeocodingResponseMapper {
private static final ObjectMapper objectMapper = new ObjectMapper();
public GeocodingResponseMapper() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(GeocodingRecord.class, new GeocodingRecordDeserializer());
module.addDeserializer(ZipCodeGeocodingRecord.class, new ZipCodeGeocodingDeserializer());
objectMapper.registerModule(module);
}
public List<GeocodingRecord> mapGeocodingResponse(String json) {
try {
return objectMapper.readValue(json, new TypeReference<List<GeocodingRecord>>() {});
} catch (JsonProcessingException e) {
throw new RuntimeException("Cannot parse ReverseGeocoding response.", e);
}
}
public ZipCodeGeocodingRecord mapZipCodeGeocodingResponse(String json) {
try {
return objectMapper.readValue(json, ZipCodeGeocodingRecord.class);
} catch (JsonProcessingException e) {
throw new RuntimeException("Cannot parse GeocodingInfo response.", e);
}
}
}

View File

@ -1,143 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.*;
import com.github.prominence.openweathermap.api.deserializer.forecast.hourly.HourlyForecastLocationDeserializer;
import com.github.prominence.openweathermap.api.deserializer.forecast.hourly.HourlyForecastRainDeserializer;
import com.github.prominence.openweathermap.api.deserializer.forecast.hourly.HourlyForecastSnowDeserializer;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.*;
import com.github.prominence.openweathermap.api.model.forecast.hourly.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Official API response documentation: <a href="https://openweathermap.org/api/hourly-forecast#JSON">https://openweathermap.org/api/hourly-forecast#JSON</a>.
*/
public class HourlyForecastResponseMapper extends AbstractMapper {
/**
* Instantiates a new forecast response mapper.
*
* @param unitSystem the unit system
*/
public HourlyForecastResponseMapper(UnitSystem unitSystem) {
objectMapper.setInjectableValues(new InjectableValues.Std().addValue("unitSystem", unitSystem != null ? unitSystem : UnitSystem.STANDARD));
final SimpleModule module = new SimpleModule();
module.addDeserializer(WeatherState.class, new WeatherStateDeserializer());
module.addDeserializer(Temperature.class, new TemperatureDeserializer());
module.addDeserializer(AtmosphericPressure.class, new AtmosphericPressureDeserializer());
module.addDeserializer(Wind.class, new WindDeserializer());
module.addDeserializer(Humidity.class, new HumidityDeserializer());
module.addDeserializer(Clouds.class, new CloudsDeserializer());
module.addDeserializer(Rain.class, new HourlyForecastRainDeserializer());
module.addDeserializer(Snow.class, new HourlyForecastSnowDeserializer());
module.addDeserializer(Location.class, new HourlyForecastLocationDeserializer());
objectMapper.registerModule(module);
}
/**
* Maps forecast response into java object.
*
* @param json the json string
* @return the forecast
*/
public HourlyForecast mapToForecast(String json) {
HourlyForecast hourlyForecast;
try {
final JsonNode root = objectMapper.readTree(json);
hourlyForecast = mapToForecast(root);
} catch (IOException e) {
throw new RuntimeException("Cannot parse Forecast response");
}
return hourlyForecast;
}
private HourlyForecast mapToForecast(JsonNode root) throws IOException {
final HourlyForecast hourlyForecast = new HourlyForecast();
hourlyForecast.setLocation(objectMapper.readValue(objectMapper.treeAsTokens(root.get("city")), Location.class));
final List<WeatherForecast> forecasts = new ArrayList<>(root.get("cnt").asInt());
final JsonNode forecastListNode = root.get("list");
for (JsonNode forecastNode : forecastListNode) {
forecasts.add(parseWeatherForecast(forecastNode));
}
hourlyForecast.setWeatherForecasts(forecasts);
return hourlyForecast;
}
private WeatherForecast parseWeatherForecast(JsonNode rootNode) throws IOException {
final WeatherForecast weatherForecast = new WeatherForecast();
final JsonNode weatherArrayNode = rootNode.get("weather");
if (weatherArrayNode != null && weatherArrayNode.isArray()) {
List<WeatherState> weatherStateList = new ArrayList<>();
for (JsonNode weatherNode : weatherArrayNode) {
weatherStateList.add(objectMapper.readValue(objectMapper.treeAsTokens(weatherNode), WeatherState.class));
}
weatherForecast.setWeatherStates(weatherStateList);
}
final JsonNode mainNode = rootNode.get("main");
weatherForecast.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(mainNode), Temperature.class));
weatherForecast.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(mainNode), AtmosphericPressure.class));
weatherForecast.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(mainNode), Humidity.class));
weatherForecast.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("clouds")), Clouds.class));
weatherForecast.setWind(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("wind")), Wind.class));
if (rootNode.has("rain")) {
weatherForecast.setRain(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("rain")), Rain.class));
}
if (rootNode.has("snow")) {
weatherForecast.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("snow")), Snow.class));
}
final JsonNode sysNode = rootNode.get("sys");
if (sysNode != null) {
weatherForecast.setDayTime("d".equals(sysNode.get("pod").asText()) ? DayTime.DAY : DayTime.NIGHT);
}
if (rootNode.has("visibility")) {
weatherForecast.setAverageVisibilityInMetres(rootNode.get("visibility").asInt());
}
if (rootNode.has("pop")) {
weatherForecast.setProbabilityOfPrecipitation(rootNode.get("pop").asDouble());
}
weatherForecast.setForecastTime(parseDateTime(rootNode.get("dt")));
weatherForecast.setForecastTimeISO(rootNode.get("dt_txt").asText());
return weatherForecast;
}
}

View File

@ -1,334 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.*;
import com.github.prominence.openweathermap.api.deserializer.onecall.*;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.model.*;
import com.github.prominence.openweathermap.api.model.onecall.AtmosphericPressure;
import com.github.prominence.openweathermap.api.model.onecall.Temperature;
import com.github.prominence.openweathermap.api.model.onecall.*;
import com.github.prominence.openweathermap.api.model.onecall.current.*;
import com.github.prominence.openweathermap.api.model.onecall.historical.HistoricalWeather;
import com.github.prominence.openweathermap.api.model.onecall.historical.HistoricalWeatherData;
import com.github.prominence.openweathermap.api.model.onecall.historical.HourlyHistorical;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Official API response documentation:
* <ul>
* <li><a href="https://openweathermap.org/api/one-call-api#parameter">https://openweathermap.org/api/one-call-api#parameter</a></li>
* <li><a href="https://openweathermap.org/api/one-call-api#hist_parameter">https://openweathermap.org/api/one-call-api#hist_parameter</a></li>
* </ul>
*/
public class OneCallWeatherResponseMapper extends AbstractMapper {
/**
* Instantiates a new forecast response mapper.
*
* @param unitSystem the unit system
*/
public OneCallWeatherResponseMapper(UnitSystem unitSystem) {
objectMapper.setInjectableValues(new InjectableValues.Std().addValue("unitSystem", unitSystem != null ? unitSystem : UnitSystem.STANDARD));
final SimpleModule module = new SimpleModule();
module.addDeserializer(Coordinates.class, new CoordinatesDeserializer());
module.addDeserializer(AtmosphericPressure.class, new OneCallAtmosphericPressureDeserializer());
module.addDeserializer(Temperature.class, new OneCallTemperatureDeserializer());
module.addDeserializer(WeatherState.class, new WeatherStateDeserializer());
module.addDeserializer(Humidity.class, new HumidityDeserializer());
module.addDeserializer(Wind.class, new WindDeserializer());
module.addDeserializer(Clouds.class, new CloudsDeserializer());
module.addDeserializer(Rain.class, new OneCallRainDeserializer());
module.addDeserializer(Snow.class, new OneCallSnowDeserializer());
module.addDeserializer(DailyTemperature.class, new OneCallDailyTemperatureDeserializer());
module.addDeserializer(DailyRain.class, new OneCallDailyRainDeserializer());
module.addDeserializer(DailySnow.class, new OneCallDailySnowDeserializer());
module.addDeserializer(Alert.class, new AlertDeserializer());
objectMapper.registerModule(module);
}
/**
* Maps current weather data response into java object.
*
* @param json the json string
* @return the current data object
*/
public CurrentWeatherData mapToCurrent(String json) {
CurrentWeatherData currentData;
try {
final JsonNode root = objectMapper.readTree(json);
currentData = mapToCurrent(root);
} catch (IOException e) {
throw new RuntimeException("Cannot parse OneCall response", e);
}
return currentData;
}
/**
* Maps current weather data response into java object.
*
* @param json the json string
* @return the current data object
*/
public HistoricalWeatherData mapToHistorical(String json) {
HistoricalWeatherData historicalData;
try {
final JsonNode root = objectMapper.readTree(json);
historicalData = mapToHistorical(root);
} catch (IOException e) {
throw new RuntimeException("Cannot parse OneCall response");
}
return historicalData;
}
private CurrentWeatherData mapToCurrent(JsonNode rootNode) throws IOException {
final CurrentWeatherData currentData = new CurrentWeatherData();
currentData.setCoordinates(objectMapper.readValue(objectMapper.treeAsTokens(rootNode), Coordinates.class));
currentData.setTimezone(parseZoneId(rootNode.get("timezone")));
currentData.setTimezoneOffset(parseZoneOffset(rootNode.get("timezone_offset")));
currentData.setCurrent(parseCurrent(rootNode.get("current")));
currentData.setMinutelyList(parseMinutelyList(rootNode.get("minutely")));
currentData.setHourlyList(parseHourlyList(rootNode.get("hourly")));
currentData.setDailyList(parseDailyList(rootNode.get("daily")));
currentData.setAlerts(parseAlerts(rootNode.get("alerts")));
return currentData;
}
private Current parseCurrent(JsonNode currentNode) throws IOException {
if (currentNode == null) {
return null;
}
final Current current = new Current();
current.setForecastTime(parseDateTime(currentNode.get("dt")));
current.setSunriseTime(parseDateTime(currentNode.get("sunrise")));
current.setSunsetTime(parseDateTime(currentNode.get("sunset")));
current.setWeatherStates(parseWeatherStates(currentNode.get("weather")));
current.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Temperature.class));
current.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), AtmosphericPressure.class));
current.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Humidity.class));
current.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Clouds.class));
current.setUvIndex(currentNode.get("uvi").asDouble());
final JsonNode visibilityNode = currentNode.get("visibility");
if (visibilityNode != null) {
current.setVisibilityInMetres(visibilityNode.asDouble());
}
current.setWind(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Wind.class));
current.setRain(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Rain.class));
current.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Snow.class));
return current;
}
private List<Minutely> parseMinutelyList(JsonNode minutelyListNode) {
if (minutelyListNode == null) {
return null;
}
final List<Minutely> minutelyList = new ArrayList<>();
for (final JsonNode minutelyNode : minutelyListNode) {
minutelyList.add(Minutely.withValue(
parseDateTime(minutelyNode.get("dt")),
minutelyNode.get("precipitation").asDouble()
));
}
return minutelyList;
}
private List<Hourly> parseHourlyList(JsonNode hourlyListNode) throws IOException {
if (hourlyListNode == null) {
return null;
}
final List<Hourly> hourlyList = new ArrayList<>();
for (final JsonNode hourlyNode : hourlyListNode) {
final Hourly hourly = new Hourly();
hourly.setForecastTime(parseDateTime(hourlyNode.get("dt")));
hourly.setWeatherStates(parseWeatherStates(hourlyNode.get("weather")));
hourly.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Temperature.class));
hourly.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), AtmosphericPressure.class));
hourly.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Humidity.class));
hourly.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Clouds.class));
final JsonNode uviNode = hourlyNode.get("uvi");
if (uviNode != null) {
hourly.setUvIndex(uviNode.asDouble());
}
final JsonNode visibilityNode = hourlyNode.get("visibility");
if (visibilityNode != null) {
hourly.setVisibilityInMetres(visibilityNode.asDouble());
}
hourly.setWind(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Wind.class));
final JsonNode popNode = hourlyNode.get("pop");
if (popNode != null) {
hourly.setProbabilityOfPrecipitation(popNode.asDouble());
}
hourly.setRain(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Rain.class));
hourly.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Snow.class));
hourlyList.add(hourly);
}
return hourlyList;
}
private List<Daily> parseDailyList(JsonNode dailyListNode) throws IOException {
if (dailyListNode == null) {
return null;
}
final List<Daily> dailyList = new ArrayList<>();
for (final JsonNode dailyNode : dailyListNode) {
final Daily daily = new Daily();
daily.setForecastTime(parseDateTime(dailyNode.get("dt")));
daily.setSunriseTime(parseDateTime(dailyNode.get("sunrise")));
daily.setSunsetTime(parseDateTime(dailyNode.get("sunset")));
final JsonNode moonriseTimeNode = dailyNode.get("moonrise");
if (moonriseTimeNode != null) {
daily.setMoonriseTime(parseDateTime(moonriseTimeNode));
}
final JsonNode moonsetTimeNode = dailyNode.get("moonset");
if (moonsetTimeNode != null) {
daily.setMoonsetTime(parseDateTime(moonsetTimeNode));
}
final JsonNode moonPhaseNode = dailyNode.get("moon_phase");
if (moonPhaseNode != null) {
daily.setMoonPhase(new MoonPhase(moonPhaseNode.asDouble()));
}
daily.setWeatherStates(parseWeatherStates(dailyNode.get("weather")));
daily.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(dailyNode), DailyTemperature.class));
daily.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(dailyNode), AtmosphericPressure.class));
daily.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(dailyNode), Humidity.class));
daily.setWind(objectMapper.readValue(objectMapper.treeAsTokens(dailyNode), Wind.class));
daily.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(dailyNode), Clouds.class));
daily.setUvIndex(dailyNode.get("uvi").asDouble());
daily.setProbabilityOfPrecipitation(dailyNode.get("pop").asDouble());
daily.setRain(objectMapper.readValue(objectMapper.treeAsTokens(dailyNode), DailyRain.class));
daily.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(dailyNode), DailySnow.class));
dailyList.add(daily);
}
return dailyList;
}
private List<Alert> parseAlerts(JsonNode alertsNode) throws IOException {
if (alertsNode == null || !alertsNode.isArray()) {
return null;
}
final List<Alert> alerts = new ArrayList<>();
for (final JsonNode alertNode : alertsNode) {
alerts.add(objectMapper.readValue(objectMapper.treeAsTokens(alertNode), Alert.class));
}
return alerts;
}
private HistoricalWeatherData mapToHistorical(JsonNode rootNode) throws IOException {
final HistoricalWeatherData historicalData = new HistoricalWeatherData();
historicalData.setCoordinates(Coordinates.of(rootNode.get("lat").asDouble(), rootNode.get("lon").asDouble()));
historicalData.setTimezone(parseZoneId(rootNode.get("timezone")));
historicalData.setTimezoneOffset(parseZoneOffset(rootNode.get("timezone_offset")));
historicalData.setHistoricalWeather(parseHistoricalWeather(rootNode.get("current")));
historicalData.setHourlyList(parseHourlyHistoricalList(rootNode.get("hourly")));
return historicalData;
}
private HistoricalWeather parseHistoricalWeather(JsonNode currentNode) throws IOException {
if (currentNode == null) {
return null;
}
final HistoricalWeather historicalWeather = new HistoricalWeather();
historicalWeather.setForecastTime(parseDateTime(currentNode.get("dt")));
historicalWeather.setSunriseTime(parseDateTime(currentNode.get("sunrise")));
historicalWeather.setSunsetTime(parseDateTime(currentNode.get("sunset")));
historicalWeather.setWeatherStates(parseWeatherStates(currentNode.get("weather")));
historicalWeather.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Temperature.class));
historicalWeather.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), AtmosphericPressure.class));
historicalWeather.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Humidity.class));
historicalWeather.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Clouds.class));
final JsonNode uviNode = currentNode.get("uvi");
if (uviNode != null) {
historicalWeather.setUvIndex(uviNode.asDouble());
}
final JsonNode visibilityNode = currentNode.get("visibility");
if (visibilityNode != null) {
historicalWeather.setVisibilityInMetres(visibilityNode.asDouble());
}
historicalWeather.setWind(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Wind.class));
historicalWeather.setRain(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Rain.class));
historicalWeather.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(currentNode), Snow.class));
return historicalWeather;
}
private List<HourlyHistorical> parseHourlyHistoricalList(JsonNode hourlyListNode) throws IOException {
if (hourlyListNode == null) {
return null;
}
final List<HourlyHistorical> hourlyList = new ArrayList<>();
for (final JsonNode hourlyNode : hourlyListNode) {
final HourlyHistorical hourly = new HourlyHistorical();
hourly.setForecastTime(parseDateTime(hourlyNode.get("dt")));
hourly.setWeatherStates(parseWeatherStates(hourlyNode.get("weather")));
hourly.setTemperature(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Temperature.class));
hourly.setAtmosphericPressure(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), AtmosphericPressure.class));
hourly.setHumidity(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Humidity.class));
hourly.setClouds(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Clouds.class));
final JsonNode visibilityNode = hourlyNode.get("visibility");
if (visibilityNode != null) {
hourly.setVisibilityInMetres(visibilityNode.asDouble());
}
hourly.setWind(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Wind.class));
hourly.setRain(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Rain.class));
hourly.setSnow(objectMapper.readValue(objectMapper.treeAsTokens(hourlyNode), Snow.class));
hourlyList.add(hourly);
}
return hourlyList;
}
}

View File

@ -1,72 +0,0 @@
package com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.roadrisk.RoadRiskAlertDeserializer;
import com.github.prominence.openweathermap.api.deserializer.roadrisk.RoadRiskRoadDetailsDeserializer;
import com.github.prominence.openweathermap.api.deserializer.roadrisk.RoadRiskWeatherDeserializer;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.roadrisk.Alert;
import com.github.prominence.openweathermap.api.model.roadrisk.RoadDetails;
import com.github.prominence.openweathermap.api.model.roadrisk.RoadRiskRecord;
import com.github.prominence.openweathermap.api.model.roadrisk.Weather;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class RoadRiskResponseMapper extends AbstractMapper {
public RoadRiskResponseMapper() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(Weather.class, new RoadRiskWeatherDeserializer());
module.addDeserializer(RoadDetails.class, new RoadRiskRoadDetailsDeserializer());
module.addDeserializer(Alert.class, new RoadRiskAlertDeserializer());
objectMapper.registerModule(module);
}
public List<RoadRiskRecord> mapToObjects(String jsonResponse) {
List<RoadRiskRecord> roadRiskRecords;
try {
final JsonNode root = objectMapper.readTree(jsonResponse);
roadRiskRecords = mapToObjects(root);
} catch (IOException e) {
throw new RuntimeException("Cannot parse SolarRadiation response", e);
}
return roadRiskRecords;
}
private List<RoadRiskRecord> mapToObjects(JsonNode rootNode) throws IOException {
List<RoadRiskRecord> roadRiskRecords = new ArrayList<>();
if (rootNode.isArray()) {
for (JsonNode recordNode : rootNode) {
RoadRiskRecord roadRiskRecord = new RoadRiskRecord();
roadRiskRecord.setForecastTime(parseDateTime(recordNode.get("dt")));
final JsonNode coordNode = recordNode.get("coord");
roadRiskRecord.setCoordinates(Coordinates.of(coordNode.get(0).asDouble(), coordNode.get(1).asDouble()));
roadRiskRecord.setWeather(objectMapper.readValue(objectMapper.treeAsTokens(recordNode.get("weather")), Weather.class));
if (recordNode.has("road")) {
roadRiskRecord.setRoadDetails(objectMapper.readValue(objectMapper.treeAsTokens(recordNode.get("road")), RoadDetails.class));
}
final JsonNode alertsNode = recordNode.get("alerts");
if (alertsNode != null && alertsNode.isArray()) {
List<Alert> alerts = new ArrayList<>();
for (JsonNode alertNode : alertsNode) {
alerts.add(objectMapper.readValue(objectMapper.treeAsTokens(alertNode), Alert.class));
}
roadRiskRecord.setAlerts(alerts);
}
roadRiskRecords.add(roadRiskRecord);
}
}
return roadRiskRecords;
}
}

View File

@ -1,49 +0,0 @@
package com.github.prominence.openweathermap.api.mapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.prominence.openweathermap.api.deserializer.CoordinatesDeserializer;
import com.github.prominence.openweathermap.api.deserializer.radiation.SolarRadiationRecordDeserializer;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.radiation.SolarRadiation;
import com.github.prominence.openweathermap.api.model.radiation.SolarRadiationRecord;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SolarRadiationResponseMapper extends AbstractMapper {
public SolarRadiationResponseMapper() {
final SimpleModule module = new SimpleModule();
module.addDeserializer(SolarRadiationRecord.class, new SolarRadiationRecordDeserializer());
module.addDeserializer(Coordinates.class, new CoordinatesDeserializer());
objectMapper.registerModule(module);
}
public SolarRadiation mapToObject(String jsonResponse) {
SolarRadiation solarRadiation;
try {
final JsonNode root = objectMapper.readTree(jsonResponse);
solarRadiation = mapToObject(root);
} catch (IOException e) {
throw new RuntimeException("Cannot parse SolarRadiation response", e);
}
return solarRadiation;
}
private SolarRadiation mapToObject(JsonNode rootNode) throws IOException {
final SolarRadiation solarRadiation = new SolarRadiation();
solarRadiation.setCoordinates(objectMapper.readValue(objectMapper.treeAsTokens(rootNode.get("coord")), Coordinates.class));
final JsonNode listRecordsNode = rootNode.get("list");
List<SolarRadiationRecord> radiationRecords = new ArrayList<>();
for (JsonNode recordNode : listRecordsNode) {
radiationRecords.add(objectMapper.readValue(objectMapper.treeAsTokens(recordNode), SolarRadiationRecord.class));
}
solarRadiation.setSolarRadiationRecords(radiationRecords);
return solarRadiation;
}
}

View File

@ -1,156 +0,0 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model;
import java.util.Objects;
/**
* The AtmosphericPressure type represents atmospheric pressure value.
* Its value can only be a double in [0, +) range.
*/
public class AtmosphericPressure {
private static final String DEFAULT_UNIT = "hPa";
private double value;
private Double seaLevelValue;
private Double groundLevelValue;
/**
* Instantiates a new Pressure.
*
* @param value the value representing pressure value.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
*/
private AtmosphericPressure(double value) {
this.value = value;
}
/**
* Static method for {@link AtmosphericPressure} creation with value checking.
*
* @param value atmospheric pressure value.
* @return instantiated {@link AtmosphericPressure} object.
*/
public static AtmosphericPressure withValue(double value) {
if (value < 0) {
throw new IllegalArgumentException("Atmospheric pressure value must be in [0, +∞) range.");
}
return new AtmosphericPressure(value);
}
/**
* Returns pressure value.
*
* @return pressure value.
*/
public double getValue() {
return value;
}
/**
* Sets pressure value.
*
* @param value new pressure value.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
*/
public void setValue(double value) {
if (value < 0) {
throw new IllegalArgumentException("Atmospheric pressure value must be in [0, +∞) range.");
}
this.value = value;
}
/**
* Gets sea level value.
*
* @return the sea level value.
*/
public Double getSeaLevelValue() {
return seaLevelValue;
}
/**
* Sets sea level value.
*
* @param seaLevelValue the sea level value.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
*/
public void setSeaLevelValue(double seaLevelValue) {
if (seaLevelValue < 0) {
throw new IllegalArgumentException("Atmospheric pressure value must be in [0, +∞) range.");
}
this.seaLevelValue = seaLevelValue;
}
/**
* Gets ground level value.
*
* @return the ground level value.
*/
public Double getGroundLevelValue() {
return groundLevelValue;
}
/**
* Sets ground level value.
*
* @param groundLevelValue the ground level value.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
*/
public void setGroundLevelValue(double groundLevelValue) {
if (groundLevelValue < 0) {
throw new IllegalArgumentException("Atmospheric pressure value must be in [0, +∞) range.");
}
this.groundLevelValue = groundLevelValue;
}
/**
* Returns pressure unitSystem. Constantly equals to 'hPa'.
*
* @return the pressure unitSystem.
*/
public String getUnit() {
return DEFAULT_UNIT;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AtmosphericPressure)) return false;
AtmosphericPressure atmosphericPressure = (AtmosphericPressure) o;
return Double.compare(atmosphericPressure.value, value) == 0 &&
Objects.equals(seaLevelValue, atmosphericPressure.seaLevelValue) &&
Objects.equals(groundLevelValue, atmosphericPressure.groundLevelValue);
}
@Override
public int hashCode() {
return Objects.hash(value, seaLevelValue, groundLevelValue);
}
@Override
public String toString() {
return "Pressure: " + value + ' ' + getUnit();
}
}

View File

@ -1,107 +0,0 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model;
import java.util.Objects;
/**
* The Clouds type represents cloudiness value percentage.
* Its value can only be an integer in [0, 100] range.
*/
public class Clouds {
private static final String DEFAULT_UNIT = "%";
private byte value;
/**
* Instantiates a new Clouds.
*
* @param value the value representing cloudiness percentage.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
*/
private Clouds(byte value) {
this.value = value;
}
/**
* Static method for {@link Clouds} creation with value checking.
*
* @param value clouds percentage value.
* @return instantiated {@link Clouds} object.
*/
public static Clouds withValue(byte value) {
if (value < 0 || value > 100) {
throw new IllegalArgumentException("Cloudiness value must be in [0, 100] range.");
}
return new Clouds(value);
}
/**
* Returns cloudiness percentage value.
*
* @return cloudiness percentage.
*/
public byte getValue() {
return value;
}
/**
* Sets cloudiness percentage value.
*
* @param value new cloudiness value.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
*/
public void setValue(byte value) {
if (value < 0 || value > 100) {
throw new IllegalArgumentException("Cloudiness value must be in [0, 100] range.");
}
this.value = value;
}
/**
* Returns cloudiness unitSystem. Constantly equals to '%'.
*
* @return the cloudiness unitSystem.
*/
public String getUnit() {
return DEFAULT_UNIT;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Clouds)) return false;
Clouds clouds = (Clouds) o;
return value == clouds.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
return "Clouds: " + value + getUnit();
}
}

View File

@ -1,126 +0,0 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;
/**
* Represents some location by its latitude and longitude.
*/
public class Coordinates {
@JsonProperty("lat")
private double latitude;
@JsonProperty("lon")
private double longitude;
private Coordinates() {
}
/**
* Method for {@link Coordinates} creation with correctness check.
*
* @param latitude latitude
* @param longitude longitude
* @return coordinate object.
*/
public static Coordinates of(double latitude, double longitude) {
final Coordinates coordinates = new Coordinates();
coordinates.setLatitude(latitude);
coordinates.setLongitude(longitude);
return coordinates;
}
/**
* Sets latitude with checks.
*
* @param latitude latitude value
*/
public void setLatitude(double latitude) {
if (latitude < -90 || latitude > 90) {
throw new IllegalArgumentException("Latitude value must be in the next range: [-90.0; 90.0].");
}
this.latitude = latitude;
}
/**
* Sets longitude with checks.
*
* @param longitude longitude value
*/
public void setLongitude(double longitude) {
if (longitude < -180 || longitude > 180) {
throw new IllegalArgumentException("Longitude value must be in the next range: [-180.0; 180.0].");
}
this.longitude = longitude;
}
/**
* Returns latitude.
*
* @return latitude
*/
public double getLatitude() {
return latitude;
}
/**
* Returns longitude.
*
* @return longitude
*/
public double getLongitude() {
return longitude;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Coordinates)) return false;
Coordinates that = (Coordinates) o;
return Double.compare(that.latitude, latitude) == 0 &&
Double.compare(that.longitude, longitude) == 0;
}
@Override
public int hashCode() {
return Objects.hash(latitude, longitude);
}
@Override
public String toString() {
return formatAsDegree(latitude) +
", " + formatAsDegree(longitude);
}
private String formatAsDegree(double value) {
int degrees = (int) value;
double secondsDouble = value % 1 * 60;
int minutes = (int) secondsDouble;
int seconds = (int) (secondsDouble % 1 * 60);
return String.format("%s° %s %s″", degrees, minutes, seconds);
}
}

View File

@ -1,107 +0,0 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model;
import java.util.Objects;
/**
* The Humidity type represents humidity value percentage.
* Its value can only be an integer in [0, 100] range.
*/
public class Humidity {
private static final String DEFAULT_UNIT = "%";
private int value;
/**
* Instantiates a new Humidity.
*
* @param value the value representing humidity percentage.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
*/
private Humidity(byte value) {
this.value = value;
}
/**
* Creates {@link Humidity} object with correctness check.
*
* @param value humidity
* @return created {@link Humidity} object
*/
public static Humidity withValue(byte value) {
if (value < 0 || value > 100) {
throw new IllegalArgumentException("Humidity value must be in [0, 100] range.");
}
return new Humidity(value);
}
/**
* Returns humidity percentage value.
*
* @return humidity percentage.
*/
public int getValue() {
return value;
}
/**
* Sets humidity percentage value.
*
* @param value new humidity value.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
*/
public void setValue(int value) {
if (value < 0 || value > 100) {
throw new IllegalArgumentException("Humidity value must be in [0, 100] range.");
}
this.value = value;
}
/**
* Returns humidity unitSystem. Constantly equals to '%'.
*
* @return the humidity unitSystem.
*/
public String getUnit() {
return DEFAULT_UNIT;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Humidity)) return false;
Humidity humidity = (Humidity) o;
return value == humidity.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
return "Humidity: " + value + getUnit();
}
}

View File

@ -1,193 +0,0 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model;
import java.util.Objects;
/**
* Represents temperature values and unit.
*/
public class Temperature {
private double value;
private Double maxTemperature;
private Double minTemperature;
private Double feelsLike;
private String unit;
private Temperature(double value, String unit) {
this.value = value;
this.unit = unit;
}
/**
* Creates {@link Temperature} object with correctness check.
*
* @param value temperature value
* @param unit temperature unit
* @return temperature object
*/
public static Temperature withValue(double value, String unit) {
if (unit == null) {
throw new IllegalArgumentException("Unit must be set.");
}
return new Temperature(value, unit);
}
/**
* Returns temperature value.
*
* @return value
*/
public double getValue() {
return value;
}
/**
* Sets temperature value.
*
* @param value temperature
*/
public void setValue(double value) {
this.value = value;
}
/**
* Returns maximal temperature value.
*
* @return maximal temperature value
*/
public Double getMaxTemperature() {
return maxTemperature;
}
/**
* Sets maximal temperature value.
*
* @param maxTemperature maximal temperature
*/
public void setMaxTemperature(Double maxTemperature) {
this.maxTemperature = maxTemperature;
}
/**
* Returns minimal temperature value.
*
* @return minimal temperature value
*/
public Double getMinTemperature() {
return minTemperature;
}
/**
* Sets minimal temperature value.
*
* @param minTemperature minimal temperature
*/
public void setMinTemperature(Double minTemperature) {
this.minTemperature = minTemperature;
}
/**
* Returns 'feels like' temperature value.
*
* @return 'feels like' temperature value
*/
public Double getFeelsLike() {
return feelsLike;
}
/**
* Sets 'feels like' temperature value.
*
* @param feelsLike 'feels like' temperature
*/
public void setFeelsLike(Double feelsLike) {
this.feelsLike = feelsLike;
}
/**
* Returns temperature unit.
*
* @return unit
*/
public String getUnit() {
return unit;
}
/**
* Sets temperature unit with correctness check.
*
* @param unit temperature unit
*/
public void setUnit(String unit) {
if (unit == null) {
throw new IllegalArgumentException("Unit must be set.");
}
this.unit = unit;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Temperature that)) return false;
return Double.compare(that.value, value) == 0 &&
Objects.equals(maxTemperature, that.maxTemperature) &&
Objects.equals(minTemperature, that.minTemperature) &&
Objects.equals(feelsLike, that.feelsLike) &&
Objects.equals(unit, that.unit);
}
@Override
public int hashCode() {
return Objects.hash(value, maxTemperature, minTemperature, feelsLike, unit);
}
@Override
public String toString() {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Temperature: ");
stringBuilder.append(value);
stringBuilder.append(' ');
stringBuilder.append(unit);
if (maxTemperature != null) {
stringBuilder.append(", Maximum value: ");
stringBuilder.append(maxTemperature);
stringBuilder.append(' ');
stringBuilder.append(unit);
}
if (minTemperature != null) {
stringBuilder.append(", Minimum value: ");
stringBuilder.append(minTemperature);
stringBuilder.append(' ');
stringBuilder.append(unit);
}
if (feelsLike != null) {
stringBuilder.append(", Feels like: ");
stringBuilder.append(feelsLike);
stringBuilder.append(' ');
stringBuilder.append(unit);
}
return stringBuilder.toString();
}
}

View File

@ -1,136 +0,0 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model;
import com.github.prominence.openweathermap.api.enums.WeatherCondition;
import java.util.Objects;
/**
* The type Weather state.
*/
public class WeatherState {
private final int id;
private final String name;
private final String description;
private String iconId;
private final WeatherCondition weatherConditionEnum;
public WeatherState(Integer id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
this.weatherConditionEnum = WeatherCondition.getById(id);
}
/**
* Gets id.
*
* @return the id
*/
public int getId() {
return id;
}
/**
* Gets name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Gets description.
*
* @return the description
*/
public String getDescription() {
return description;
}
/**
* Gets icon id.
*
* @return the icon id
*/
public String getIconId() {
return iconId;
}
/**
* Sets icon id.
*
* @param iconId the icon id
*/
public void setIconId(String iconId) {
this.iconId = iconId;
}
/**
* Gets weather condition enum.
*
* @return the weather condition enum
*/
public WeatherCondition getWeatherConditionEnum() {
return weatherConditionEnum;
}
/**
* Gets weather icon url.
*
* @return the weather icon url
*/
public String getWeatherIconUrl() {
if (iconId != null) {
return WeatherCondition.getIconUrl(iconId);
}
if (weatherConditionEnum != null) {
// return the default one for the current weather condition
return weatherConditionEnum.getDayIconUrl();
}
return null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WeatherState that = (WeatherState) o;
return Objects.equals(id, that.id) &&
Objects.equals(name, that.name) &&
Objects.equals(description, that.description) &&
Objects.equals(iconId, that.iconId);
}
@Override
public int hashCode() {
return Objects.hash(id, name, description, iconId, weatherConditionEnum);
}
@Override
public String toString() {
return "Weather state: " + name + "(" + description + ").";
}
}

View File

@ -1,173 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model;
import java.util.Objects;
/**
* The type Wind.
*/
public class Wind {
private double speed;
private Double degrees;
private Double gust;
private String unit;
/**
* Instantiates a new Wind.
*
* @param speed the speed
* @param unit the unitSystem
*/
private Wind(double speed, String unit) {
this.speed = speed;
this.unit = unit;
}
/**
* Creates {@link Wind} object with correctness check.
*
* @param speed the speed
* @param unit the unitSystem
* @return wind object
*/
public static Wind withValue(double speed, String unit) {
if (speed < 0) {
throw new IllegalArgumentException("Wind speed value must be in positive or zero.");
}
if (unit == null) {
throw new IllegalArgumentException("Unit must be set.");
}
return new Wind(speed, unit);
}
/**
* Gets speed.
*
* @return the speed
*/
public double getSpeed() {
return speed;
}
/**
* Sets speed.
*
* @param speed the speed
*/
public void setSpeed(double speed) {
if (speed < 0) {
throw new IllegalArgumentException("Wind speed value must be in positive or zero.");
}
this.speed = speed;
}
/**
* Gets gust value.
*
* @return the gust
*/
public Double getGust() {
return gust;
}
/**
* Sets gust value.
*
* @param gust the gust.
*/
public void setGust(double gust) {
if (gust < 0) {
throw new IllegalArgumentException("Gust value must be positive or zero.");
}
this.gust = gust;
}
/**
* Gets degrees.
*
* @return the degrees
*/
public Double getDegrees() {
return degrees;
}
/**
* Sets degrees.
*
* @param degrees the degrees
*/
public void setDegrees(double degrees) {
if (degrees < 0 || degrees > 360) {
throw new IllegalArgumentException("Wind direction value must be in [0, 360] range.");
}
this.degrees = degrees;
}
/**
* Gets unitSystem.
*
* @return the unitSystem
*/
public String getUnit() {
return unit;
}
/**
* Sets unitSystem.
*
* @param unit the unitSystem
*/
public void setUnit(String unit) {
if (unit == null) {
throw new IllegalArgumentException("Unit must be set.");
}
this.unit = unit;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Wind)) return false;
Wind wind = (Wind) o;
return Double.compare(wind.speed, speed) == 0 &&
Objects.equals(degrees, wind.degrees) &&
Objects.equals(gust, wind.gust) &&
Objects.equals(unit, wind.unit);
}
@Override
public int hashCode() {
return Objects.hash(speed, degrees, gust, unit);
}
@Override
public String toString() {
String output = "Wind speed: " + speed + " " + unit +
", degrees: " + degrees;
if (gust != null) {
output += ", Gust: " + gust + " " + unit;
}
return output;
}
}

View File

@ -0,0 +1,102 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model.air.pollution;
import com.github.prominence.openweathermap.api.enums.AirQualityIndex;
import java.time.OffsetDateTime;
/**
* Interface of air pollution concentration measurements.
*/
public interface AirPollutionConcentration {
/**
* The date time when the measurement happened.
*
* @return datetime
*/
OffsetDateTime getMeasurementTime();
/**
* The air quality index as an (overview).
*
* @return index
*/
AirQualityIndex getAirQualityIndex();
/**
* The concentration of CO in the air.
*
* @return CO
*/
Concentration getCarbonMonoxide();
/**
* The concentration of NO in the air.
*
* @return NO
*/
Concentration getNitrogenMonoxide();
/**
* The concentration of NO2 in the air.
*
* @return NO2
*/
Concentration getNitrogenDioxide();
/**
* The concentration of O3 in the air.
*
* @return O3
*/
Concentration getOzone();
/**
* The concentration of SO2 in the air.
*
* @return SO2
*/
Concentration getSulphurDioxide();
/**
* The concentration of pine particles matter in the air.
*
* @return fine particles
*/
Concentration getFineParticlesMatter();
/**
* The concentration of coarse particulate matter in the air.
*
* @return coarse particles
*/
Concentration getCoarseParticulateMatter();
/**
* The concentration of NH3 in the air.
*
* @return NH3
*/
Concentration getAmmonia();
}

View File

@ -1,87 +1,39 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* * Copyright (c) 2021 Alexey Zinchenko
* *
* * 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.
* 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 com.github.prominence.openweathermap.api.model.air.pollution;
import com.github.prominence.openweathermap.api.model.Coordinates;
import com.github.prominence.openweathermap.api.model.generic.location.CoordinateAware;
import java.util.List;
import java.util.Objects;
/**
* The type Air pollution.
* Interface of air pollution overview.
*/
public class AirPollutionDetails {
private Coordinates coordinates;
private List<AirPollutionRecord> airPollutionRecords;
public interface AirPollutionDetails extends CoordinateAware {
/**
* Gets coordinate.
*
* @return the coordinate
* The pollution details.
* @return pollution
*/
public Coordinates getCoordinates() {
return coordinates;
}
/**
* Sets coordinate.
*
* @param coordinates the coordinate
*/
public void setCoordinates(Coordinates coordinates) {
this.coordinates = coordinates;
}
/**
* Gets air pollution details.
*
* @return the air pollution details
*/
public List<AirPollutionRecord> getAirPollutionRecords() {
return airPollutionRecords;
}
/**
* Sets air pollution details.
*
* @param airPollutionRecords the air pollution details
*/
public void setAirPollutionRecords(List<AirPollutionRecord> airPollutionRecords) {
this.airPollutionRecords = airPollutionRecords;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AirPollutionDetails that = (AirPollutionDetails) o;
return Objects.equals(coordinates, that.coordinates) && Objects.equals(airPollutionRecords, that.airPollutionRecords);
}
@Override
public int hashCode() {
return Objects.hash(coordinates, airPollutionRecords);
}
List<AirPollutionConcentration> getAirPollutionConcentration();
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model.air.pollution;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.prominence.openweathermap.api.model.generic.location.Coordinates;
import lombok.Data;
import java.util.List;
import java.util.stream.Collectors;
/**
* The type representing Air pollution data.
*/
@Data
public class AirPollutionDetailsModel implements AirPollutionDetails {
@JsonProperty("coord")
private Coordinates coordinates;
@JsonProperty("list")
private List<AirPollutionRecord> airPollutionRecords;
@JsonIgnore
@Override
public List<AirPollutionConcentration> getAirPollutionConcentration() {
return airPollutionRecords.stream()
.map(AirPollutionConcentration.class::cast)
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model.air.pollution;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.github.prominence.openweathermap.api.deserializer.ConcentrationDeserializer;
import lombok.Data;
/**
* The type Air pollution record.
*/
@Data
public class AirPollutionMeasurements {
@JsonDeserialize(using = ConcentrationDeserializer.class)
@JsonProperty("co")
private Concentration carbonMonoxide;
@JsonDeserialize(using = ConcentrationDeserializer.class)
@JsonProperty("no")
private Concentration nitrogenMonoxide;
@JsonDeserialize(using = ConcentrationDeserializer.class)
@JsonProperty("no2")
private Concentration nitrogenDioxide;
@JsonDeserialize(using = ConcentrationDeserializer.class)
@JsonProperty("o3")
private Concentration ozone;
@JsonDeserialize(using = ConcentrationDeserializer.class)
@JsonProperty("so2")
private Concentration sulphurDioxide;
@JsonDeserialize(using = ConcentrationDeserializer.class)
@JsonProperty("pm2_5")
private Concentration fineParticlesMatter;
@JsonDeserialize(using = ConcentrationDeserializer.class)
@JsonProperty("pm10")
private Concentration coarseParticulateMatter;
@JsonDeserialize(using = ConcentrationDeserializer.class)
@JsonProperty("nh3")
private Concentration ammonia;
}

View File

@ -1,376 +1,105 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* * Copyright (c) 2021 Alexey Zinchenko
* *
* * 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.
* 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 com.github.prominence.openweathermap.api.model.air.pollution;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.github.prominence.openweathermap.api.deserializer.EpochSecondsDeserializer;
import com.github.prominence.openweathermap.api.enums.AirQualityIndex;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.stream.Stream;
import java.time.OffsetDateTime;
import java.util.Optional;
/**
* The type Air pollution record.
* The type representing an Air pollution record.
*/
public class AirPollutionRecord {
private LocalDateTime forecastTime;
@Data
public class AirPollutionRecord implements AirPollutionConcentration {
@JsonDeserialize(using = EpochSecondsDeserializer.class)
@JsonProperty("dt")
private OffsetDateTime measurementTime;
@JsonProperty("main")
private AirQualityIndex airQualityIndex;
@JsonProperty("components")
private AirPollutionMeasurements components;
private Double CO;
private Double NO;
private Double NO2;
private Double O3;
private Double SO2;
private Double PM2_5;
private Double PM10;
private Double NH3;
/**
* Gets forecast time.
*
* @return the forecast time
*/
public LocalDateTime getForecastTime() {
return forecastTime;
@Override
public OffsetDateTime getMeasurementTime() {
return measurementTime;
}
/**
* Sets forecast time.
*
* @param forecastTime the forecast time
*/
public void setForecastTime(LocalDateTime forecastTime) {
this.forecastTime = forecastTime;
}
/**
* Gets air quality index.
*
* @return the air quality index
*/
@Override
public AirQualityIndex getAirQualityIndex() {
return airQualityIndex;
}
/**
* Sets air quality index.
*
* @param airQualityIndex the air quality index
*/
public void setAirQualityIndex(AirQualityIndex airQualityIndex) {
this.airQualityIndex = airQualityIndex;
}
/**
* Gets carbon monoxide concentration value in μg/m^3.
*
* @return the carbon monoxide value
*/
public Double getCO() {
return CO;
}
/**
* Gets carbon monoxide concentration value in μg/m^3.
*
* @return the carbon monoxide value
*/
public Double getCarbonMonoxide() {
return getCO();
}
/**
* Sets carbon monoxide concentration value in μg/m^3.
*
* @param CO the carbon monoxide value
*/
public void setCO(Double CO) {
this.CO = CO;
}
/**
* Gets nitrogen monoxide concentration value in μg/m^3.
*
* @return the nitrogen monoxide value
*/
public Double getNO() {
return NO;
}
/**
* Gets nitrogen monoxide concentration value in μg/m^3.
*
* @return the nitrogen monoxide value
*/
public Double getNitrogenMonoxide() {
return getNO();
}
/**
* Sets nitrogen monoxide concentration value in μg/m^3.
*
* @param NO the nitrogen monoxide value
*/
public void setNO(Double NO) {
this.NO = NO;
}
/**
* Gets nitrogen dioxide concentration value in μg/m^3.
*
* @return the nitrogen dioxide value
*/
public Double getNO2() {
return NO2;
}
/**
* Gets nitrogen dioxide concentration value in μg/m^3.
*
* @return the nitrogen dioxide value
*/
public Double getNitrogenDioxide() {
return getNO2();
}
/**
* Sets nitrogen dioxide concentration value in μg/m^3.
*
* @param NO2 the nitrogen dioxide value
*/
public void setNO2(Double NO2) {
this.NO2 = NO2;
}
/**
* Gets ozone concentration value in μg/m^3.
*
* @return the ozone value
*/
public Double getO3() {
return O3;
}
/**
* Gets ozone concentration value in μg/m^3.
*
* @return the ozone value
*/
public Double getOzone() {
return getO3();
}
/**
* Sets ozone concentration value in μg/m^3.
*
* @param o3 the ozone value
*/
public void setO3(Double o3) {
O3 = o3;
}
/**
* Gets sulphur dioxide concentration value in μg/m^3.
*
* @return the sulphur dioxide value
*/
public Double getSO2() {
return SO2;
}
/**
* Gets sulphur dioxide concentration value in μg/m^3.
*
* @return the sulphur dioxide value
*/
public Double getSulphurDioxide() {
return getSO2();
}
/**
* Sets sulphur dioxide concentration value in μg/m^3.
*
* @param SO2 the sulphur dioxide value
*/
public void setSO2(Double SO2) {
this.SO2 = SO2;
}
/**
* Gets fine particles matter concentration value in μg/m^3.
*
* @return the fine particles matter value
*/
public Double getPM2_5() {
return PM2_5;
}
/**
* Gets fine particles matter concentration value in μg/m^3.
*
* @return the fine particles matter value
*/
public Double getFineParticlesMatter() {
return getPM2_5();
}
/**
* Sets fine particles matter concentration value in μg/m^3.
*
* @param PM2_5 the fine particles matter value
*/
public void setPM2_5(Double PM2_5) {
this.PM2_5 = PM2_5;
}
/**
* Gets coarse particulate matter concentration value in μg/m^3.
*
* @return the coarse particulate matter value
*/
public Double getPM10() {
return PM10;
}
/**
* Gets coarse particulate matter concentration value in μg/m^3.
*
* @return the coarse particulate matter value
*/
public Double getCoarseParticulateMatter() {
return getPM10();
}
/**
* Sets coarse particulate matter concentration value in μg/m^3.
*
* @param PM10 the coarse particulate matter value
*/
public void setPM10(Double PM10) {
this.PM10 = PM10;
}
/**
* Gets ammonia concentration value in μg/m^3.
*
* @return the ammonia value
*/
public Double getNH3() {
return NH3;
}
/**
* Gets ammonia concentration value in μg/m^3.
*
* @return the ammonia value
*/
public Double getAmmonia() {
return getNH3();
}
/**
* Sets ammonia concentration value in μg/m^3.
*
* @param NH3 the ammonia value
*/
public void setNH3(Double NH3) {
this.NH3 = NH3;
}
@JsonIgnore
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AirPollutionRecord that = (AirPollutionRecord) o;
return Objects.equals(forecastTime, that.forecastTime) && airQualityIndex == that.airQualityIndex && Objects.equals(CO, that.CO) && Objects.equals(NO, that.NO) && Objects.equals(NO2, that.NO2) && Objects.equals(O3, that.O3) && Objects.equals(SO2, that.SO2) && Objects.equals(PM2_5, that.PM2_5) && Objects.equals(PM10, that.PM10) && Objects.equals(NH3, that.NH3);
public Concentration getCarbonMonoxide() {
return Optional.ofNullable(components).map(AirPollutionMeasurements::getCarbonMonoxide).orElse(null);
}
@JsonIgnore
@Override
public int hashCode() {
return Objects.hash(forecastTime, airQualityIndex, CO, NO, NO2, O3, SO2, PM2_5, PM10, NH3);
public Concentration getNitrogenMonoxide() {
return Optional.ofNullable(components).map(AirPollutionMeasurements::getNitrogenMonoxide).orElse(null);
}
@JsonIgnore
@Override
public String toString() {
final StringBuilder stringBuilder = new StringBuilder()
.append("Air Pollution Record for ")
.append(forecastTime)
.append(", AQI=")
.append(airQualityIndex.name())
.append(".");
final boolean anyConcentrationAvailable = Stream.of(CO, NO, NO2, O3, SO2, PM2_5, PM10, NH3).anyMatch(Objects::nonNull);
if (anyConcentrationAvailable) {
stringBuilder.append(" Concentrations:");
if (CO != null) {
stringBuilder
.append(" CO(Carbon monoxide) = ")
.append(CO)
.append(" μg/m^3;");
}
if (NO != null) {
stringBuilder
.append(" NO(Nitrogen monoxide) = ")
.append(NO)
.append(" μg/m^3;");
}
if (NO2 != null) {
stringBuilder
.append(" NO2(Nitrogen dioxide) = ")
.append(NO2)
.append(" μg/m^3;");
}
if (O3 != null) {
stringBuilder
.append(" O3(Ozone) = ")
.append(O3)
.append(" μg/m^3;");
}
if (SO2 != null) {
stringBuilder
.append(" SO2(Sulphur dioxide) = ")
.append(SO2)
.append(" μg/m^3;");
}
if (PM2_5 != null) {
stringBuilder
.append(" PM2.5(Fine particles matter) = ")
.append(PM2_5)
.append(" μg/m^3;");
}
if (PM10 != null) {
stringBuilder
.append(" PM10(Coarse particulate matter) = ")
.append(PM10)
.append(" μg/m^3;");
}
if (NH3 != null) {
stringBuilder
.append(" NH3(Ammonia) = ")
.append(NH3)
.append(" μg/m^3;");
}
}
return stringBuilder.toString();
public Concentration getNitrogenDioxide() {
return Optional.ofNullable(components).map(AirPollutionMeasurements::getNitrogenDioxide).orElse(null);
}
@JsonIgnore
@Override
public Concentration getOzone() {
return Optional.ofNullable(components).map(AirPollutionMeasurements::getOzone).orElse(null);
}
@JsonIgnore
@Override
public Concentration getSulphurDioxide() {
return Optional.ofNullable(components).map(AirPollutionMeasurements::getSulphurDioxide).orElse(null);
}
@JsonIgnore
@Override
public Concentration getFineParticlesMatter() {
return Optional.ofNullable(components).map(AirPollutionMeasurements::getFineParticlesMatter).orElse(null);
}
@JsonIgnore
@Override
public Concentration getCoarseParticulateMatter() {
return Optional.ofNullable(components).map(AirPollutionMeasurements::getCoarseParticulateMatter).orElse(null);
}
@JsonIgnore
@Override
public Concentration getAmmonia() {
return Optional.ofNullable(components).map(AirPollutionMeasurements::getAmmonia).orElse(null);
}
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model.air.pollution;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.ToString;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* Entity for air pollution concentration measurement.
*/
@EqualsAndHashCode
@ToString
@AllArgsConstructor
public class Concentration {
private static final int DECIMAL_PLACES = 10;
@NonNull
private final BigDecimal value;
/**
* Returns the measured value in micrograms/m^3.
*
* @return value
*/
@JsonIgnore
public BigDecimal asMicrogramsPerCubicMeters() {
return value.setScale(DECIMAL_PLACES, RoundingMode.HALF_EVEN);
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2021-present Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model.forecast;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.prominence.openweathermap.api.enums.DayTime;
import lombok.Data;
@Data
public class MetaData {
@JsonProperty("pod")
private DayTime partOfDay;
}

View File

@ -1,98 +0,0 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model.forecast.climatic;
import java.util.Objects;
/**
* The AtmosphericPressure type represents atmospheric pressure value.
* Its value can only be a double in [0, +) range.
*/
public class AtmosphericPressure {
private static final String DEFAULT_UNIT = "hPa";
private double seaLevelValue;
protected AtmosphericPressure() {
}
/**
* Static method for {@link AtmosphericPressure} creation with value checking.
* @param seaLevelValue atmospheric pressure value.
* @return instantiated {@link AtmosphericPressure} object.
*/
public static AtmosphericPressure withValue(double seaLevelValue) {
final AtmosphericPressure atmosphericPressure = new AtmosphericPressure();
atmosphericPressure.setSeaLevelValue(seaLevelValue);
return atmosphericPressure;
}
/**
* Gets sea level value.
*
* @return the sea level value.
*/
public Double getSeaLevelValue() {
return seaLevelValue;
}
/**
* Sets sea level value.
*
* @param seaLevelValue the sea level value.
* @throws IllegalArgumentException in case if provided value isn't in allowed range.
*/
public void setSeaLevelValue(double seaLevelValue) {
if (seaLevelValue < 0) {
throw new IllegalArgumentException("Atmospheric pressure value must be in [0, +∞) range.");
}
this.seaLevelValue = seaLevelValue;
}
/**
* Returns pressure unitSystem. Constantly equals to 'hPa'.
*
* @return the pressure unitSystem.
*/
public String getUnit() {
return DEFAULT_UNIT;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AtmosphericPressure that = (AtmosphericPressure) o;
return Double.compare(that.seaLevelValue, seaLevelValue) == 0;
}
@Override
public int hashCode() {
return Objects.hash(seaLevelValue);
}
@Override
public String toString() {
return "Pressure: " + seaLevelValue + ' ' + getUnit();
}
}

View File

@ -1,85 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model.forecast.climatic;
import java.util.List;
import java.util.Objects;
/**
* Represents information about forecast for different timestamps.
*/
public class Forecast {
private Location location;
private List<? extends WeatherForecast> weatherForecasts;
/**
* Returns location information.
* @return location
*/
public Location getLocation() {
return location;
}
/**
* Sets forecast location.
* @param location forecast location
*/
public void setLocation(Location location) {
this.location = location;
}
/**
* Returns list of weather forecasts for different timestamps.
* @return list of forecast-per-timestamp information.
*/
public List<? extends WeatherForecast> getWeatherForecasts() {
return weatherForecasts;
}
/**
* Sets list of weather forecasts for different timestamps.
* @param weatherForecasts list of forecast information
*/
public void setWeatherForecasts(List<? extends WeatherForecast> weatherForecasts) {
this.weatherForecasts = weatherForecasts;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Forecast forecast = (Forecast) o;
return Objects.equals(location, forecast.location) && Objects.equals(weatherForecasts, forecast.weatherForecasts);
}
@Override
public int hashCode() {
return Objects.hash(location, weatherForecasts);
}
@Override
public String toString() {
return "A forecast for " + location.getName() + " with " + weatherForecasts.size() + " timestamps.";
}
}

View File

@ -1,198 +0,0 @@
/*
* Copyright (c) 2022 Alexey Zinchenko
*
* 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 com.github.prominence.openweathermap.api.model.forecast.climatic;
import com.github.prominence.openweathermap.api.model.Coordinates;
import java.time.ZoneOffset;
import java.util.Objects;
/**
* Represents location information.
*/
public class Location {
private int id;
private String name;
private String countryCode;
private ZoneOffset zoneOffset;
private Coordinates coordinates;
private Long population;
protected Location(int id, String name) {
this.id = id;
this.name = name;
}
/**
* Creates {@link Location} object with correctness check.
* @param id location id
* @param name location name
* @return location object
*/
public static Location withValues(int id, String name) {
if (name == null) {
throw new IllegalArgumentException("Name must be set.");
}
return new Location(id, name);
}
/**
* Returns ID.
* @return location ID
*/
public int getId() {
return id;
}
/**
* Sets location ID.
* @param id location id
*/
public void setId(int id) {
this.id = id;
}
/**
* Returns location name.
* @return location name
*/
public String getName() {
return name;
}
/**
* Sets location name.
* @param name location name
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns country code.
* @return location country code
*/
public String getCountryCode() {
return countryCode;
}
/**
* Sets location country code.
* @param countryCode location country code
*/
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
/**
* Returns location timezone offset.
* @return timezone offset
*/
public ZoneOffset getZoneOffset() {
return zoneOffset;
}
/**
* Sets location timezone offset.
* @param zoneOffset timezone offset
*/
public void setZoneOffset(ZoneOffset zoneOffset) {
this.zoneOffset = zoneOffset;
}
/**
* Returns location coordinates.
* @return location coordinates.
*/
public Coordinates getCoordinates() {
return coordinates;
}
/**
* Sets location coordinates.
* @param coordinates location coordinates
*/
public void setCoordinates(Coordinates coordinates) {
this.coordinates = coordinates;
}
/**
* Sets location population.
* @return location population
*/
public Long getPopulation() {
return population;
}
/**
* Sets location population.
* @param population location population
*/
public void setPopulation(Long population) {
this.population = population;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Location)) return false;
Location location = (Location) o;
return id == location.id &&
Objects.equals(name, location.name) &&
Objects.equals(countryCode, location.countryCode) &&
Objects.equals(zoneOffset, location.zoneOffset) &&
Objects.equals(coordinates, location.coordinates) &&
Objects.equals(population, location.population);
}
@Override
public int hashCode() {
return Objects.hash(id, name, countryCode, zoneOffset, coordinates, population);
}
@Override
public String toString() {
final StringBuilder stringBuilder = new StringBuilder();
if (coordinates != null) {
stringBuilder.append(coordinates);
stringBuilder.append(". ");
}
stringBuilder.append("ID: ");
stringBuilder.append(id);
stringBuilder.append(", Name: ");
stringBuilder.append(name);
if (countryCode != null) {
stringBuilder.append('(');
stringBuilder.append(countryCode);
stringBuilder.append(')');
}
if (population != null) {
stringBuilder.append(", Population: ");
stringBuilder.append(population);
}
return stringBuilder.toString();
}
}

Some files were not shown because too many files have changed in this diff Show More