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
14 * Imported from https://github.com/google-gson/typeadapters/tree/master/jsr310/src
15 * and repackaged to avoid the default package.
17 package org.openhab.binding.lametrictime.internal.api.common.impl.typeadapters.imported;
19 import java.io.IOException;
20 import java.util.Objects;
21 import java.util.function.Function;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
26 import com.google.gson.TypeAdapter;
27 import com.google.gson.stream.JsonReader;
28 import com.google.gson.stream.JsonToken;
29 import com.google.gson.stream.JsonWriter;
32 * Abstract type adapter for jsr310 date-time types.
34 * @author Christophe Bornet - Initial contribution
37 abstract class TemporalTypeAdapter<T> extends TypeAdapter<T> {
39 Function<String, T> parseFunction;
41 TemporalTypeAdapter(Function<String, T> parseFunction) {
42 Objects.requireNonNull(parseFunction);
43 this.parseFunction = parseFunction;
47 public void write(JsonWriter out, @Nullable T value) throws IOException {
51 out.value(value.toString());
56 public @Nullable T read(JsonReader in) throws IOException {
57 if (in.peek() == JsonToken.NULL) {
61 String temporalString = preProcess(in.nextString());
62 return parseFunction.apply(temporalString);
65 public String preProcess(String in) {