Skip to content

Method: equals(Object)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.model.descriptors;
16:
17: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
18:
19: import java.lang.reflect.Field;
20: import java.net.URI;
21: import java.util.Collection;
22: import java.util.Collections;
23: import java.util.HashSet;
24: import java.util.Set;
25:
26: /**
27: * Defines base descriptor, which is used to specify context information for entities and their fields. </p>
28: * <p>
29: * The descriptor hierarchy is a classical <b>Composite</b> pattern.
30: *
31: * @author ledvima1
32: */
33: public abstract class Descriptor {
34:
35: protected final URI context;
36:
37: protected Descriptor() {
38: this(null);
39: }
40:
41: protected Descriptor(URI context) {
42: this.context = context;
43: }
44:
45: /**
46: * Gets context for this descriptor. </p>
47: * <p>
48: * Note that the context URI may be {@code null}, meaning that the default context is referenced
49: *
50: * @return Context URI
51: */
52: public URI getContext() {
53: return context;
54: }
55:
56: /**
57: * Gets attribute descriptors specified in this descriptor. </p>
58: *
59: * @return Unmodifiable view of attribute descriptors
60: */
61: public abstract Collection<Descriptor> getAttributeDescriptors();
62:
63: /**
64: * Gets descriptor for the specified attribute. </p>
65: *
66: * @param attribute Entity attribute, as specified by the application metamodel
67: * @return Descriptor
68: * @throws IllegalArgumentException If the descriptor is not available
69: */
70: public abstract Descriptor getAttributeDescriptor(FieldSpecification<?, ?> attribute);
71:
72: /**
73: * Adds descriptor for the specified attribute. </p>
74: *
75: * @param attribute The attribute to set descriptor for
76: * @param descriptor The descriptor to use
77: */
78: public abstract void addAttributeDescriptor(Field attribute, Descriptor descriptor);
79:
80: /**
81: * Adds repository context for the specified attribute. </p>
82: * <p>
83: * This in effect means creating a descriptor for the specified field with the specified context.
84: *
85: * @param attribute The attribute to set context for
86: * @param context The context to set
87: * @see #addAttributeDescriptor(Field, Descriptor)
88: */
89: public abstract void addAttributeContext(Field attribute, URI context);
90:
91: /**
92: * Gets all contexts present in this descriptor. </p> <p/> If any of the descriptors specifies the default context,
93: * an empty set is returned. </p>
94: * <p>
95: * In case of entity descriptor this means recursively asking all of its attributes for their context.
96: *
97: * @return Set of context URIs or an empty set, if the default one should be used
98: */
99: public Set<URI> getAllContexts() {
100: Set<URI> contexts = new HashSet<>();
101: contexts = getContextsInternal(contexts, new HashSet<>());
102: return contexts != null ? contexts : Collections.<URI>emptySet();
103: }
104:
105: /**
106: * Gets the contexts, discarding any already visited descriptors.
107: *
108: * @param contexts Contexts collected so far
109: * @param visited Visited descriptors
110: * @return Already visited contexts + those found in this descriptor
111: */
112: protected abstract Set<URI> getContextsInternal(Set<URI> contexts, Set<Descriptor> visited);
113:
114: @Override
115: public int hashCode() {
116: final int prime = 31;
117: int result = 1;
118: result = prime * result + ((context == null) ? 0 : context.hashCode());
119: return result;
120: }
121:
122: @Override
123: public boolean equals(Object obj) {
124:• if (this == obj)
125: return true;
126:• if (obj == null)
127: return false;
128:• if (getClass() != obj.getClass())
129: return false;
130: Descriptor other = (Descriptor) obj;
131:• if (context == null) {
132:• if (other.context != null)
133: return false;
134:• } else if (!context.equals(other.context))
135: return false;
136: return true;
137: }
138:
139: @Override
140: public String toString() {
141: return context != null ? context.toString() : "default_context";
142: }
143: }