1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: EntityMetadata.java,v 1.1 2008/02/07 17:12:28 mark Exp $
7 */
8
9package com.sleepycat.persist.model;
10
11import java.io.Serializable;
12import java.util.Map;
13
14/**
15 * The metadata for a persistent entity class.  An entity class may be
16 * specified with the {@link Entity} annotation.
17 *
18 * <p>{@code EntityMetadata} objects are thread-safe.  Multiple threads may
19 * safely call the methods of a shared {@code EntityMetadata} object.</p>
20 *
21 * @author Mark Hayes
22 */
23public class EntityMetadata implements Serializable {
24
25    private static final long serialVersionUID = 4224509631681963159L;
26
27    private String className;
28    private PrimaryKeyMetadata primaryKey;
29    private Map<String,SecondaryKeyMetadata> secondaryKeys;
30
31    /**
32     * Used by an {@code EntityModel} to construct entity metadata.
33     */
34    public EntityMetadata(String className,
35                          PrimaryKeyMetadata primaryKey,
36                          Map<String,SecondaryKeyMetadata> secondaryKeys) {
37        this.className = className;
38        this.primaryKey = primaryKey;
39        this.secondaryKeys = secondaryKeys;
40    }
41
42    /**
43     * Returns the name of the entity class.
44     */
45    public String getClassName() {
46        return className;
47    }
48
49    /**
50     * Returns the primary key metadata for this entity.  Note that the primary
51     * key field may be declared in this class or in a subclass. This metadata
52     * may be specified using the {@link PrimaryKey} annotation.
53     */
54    public PrimaryKeyMetadata getPrimaryKey() {
55        return primaryKey;
56    }
57
58    /**
59     * Returns an unmodifiable map of key name to secondary key metadata, or
60     * an empty map if no secondary keys are defined for this entity.  The
61     * returned map contains a mapping for each secondary key of this entity,
62     * including secondary keys declared in subclasses and superclasses.  This
63     * metadata may be specified using {@link SecondaryKey} annotations.
64     */
65    public Map<String,SecondaryKeyMetadata> getSecondaryKeys() {
66        return secondaryKeys;
67    }
68
69    @Override
70    public boolean equals(Object other) {
71        if (other instanceof EntityMetadata) {
72            EntityMetadata o = (EntityMetadata) other;
73            return ClassMetadata.nullOrEqual(className, o.className) &&
74                   ClassMetadata.nullOrEqual(primaryKey, o.primaryKey) &&
75                   ClassMetadata.nullOrEqual(secondaryKeys, o.secondaryKeys);
76        } else {
77            return false;
78        }
79    }
80
81    @Override
82    public int hashCode() {
83        return ClassMetadata.hashCode(className) +
84               ClassMetadata.hashCode(primaryKey) +
85               ClassMetadata.hashCode(secondaryKeys);
86    }
87}
88