Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.googlecode.objectify.impl.translate;

import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.Value;
import com.google.cloud.datastore.ValueType;
import java.math.BigDecimal;

/**
* Converts java.math.BigDecimal into a java.lang.String.
*
* <p>All custom translators must be registered *before* entity classes are registered.
*/
public class BigDecimalTranslatorFactory extends SimpleTranslatorFactory<BigDecimal, String> {
public BigDecimalTranslatorFactory() {
super(BigDecimal.class, ValueType.STRING);
}

@Override
protected BigDecimal toPojo(Value<String> value) {
return new BigDecimal(value.get());
}

@Override
protected Value<String> toDatastore(BigDecimal value) {
return StringValue.of(value.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.googlecode.objectify.impl.translate;

import com.google.cloud.Timestamp;
import com.google.cloud.datastore.TimestampValue;
import com.google.cloud.datastore.Value;
import com.google.cloud.datastore.ValueType;
import java.time.Instant;

/**
* Converts JSR-310 Instant into a java.util.Date.
*
* <p>All custom translators must be registered *before* entity classes are registered.
*/
public class InstantTimeTranslatorFactory extends SimpleTranslatorFactory<Instant, Timestamp> {
public InstantTimeTranslatorFactory() {
super(Instant.class, ValueType.TIMESTAMP);
}

@Override
protected Instant toPojo(Value<Timestamp> value) {
return Instant.ofEpochSecond(value.get().getSeconds()).plusNanos(value.get().getNanos());
}

@Override
protected Value<Timestamp> toDatastore(Instant value) {
return TimestampValue.of(
Timestamp.ofTimeSecondsAndNanos(value.getEpochSecond(), value.getNano()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.googlecode.objectify.impl.translate;

import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.Value;
import com.google.cloud.datastore.ValueType;
import java.time.LocalDateTime;

/**
* Converts JSR-310 LocalDateTime into a String.
*
* <p>All custom translators must be registered *before* entity classes are registered.
*/
public class LocalDateTimeTranslatorFactory extends SimpleTranslatorFactory<LocalDateTime, String> {
public LocalDateTimeTranslatorFactory() {
super(LocalDateTime.class, ValueType.STRING);
}

@Override
protected LocalDateTime toPojo(Value<String> value) {
return LocalDateTime.parse(value.get());
}

@Override
protected Value<String> toDatastore(LocalDateTime value) {
return StringValue.of(value.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.googlecode.objectify.impl.translate;

import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.Value;
import com.google.cloud.datastore.ValueType;
import java.time.LocalDate;

/**
* Converts JSR-310 LocalDate into a String.
*
* <p>All custom translators must be registered *before* entity classes are registered.
*/
public class LocalDateTranslatorFactory extends SimpleTranslatorFactory<LocalDate, String> {
public LocalDateTranslatorFactory() {
super(LocalDate.class, ValueType.STRING);
}

@Override
protected LocalDate toPojo(Value<String> value) {
return LocalDate.parse(value.get());
}

@Override
protected Value<String> toDatastore(LocalDate value) {
return StringValue.of(value.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.googlecode.objectify.impl.translate;

import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.Value;
import com.google.cloud.datastore.ValueType;
import java.util.Locale;

/**
* Converts java.util.Locale into a java.lang.String.
*
* <p>All custom translators must be registered *before* entity classes are registered.
*/
public class LocaleTranslatorFactory extends SimpleTranslatorFactory<Locale, String> {
public LocaleTranslatorFactory() {
super(Locale.class, ValueType.STRING);
}

@Override
protected Locale toPojo(Value<String> value) {
return Locale.forLanguageTag(value.get());
}

@Override
protected Value<String> toDatastore(Locale value) {
return StringValue.of(value.toLanguageTag());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.googlecode.objectify.impl.translate;

import com.google.cloud.Timestamp;
import com.google.cloud.datastore.TimestampValue;
import com.google.cloud.datastore.Value;
import com.google.cloud.datastore.ValueType;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

/**
* Converts JSR-310 OffsetDateTime into a java.util.Date.
*
* <p>All custom translators must be registered *before* entity classes are registered.
*/
public class OffsetDateTimeTranslatorFactory
extends SimpleTranslatorFactory<OffsetDateTime, Timestamp> {
public OffsetDateTimeTranslatorFactory() {
super(OffsetDateTime.class, ValueType.TIMESTAMP);
}

@Override
protected OffsetDateTime toPojo(Value<Timestamp> value) {
return Instant.ofEpochSecond(value.get().getSeconds())
.plusNanos(value.get().getNanos())
.atZone(ZoneOffset.UTC)
.toOffsetDateTime();
}

@Override
protected Value<Timestamp> toDatastore(OffsetDateTime value) {
return TimestampValue.of(
Timestamp.ofTimeSecondsAndNanos(value.toEpochSecond(), value.getNano()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Translators

/** Where we should insert new translators */
private int insertPoint;

/** Where we should insert new early translators */
private int earlyInsertPoint;

Expand Down Expand Up @@ -87,6 +87,14 @@ public Translators(final ObjectifyFactory fact) {
this.translatorFactories.add(new LatLngTranslatorFactory());
this.translatorFactories.add(new BlobTranslatorFactory());
this.translatorFactories.add(new RawValueTranslatorFactory());
this.translatorFactories.add(new BigDecimalTranslatorFactory());
this.translatorFactories.add(new InstantTimeTranslatorFactory());
this.translatorFactories.add(new LocalDateTranslatorFactory());
this.translatorFactories.add(new LocalDateTimeTranslatorFactory());
this.translatorFactories.add(new LocaleTranslatorFactory());
this.translatorFactories.add(new OffsetDateTimeTranslatorFactory());
this.translatorFactories.add(new ZonedDateTimeTranslatorFactory());
this.translatorFactories.add(new ZoneIdTranslatorFactory());
this.translatorFactories.add(new ObjectTranslatorFactory(this));

// LAST! It catches everything.
Expand All @@ -104,7 +112,7 @@ public void add(final TranslatorFactory<?, ?> trans) {
this.translatorFactories.add(insertPoint, trans);
insertPoint++;
}

/**
* <p>Add a new translator to the beginning of the list, before all other translators
* except other translators that have been added early.</p>
Expand Down Expand Up @@ -151,4 +159,4 @@ public <P> Translator<P, FullEntity<?>> getRoot(final Class<P> clazz) {

throw new IllegalArgumentException("Don't know how to translate " + tk.getType() + " with annotations " + Arrays.toString(tk.getAnnotations()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.googlecode.objectify.impl.translate;

import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.Value;
import com.google.cloud.datastore.ValueType;
import java.time.ZoneId;

/**
* Converts JSR-310 ZoneId into a java.lang.String.
*
* <p>All custom translators must be registered *before* entity classes are registered.
*/
public class ZoneIdTranslatorFactory extends SimpleTranslatorFactory<ZoneId, String> {
public ZoneIdTranslatorFactory() {
super(ZoneId.class, ValueType.STRING);
}

@Override
protected ZoneId toPojo(Value<String> value) {
return ZoneId.of(value.get());
}

@Override
protected Value<String> toDatastore(ZoneId value) {
return StringValue.of(value.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.googlecode.objectify.impl.translate;

import com.google.cloud.Timestamp;
import com.google.cloud.datastore.TimestampValue;
import com.google.cloud.datastore.Value;
import com.google.cloud.datastore.ValueType;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

/**
* Converts JSR-310 ZonedDateTime into a java.util.Date.
*
* <p>All custom translators must be registered *before* entity classes are registered.
*/
public class ZonedDateTimeTranslatorFactory
extends SimpleTranslatorFactory<ZonedDateTime, Timestamp> {
public ZonedDateTimeTranslatorFactory() {
super(ZonedDateTime.class, ValueType.TIMESTAMP);
}

@Override
protected ZonedDateTime toPojo(Value<Timestamp> value) {
return Instant.ofEpochSecond(value.get().getSeconds())
.plusNanos(value.get().getNanos())
.atZone(ZoneOffset.UTC);
}

@Override
protected Value<Timestamp> toDatastore(ZonedDateTime value) {
return TimestampValue.of(
Timestamp.ofTimeSecondsAndNanos(value.toEpochSecond(), value.getNano()));
}
}