forked from rgushel/protobuf-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a526366
commit 20e82c2
Showing
4 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/net/badata/protobuf/converter/type/EnumEnumConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/net/badata/protobuf/converter/type/LocalDateTimestampConverterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters