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
;
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:
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).
24 * To convert from an {@link Entity} use:
26 * EmbeddedEntity sv = new EmbeddedEntity();
27 * sv.setKey(entity.getKey())
28 * sv.setPropertiesFrom(entity)
30 * To convert to an {@link Entity} use:
32 * Entity entity = new Entity(sv.getKey())
33 * entity.setPropertiesFrom(sv);
37 public final class EmbeddedEntity
extends PropertyContainer
{
38 private Key key
= null;
39 private final Map
<String
, Object
> propertyMap
= new HashMap
<String
, Object
>();
42 * @return the key or {@code null}.
49 * @param key the key to set
51 public void setKey(Keykey
) {
56 Map
<String
, Object
> getPropertyMap() {
61 public EmbeddedEntity
clone() {
62 EmbeddedEntity result
= new EmbeddedEntity();
64 result
.setPropertiesFrom(this);
69 public String
toString() {
70 StringBuilder builder
= new StringBuilder('<');
71 builder
.append(getClass().getName());
73 builder
.append(" [").append(key
).append(']');
75 builder
.append(":\n");
76 appendPropertiesTo(builder
);
77 builder
.append(">\n");
78 return builder
.toString();
82 public int hashCode() {
83 return Objects
.hashCode(key
, propertyMap
);
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);