use of com.google.appengine.api.datastore.Entity.WrappedValue in project appengine-java-standard by GoogleCloudPlatform.
the class DataTypeTranslator method toV1Value.
private static Value.Builder toV1Value(@Nullable Object value) {
boolean indexed = true;
boolean forceIndexedEmbeddedEntity = false;
if (value instanceof WrappedValue) {
WrappedValue wrappedValue = (WrappedValue) value;
indexed = wrappedValue.isIndexed();
forceIndexedEmbeddedEntity = wrappedValue.getForceIndexedEmbeddedEntity();
value = wrappedValue.getValue();
}
if (value instanceof Collection<?>) {
Collection<?> values = (Collection<?>) value;
if (values.isEmpty()) {
if (DatastoreServiceConfig.getEmptyListSupport()) {
return Value.newBuilder().setExcludeFromIndexes(!indexed).setArrayValue(ArrayValue.getDefaultInstance());
} else {
// null and empty list is lost.
return toV1Value(null, indexed, forceIndexedEmbeddedEntity);
}
} else {
Value.Builder valueBuilder = Value.newBuilder();
for (Object listValue : values) {
valueBuilder.getArrayValueBuilder().addValues(toV1Value(listValue, indexed, forceIndexedEmbeddedEntity));
}
return valueBuilder;
}
} else {
return toV1Value(value, indexed, forceIndexedEmbeddedEntity);
}
}
use of com.google.appengine.api.datastore.Entity.WrappedValue in project appengine-java-standard by GoogleCloudPlatform.
the class DataTypeTranslator method addPropertiesToPb.
/**
* Add all of the properties in the specified map to an {@code EntityProto}. This involves
* determining the type of each property and creating the proper type-specific protocol buffer.
*
* <p>If the property value is an {@link UnindexedValue}, or if it's a type that is never indexed,
* e.g. {@code Text} and {@code Blob}, it's added to {@code EntityProto.raw_property}. Otherwise
* it's added to {@code EntityProto.property}.
*
* @param map A not {@code null} map of all the properties which will be set on {@code proto}
* @param proto A not {@code null} protocol buffer
*/
public static void addPropertiesToPb(Map<String, ?> map, EntityProto proto) {
for (Map.Entry<String, ?> entry : map.entrySet()) {
String name = entry.getKey();
boolean forceIndexedEmbeddedEntity = false;
boolean indexed = true;
Object value = entry.getValue();
if (entry.getValue() instanceof WrappedValue) {
WrappedValue wrappedValue = (WrappedValue) entry.getValue();
forceIndexedEmbeddedEntity = wrappedValue.getForceIndexedEmbeddedEntity();
indexed = wrappedValue.isIndexed();
value = wrappedValue.getValue();
}
if (value instanceof Collection<?>) {
Collection<?> values = (Collection<?>) value;
addListPropertyToPb(proto, name, indexed, values, forceIndexedEmbeddedEntity);
} else {
addPropertyToPb(name, value, indexed, forceIndexedEmbeddedEntity, false, proto);
}
}
}
use of com.google.appengine.api.datastore.Entity.WrappedValue in project appengine-java-standard by GoogleCloudPlatform.
the class PropertyContainer method setPropertiesFrom.
/**
* A convenience method that populates properties from those in the given container.
*
* <p>This method transfers information about unindexed properties and clones any mutable values.
*
* @param src The container from which we will populate ourself.
*/
public void setPropertiesFrom(PropertyContainer src) {
for (Map.Entry<String, @Nullable Object> entry : src.getPropertyMap().entrySet()) {
String name = entry.getKey();
Object entryValue = entry.getValue();
boolean isUnindexedValue = false;
boolean isWrappedValue = false;
boolean indexed = true;
boolean forceIndexedEntityValue = false;
Object valueToAdd = entryValue;
if (entryValue instanceof UnindexedValue) {
isUnindexedValue = true;
indexed = false;
valueToAdd = ((UnindexedValue) entryValue).getValue();
} else if (entryValue instanceof WrappedValue) {
WrappedValue wrappedValue = (WrappedValue) entryValue;
isWrappedValue = true;
indexed = wrappedValue.isIndexed();
forceIndexedEntityValue = wrappedValue.getForceIndexedEmbeddedEntity();
valueToAdd = wrappedValue.getValue();
}
// Clone collections
if (valueToAdd instanceof Collection<?>) {
// Convert all collections to an ArrayList, as that is the representation
// returned from the datastore.
Collection<?> srcColl = (Collection<?>) valueToAdd;
Collection<Object> destColl = new ArrayList<Object>(srcColl.size());
valueToAdd = destColl;
for (Object element : srcColl) {
destColl.add(cloneIfMutable(element));
}
} else {
valueToAdd = cloneIfMutable(valueToAdd);
}
if (isUnindexedValue) {
valueToAdd = new UnindexedValue(valueToAdd);
} else if (isWrappedValue) {
valueToAdd = new WrappedValueImpl(valueToAdd, indexed, forceIndexedEntityValue);
}
// Put the copy in the map
getPropertyMap().put(name, valueToAdd);
}
}
Aggregations