1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.datastore
;
5 import java
.util
.HashMap
;
7 import java
.util
.Objects
;
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:
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).
23 * To convert from an {@link Entity} use:
25 * EmbeddedEntity sv = new EmbeddedEntity();
26 * sv.setKey(entity.getKey())
27 * sv.setPropertiesFrom(entity)
29 * To convert to an {@link Entity} use:
31 * Entity entity = new Entity(sv.getKey())
32 * entity.setPropertiesFrom(sv);
36 public final class EmbeddedEntity
extends PropertyContainer
{
37 private Key key
= null;
38 private final Map
<String
, Object
> propertyMap
= new HashMap
<String
, Object
>();
41 * @return the key or {@code null}.
48 * @param key the key to set
50 public void setKey( Key key
) {
55 Map
<String
, Object
> getPropertyMap() {
60 public EmbeddedEntity
clone() {
61 EmbeddedEntity result
= new EmbeddedEntity();
63 result
.setPropertiesFrom(this);
68 public String
toString() {
69 StringBuilder builder
= new StringBuilder("<EmbeddedEntity");
71 builder
.append(" [").append(key
).append(']');
73 builder
.append(":\n");
74 appendPropertiesTo(builder
);
75 builder
.append(">\n");
76 return builder
.toString();
80 public int hashCode() {
81 return Objects
.hash(key
, propertyMap
);
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);