Skip to content

Commit

Permalink
v9
Browse files Browse the repository at this point in the history
  • Loading branch information
astuteficus committed Oct 10, 2018
1 parent a526366 commit 20e82c2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
}

group = 'com.github.BAData'
version = '1.1.5.CUSTOM-SNAPSHOT'
version = '1.1.9.CUSTOM-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'maven'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.badata.protobuf.converter.type;

import java.lang.reflect.ParameterizedType;

/**
* Converts domain {@link Enum Enum} field value to protobuf {@link String String} field value.
*
* @author jsjem
* @author Roman Gushel
*/
public class EnumEnumConverter<T extends Enum<T>, D extends Enum<D>> implements TypeConverter<T, D> {

/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public T toDomainValue(Object instance) {
String value = ((Enum) instance).name();
Class<T> enumType = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
return Enum.valueOf(enumType, value);
}

/**
* {@inheritDoc}
*/
@Override
public D toProtobufValue(Object instance) {
String value = ((Enum) instance).name();
Class<D> enumType = (Class<D>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
return Enum.valueOf(enumType, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.badata.protobuf.converter.type;


import com.google.protobuf.Timestamp;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

public class LocalDateTimestampConverterImpl implements TypeConverter<LocalDate, Timestamp> {

@Override
public LocalDate toDomainValue(final Object instance) {
Timestamp ts = (Timestamp) instance;
return Instant.ofEpochSecond(ts.getSeconds() , ts.getNanos())
.atZone(ZoneId.systemDefault())
.toLocalDate();
}

@Override
public Timestamp toProtobufValue(final Object instance) {
LocalDate localDate = (LocalDate) instance;
return Timestamp.newBuilder()
.setSeconds(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().getEpochSecond())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected void write(final Object destination, final FieldResolver fieldResolver
}
}

private void writeValue(final Object destination, final FieldResolver fieldResolver, final Object value) throws WriteException {
private void writeValue(final Object destination, final FieldResolver fieldResolver, Object value) throws WriteException {
String setterName = FieldUtils.createDomainSetterName(fieldResolver);
try {
destinationClass.getMethod(setterName, fieldResolver.getDomainType()).invoke(destination, value);
Expand All @@ -58,6 +58,9 @@ private void writeValue(final Object destination, final FieldResolver fieldReso
} catch (NoSuchMethodException e) {
throw new WriteException(
String.format("Setter not found: '%s.%s()'", destinationClass.getName(), setterName));
} catch (IllegalArgumentException e) {
throw new WriteException(
String.format("Argument type mismatch: %s -> %s", value.getClass().getCanonicalName(), setterName));
}
}
}

0 comments on commit 20e82c2

Please sign in to comment.