2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.sleepiq.internal.api.impl.typeadapters;
15 import java.io.IOException;
16 import java.util.Objects;
17 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
22 import com.google.gson.TypeAdapter;
23 import com.google.gson.stream.JsonReader;
24 import com.google.gson.stream.JsonToken;
25 import com.google.gson.stream.JsonWriter;
28 * Abstract type adapter for jsr310 date-time types.
30 * @author Christophe Bornet - Initial contribution
33 abstract class TemporalTypeAdapter<T> extends TypeAdapter<T> {
35 Function<String, T> parseFunction;
37 TemporalTypeAdapter(Function<String, T> parseFunction) {
38 Objects.requireNonNull(parseFunction);
39 this.parseFunction = parseFunction;
43 public void write(JsonWriter out, @Nullable T value) throws IOException {
47 out.value(value.toString());
52 public @Nullable T read(JsonReader in) throws IOException {
53 if (in.peek() == JsonToken.NULL) {
57 String temporalString = preProcess(in.nextString());
58 return parseFunction.apply(temporalString);
61 public String preProcess(String in) {