Skip to content

Commit

Permalink
Add TypeAdapter for UUID, OffsetDateTime and URI
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed May 1, 2024
1 parent d3b08a2 commit 23169e3
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2019 - 2024 Blazebit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blazebit.expression.declarative.impl;

import java.io.Serializable;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import com.blazebit.domain.runtime.model.DomainType;
import com.blazebit.expression.ExpressionInterpreter;
import com.blazebit.expression.spi.TypeAdapter;

/**
* @author Christian Beikov
* @since 1.0.0
*/
public class OffsetDateTimeTimestampTypeAdapter implements TypeAdapter<OffsetDateTime, Instant>, Serializable {

public static final OffsetDateTimeTimestampTypeAdapter INSTANCE = new OffsetDateTimeTimestampTypeAdapter();

private OffsetDateTimeTimestampTypeAdapter() {
}

@Override
public Instant toInternalType(ExpressionInterpreter.Context context, OffsetDateTime value, DomainType domainType) {
if (value == null) {
return null;
}
return value.toInstant();
}

@Override
public OffsetDateTime toModelType(ExpressionInterpreter.Context context, Instant value, DomainType domainType) {
if (value == null) {
return null;
}
return OffsetDateTime.ofInstant(value, ZoneOffset.UTC);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
import java.lang.reflect.TypeVariable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.UUID;

/**
* @author Christian Beikov
Expand Down Expand Up @@ -63,13 +66,16 @@ public final class TypeAdapterRegistry implements TypeResolver {
put(exactTypeAdapters, approximateTypeAdapters, boolean.class, new TypeAdapterMetadataDefinition<>(BooleanTypeAdapter.INSTANCE, BOOLEAN));
put(exactTypeAdapters, approximateTypeAdapters, char.class, new TypeAdapterMetadataDefinition<>(CharacterTypeAdapter.INSTANCE, STRING));
put(exactTypeAdapters, approximateTypeAdapters, Character.class, new TypeAdapterMetadataDefinition<>(CharacterTypeAdapter.INSTANCE, STRING));
put(exactTypeAdapters, approximateTypeAdapters, UUID.class, new TypeAdapterMetadataDefinition<>(UUIDTypeAdapter.INSTANCE, STRING));
put(exactTypeAdapters, approximateTypeAdapters, URI.class, new TypeAdapterMetadataDefinition<>(URITypeAdapter.INSTANCE, STRING));
put(exactTypeAdapters, approximateTypeAdapters, Calendar.class, new TypeAdapterMetadataDefinition<>(CalendarTimestampTypeAdapter.INSTANCE, TIMESTAMP));
put(exactTypeAdapters, approximateTypeAdapters, GregorianCalendar.class, new TypeAdapterMetadataDefinition<>(GregorianCalendarTimestampTypeAdapter.INSTANCE, TIMESTAMP));
put(exactTypeAdapters, approximateTypeAdapters, java.sql.Date.class, new TypeAdapterMetadataDefinition<>(JavaSqlDateDateTypeAdapter.INSTANCE, DATE));
put(exactTypeAdapters, approximateTypeAdapters, java.sql.Timestamp.class, new TypeAdapterMetadataDefinition<>(JavaSqlTimestampTimestampTypeAdapter.INSTANCE, TIMESTAMP));
put(exactTypeAdapters, approximateTypeAdapters, java.util.Date.class, new TypeAdapterMetadataDefinition<>(JavaUtilDateTimestampTypeAdapter.INSTANCE, TIMESTAMP));
put(exactTypeAdapters, approximateTypeAdapters, LocalDateTime.class, new TypeAdapterMetadataDefinition<>(LocalDateTimeTimestampTypeAdapter.INSTANCE, TIMESTAMP));
put(exactTypeAdapters, approximateTypeAdapters, ZonedDateTime.class, new TypeAdapterMetadataDefinition<>(ZonedDateTimeTimestampTypeAdapter.INSTANCE, TIMESTAMP));
put(exactTypeAdapters, approximateTypeAdapters, OffsetDateTime.class, new TypeAdapterMetadataDefinition<>(OffsetDateTimeTimestampTypeAdapter.INSTANCE, TIMESTAMP));
put(exactTypeAdapters, approximateTypeAdapters, java.sql.Time.class, new TypeAdapterMetadataDefinition<>(JavaSqlTimeTimeTypeAdapter.INSTANCE, TIME));

exactTypeAdapters.put(byte.class, new TypeAdapterMetadataDefinition<>(ExactByteTypeAdapter.INSTANCE, EXACT_INTEGER));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 - 2024 Blazebit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blazebit.expression.declarative.impl;

import java.io.Serializable;
import java.net.URI;

import com.blazebit.domain.runtime.model.DomainType;
import com.blazebit.expression.ExpressionInterpreter;
import com.blazebit.expression.spi.TypeAdapter;

/**
* @author Christian Beikov
* @since 1.0.0
*/
public class URITypeAdapter implements TypeAdapter<URI, String>, Serializable {

public static final URITypeAdapter INSTANCE = new URITypeAdapter();

private URITypeAdapter() {
}

@Override
public String toInternalType(ExpressionInterpreter.Context context, URI value, DomainType domainType) {
return value.toString();
}

@Override
public URI toModelType(ExpressionInterpreter.Context context, String value, DomainType domainType) {
return URI.create(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 - 2024 Blazebit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blazebit.expression.declarative.impl;

import java.io.Serializable;
import java.util.UUID;

import com.blazebit.domain.runtime.model.DomainType;
import com.blazebit.expression.ExpressionInterpreter;
import com.blazebit.expression.spi.TypeAdapter;

/**
* @author Christian Beikov
* @since 1.0.0
*/
public class UUIDTypeAdapter implements TypeAdapter<UUID, String>, Serializable {

public static final UUIDTypeAdapter INSTANCE = new UUIDTypeAdapter();

private UUIDTypeAdapter() {
}

@Override
public String toInternalType(ExpressionInterpreter.Context context, UUID value, DomainType domainType) {
return value.toString();
}

@Override
public UUID toModelType(ExpressionInterpreter.Context context, String value, DomainType domainType) {
return UUID.fromString(value);
}
}

0 comments on commit 23169e3

Please sign in to comment.