App Engine Java SDK version 1.7.0
[gae.git] / java / src / main / com / google / appengine / api / datastore / EmbeddedEntity.java
bloba17e183538f86aa9cfc57b9bb80b17cf73ab11d0
1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.datastore;
5 import com.google.common.base.Objects;
7 import java.util.HashMap;
8 import java.util.Map;
10 /**
11 * A property value containing embedded entity properties (and optionally a {@link Key}).
13 * This class is similar to {@link Entity}, but differs in the following ways:
14 * <ul>
15 * <li>{@link #equals(Object)} and {@link #hashCode()} compare the embedded
16 * properties in addition to the {@link Key}.
17 * <li>It is not queriable when stored in the datastore.
18 * <li>A {@link Key} is optional.
19 * <li>{@link Key Keys} without a name or id are considered equal if all other
20 * aspects of the keys are equal (as they will not be assigned IDs by the
21 * datastore when embedded).
22 * </ul>
24 * To convert from an {@link Entity} use:
25 * <pre> {@code
26 * EmbeddedEntity sv = new EmbeddedEntity();
27 * sv.setKey(entity.getKey())
28 * sv.setPropertiesFrom(entity)
29 * }</pre>
30 * To convert to an {@link Entity} use:
31 * <pre> {@code
32 * Entity entity = new Entity(sv.getKey())
33 * entity.setPropertiesFrom(sv);
34 * }</pre>
37 public final class EmbeddedEntity extends PropertyContainer {
38 private Key key = null;
39 private final Map<String, Object> propertyMap = new HashMap<String, Object>();
41 /**
42 * @return the key or {@code null}.
44 public Key getKey() {
45 return key;
48 /**
49 * @param key the key to set
51 public void setKey(Keykey) {
52 this.key = key;
55 @Override
56 Map<String, Object> getPropertyMap() {
57 return propertyMap;
60 @Override
61 public EmbeddedEntity clone() {
62 EmbeddedEntity result = new EmbeddedEntity();
63 result.setKey(key);
64 result.setPropertiesFrom(this);
65 return result;
68 @Override
69 public String toString() {
70 StringBuilder builder = new StringBuilder('<');
71 builder.append(getClass().getName());
72 if (key != null) {
73 builder.append(" [").append(key).append(']');
75 builder.append(":\n");
76 appendPropertiesTo(builder);
77 builder.append(">\n");
78 return builder.toString();
81 @Override
82 public int hashCode() {
83 return Objects.hashCode(key, propertyMap);
86 @Override
87 public boolean equals(Object obj) {
88 if (this == obj) return true;
89 if (!(obj instanceof EmbeddedEntity)) return false;
90 EmbeddedEntity other = (EmbeddedEntity) obj;
91 if (!propertyMap.equals(other.propertyMap)) return false;
92 return key == null ? other.key == null : key.equals(other.key, false);