de.chojo.sadu.mapper.reader.StandardReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sadu-mapper Show documentation
Show all versions of sadu-mapper Show documentation
SADU module to map values received from a database to java objects using the queries module.
The newest version!
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/
package de.chojo.sadu.mapper.reader;
import de.chojo.sadu.core.conversion.UUIDConverter;
import de.chojo.sadu.mapper.wrapper.Row;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.UUID;
/**
* Default implementations of {@link ValueReader}.
*/
public final class StandardReader {
public static final ValueReader INSTANT_FROM_MILLIS = ValueReader.create(Instant::ofEpochMilli, Row::getLong, Row::getLong);
public static final ValueReader INSTANT_FROM_SECONDS = ValueReader.create(Instant::ofEpochSecond, Row::getLong, Row::getLong);
public static final ValueReader INSTANT_FROM_TIMESTAMP = ValueReader.create(Timestamp::toInstant, Row::getTimestamp, Row::getTimestamp);
public static final ValueReader LOCAL_DATE_TIME = ValueReader.create(Timestamp::toLocalDateTime, Row::getTimestamp, Row::getTimestamp);
public static final ValueReader OFFSET_DATE_TIME = ValueReader.create(t -> OffsetDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault()), Row::getTimestamp, Row::getTimestamp);
public static final ValueReader ZONED_DATE_TIME = ValueReader.create(t -> t.toInstant().atZone(ZoneId.systemDefault()), Row::getTimestamp, Row::getTimestamp);
public static final ValueReader OFFSET_TIME = ValueReader.create(t -> t.toLocalTime().atOffset(OffsetTime.now().getOffset()), Row::getTime, Row::getTime);
public static final ValueReader LOCAL_DATE = ValueReader.create(Date::toLocalDate, Row::getDate, Row::getDate);
public static final ValueReader LOCAL_TIME = ValueReader.create(Time::toLocalTime, Row::getTime, Row::getTime);
public static final ValueReader UUID_FROM_STRING = ValueReader.create(UUID::fromString, Row::getString, Row::getString);
public static final ValueReader UUID_FROM_BYTES = ValueReader.create(UUIDConverter::convert, Row::getBytes, Row::getBytes);
@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader localDate(Calendar calendar) {
return ValueReader.create(Date::toLocalDate, (row, name) -> row.getDate(name, calendar), (row, integer) -> row.getDate(integer, calendar));
}
@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader localTime(Calendar calendar) {
return ValueReader.create(Time::toLocalTime, (row, name) -> row.getTime(name, calendar), (row, integer) -> row.getTime(integer, calendar));
}
@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader offsetTime(Calendar calendar) {
return ValueReader.create(t -> t.toLocalTime().atOffset(OffsetTime.now().getOffset()), (row, name) -> row.getTime(name, calendar), (row, integer) -> row.getTime(integer, calendar));
}
@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader localDateTime(Calendar calendar) {
return ValueReader.create(Timestamp::toLocalDateTime, (row, name) -> row.getTimestamp(name, calendar), (row, integer) -> row.getTimestamp(integer, calendar));
}
@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader offsetDateTime(Calendar calendar) {
return ValueReader.create(t -> OffsetDateTime.ofInstant(t.toInstant(), ZoneId.systemDefault()), (row, name) -> row.getTimestamp(name, calendar), (row, integer) -> row.getTimestamp(integer, calendar));
}
@Contract(value = "_ -> new", pure = true)
public static @NotNull ValueReader zonedDateTime(Calendar calendar) {
return ValueReader.create(t -> t.toInstant().atZone(ZoneId.systemDefault()), (row, name) -> row.getTimestamp(name, calendar), (row, integer) -> row.getTimestamp(integer, calendar));
}
@Contract(value = "_ -> new", pure = true)
public static > @NotNull ValueReader forEnum(Class clazz) {
return ValueReader.create(v -> Enum.valueOf(clazz, v), Row::getString, Row::getString);
}
}