1.9.30 sync.
[gae.git] / java / src / main / com / google / appengine / api / datastore / EmbeddedEntity.java
blobf56a2b102b279242d8d66426a953b2ea7042a712
1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.datastore;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Objects;
9 /**
10 * A property value containing embedded entity properties (and optionally a {@link Key}).
12 * This class is similar to {@link Entity}, but differs in the following ways:
13 * <ul>
14 * <li>{@link #equals(Object)} and {@link #hashCode()} compare the embedded
15 * properties in addition to the {@link Key}.
16 * <li>It is not queryable when stored in the datastore.
17 * <li>A {@link Key} is optional.
18 * <li>{@link Key Keys} without a name or id are considered equal if all other
19 * aspects of the keys are equal (as they will not be assigned IDs by the
20 * datastore when embedded).
21 * </ul>
23 * To convert from an {@link Entity} use:
24 * <pre> {@code
25 * EmbeddedEntity sv = new EmbeddedEntity();
26 * sv.setKey(entity.getKey())
27 * sv.setPropertiesFrom(entity)
28 * }</pre>
29 * To convert to an {@link Entity} use:
30 * <pre> {@code
31 * Entity entity = new Entity(sv.getKey())
32 * entity.setPropertiesFrom(sv);
33 * }</pre>
36 public final class EmbeddedEntity extends PropertyContainer {
37 private Key key = null;
38 private final Map<String, Object> propertyMap = new HashMap<String, Object>();
40 /**
41 * @return the key or {@code null}.
43 public Key getKey() {
44 return key;
47 /**
48 * @param key the key to set
50 public void setKey( Key key) {
51 this.key = key;
54 @Override
55 Map<String, Object> getPropertyMap() {
56 return propertyMap;
59 @Override
60 public EmbeddedEntity clone() {
61 EmbeddedEntity result = new EmbeddedEntity();
62 result.setKey(key);
63 result.setPropertiesFrom(this);
64 return result;
67 @Override
68 public String toString() {
69 StringBuilder builder = new StringBuilder("<EmbeddedEntity");
70 if (key != null) {
71 builder.append(" [").append(key).append(']');
73 builder.append(":\n");
74 appendPropertiesTo(builder);
75 builder.append(">\n");
76 return builder.toString();
79 @Override
80 public int hashCode() {
81 return Objects.hash(key, propertyMap);
84 @Override
85 public boolean equals(Object obj) {
86 if (this == obj) return true;
87 if (!(obj instanceof EmbeddedEntity)) return false;
88 EmbeddedEntity other = (EmbeddedEntity) obj;
89 if (!propertyMap.equals(other.propertyMap)) return false;
90 return key == null ? other.key == null : key.equals(other.key, false);