Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / lang / java / src / com / sleepycat / persist / model / SecondaryKeyMetadata.java
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7
8 package com.sleepycat.persist.model;
9
10 /**
11  * The metadata for a secondary key field.  A secondary key may be specified
12  * with the {@link SecondaryKey} annotation.
13  *
14  * <p>{@code SecondaryKeyMetadata} objects are thread-safe.  Multiple threads
15  * may safely call the methods of a shared {@code SecondaryKeyMetadata}
16  * object.</p>
17  *
18  * @author Mark Hayes
19  */
20 public class SecondaryKeyMetadata extends FieldMetadata {
21
22     private static final long serialVersionUID = 8118924993396722502L;
23
24     private String keyName;
25     private Relationship relationship;
26     private String elementClassName;
27     private String relatedEntity;
28     private DeleteAction deleteAction;
29
30     /**
31      * Used by an {@code EntityModel} to construct secondary key metadata.
32      */
33     public SecondaryKeyMetadata(String name,
34                                 String className,
35                                 String declaringClassName,
36                                 String elementClassName,
37                                 String keyName,
38                                 Relationship relationship,
39                                 String relatedEntity,
40                                 DeleteAction deleteAction) {
41         super(name, className, declaringClassName);
42         this.elementClassName = elementClassName;
43         this.keyName = keyName;
44         this.relationship = relationship;
45         this.relatedEntity = relatedEntity;
46         this.deleteAction = deleteAction;
47     }
48
49     /**
50      * Returns the class name of the array or collection element for a {@link
51      * Relationship#ONE_TO_MANY ONE_TO_MANY} or {@link
52      * Relationship#MANY_TO_MANY MANY_TO_MANY} relationship, or null for a
53      * Relationship#ONE_TO_ONE ONE_TO_ONE} or {@link Relationship#MANY_TO_ONE
54      * MANY_TO_ONE} relationship.
55      */
56     public String getElementClassName() {
57         return elementClassName;
58     }
59
60     /**
61      * Returns the key name, which may be different from the field name.
62      */
63     public String getKeyName() {
64         return keyName;
65     }
66
67     /**
68      * Returns the relationship between instances of the entity class and the
69      * secondary keys.  This may be specified using the {@link
70      * SecondaryKey#relate} annotation.
71      */
72     public Relationship getRelationship() {
73         return relationship;
74     }
75
76     /**
77      * Returns the class name of the related (foreign) entity, for which
78      * foreign key constraints are specified using the {@link
79      * SecondaryKey#relatedEntity} annotation.
80      */
81     public String getRelatedEntity() {
82         return relatedEntity;
83     }
84
85     /**
86      * Returns the action to take when a related entity is deleted having a
87      * primary key value that exists as a secondary key value for this entity.
88      * This may be specified using the {@link
89      * SecondaryKey#onRelatedEntityDelete} annotation.
90      */
91     public DeleteAction getDeleteAction() {
92         return deleteAction;
93     }
94
95     @Override
96     public boolean equals(Object other) {
97         if (other instanceof SecondaryKeyMetadata) {
98             SecondaryKeyMetadata o = (SecondaryKeyMetadata) other;
99             return super.equals(o) &&
100                    relationship == o.relationship &&
101                    ClassMetadata.nullOrEqual(deleteAction, o.deleteAction) &&
102                    ClassMetadata.nullOrEqual(keyName, o.keyName) &&
103                    ClassMetadata.nullOrEqual(elementClassName,
104                                              o.elementClassName) &&
105                    ClassMetadata.nullOrEqual(relatedEntity, o.relatedEntity);
106         } else {
107             return false;
108         }
109     }
110
111     @Override
112     public int hashCode() {
113         return super.hashCode() +
114                relationship.hashCode() +
115                ClassMetadata.hashCode(deleteAction) +
116                ClassMetadata.hashCode(keyName) +
117                ClassMetadata.hashCode(elementClassName) +
118                ClassMetadata.hashCode(relatedEntity);
119     }
120 }