Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2011-2015 Spotify AB
*
* 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.spotify.asyncdatastoreclient;
import com.google.common.collect.Maps;
import com.google.protobuf.ByteString;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Represents an entity that is stored in Datastore.
*
* All properties are immutable; use {@code Entity.builder()} to construct new
* {@code Entity} instances.
*/
public final class Entity {
private final com.google.datastore.v1.Entity entity;
private Map properties;
private Entity(final com.google.datastore.v1.Entity entity) {
this.entity = entity;
this.properties = null;
}
public static final class Builder {
private com.google.datastore.v1.Entity.Builder entity;
private Map properties;
private Builder() {
this.entity = com.google.datastore.v1.Entity.newBuilder();
this.properties = Maps.newHashMap();
}
private Builder(final Key key) {
this.entity = com.google.datastore.v1.Entity.newBuilder().setKey(key.getPb());
this.properties = Maps.newHashMap();
}
private Builder(final Entity entity) {
this(entity.getPb());
}
private Builder(final com.google.datastore.v1.Entity entity) {
this.entity = com.google.datastore.v1.Entity.newBuilder(entity);
this.properties = entity.getProperties().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> Value.builder(e.getValue()).build()));
}
/**
* Creates a new {@code Entity}.
*
* @return an immutable entity.
*/
public Entity build() {
entity.getMutableProperties().clear();
entity.putAllProperties(
properties
.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey, e -> e.getValue().getPb().toBuilder().build())));
return new Entity(entity.build());
}
/**
* Set the key for this entity.
*
* @param key the key to set for this entity.
* @return this entity builder.
*/
public Builder key(final Key key) {
entity.setKey(key.getPb());
return this;
}
/**
* Set property and its value for this entity.
*
* @param name the property name to set.
* @param value the property value.
* @return this entity builder.
*/
public Builder property(final String name, final Value value) {
properties.put(name, value);
return this;
}
/**
* Set property and its value for this entity.
*
* @param name the property name to set.
* @param value the property value.
* @return this entity builder.
*/
public Builder property(final String name, final Object value) {
properties.put(name, Value.builder(value).build());
return this;
}
/**
* Set property and its value for this entity.
*
* @param name the property name to set.
* @param value the property value.
* @param indexed indicates whether the value should be indexed or not.
* @return this entity builder.
*/
public Builder property(final String name, final Object value, final boolean indexed) {
properties.put(name, Value.builder(value).indexed(indexed).build());
return this;
}
/**
* Set property and a list of value for this entity.
*
* @param name the property name to set.
* @param values a list of value.
* @return this entity builder.
*/
public Builder property(final String name, final List