-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNested.java
91 lines (74 loc) · 2.46 KB
/
Nested.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.db.mixing;
import sirius.kernel.di.std.Part;
import sirius.kernel.health.Exceptions;
import java.util.Objects;
/**
* Base class for all nested objects.
* <p>
* A nested object can be placed in a {@link sirius.db.mixing.types.NestedList} or
* {@link sirius.db.mixing.types.StringNestedMap} if the {@link BaseMapper mapper} supports it.
* <p>
* Do not use this to embed a single object in an entity as a {@link Composite} can be used for that. This
* will automatically unfold all properties along with consistency checks.
* <p>
* As it derives from {@link Mixable} a nested object can be extended using <tt>Mixins</tt>.
*/
public class Nested extends Mixable {
@Part
private static Mixing mixing;
/**
* Creates a copy of this object.
* <p>
* Note that this will only copy known {@link Property properties} and skip transient ones.
*
* @return a copy of this object
*/
public Nested copy() {
try {
EntityDescriptor descriptor = mixing.getDescriptor(getClass());
Nested copy = getClass().getDeclaredConstructor().newInstance();
for (Property property : descriptor.getProperties()) {
property.setValue(copy, property.getValueAsCopy(this));
}
return copy;
} catch (Exception e) {
throw Exceptions.handle(Mixing.LOG, e);
}
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (!getClass().isInstance(obj)) {
return false;
}
EntityDescriptor descriptor = mixing.getDescriptor(getClass());
for (Property property : descriptor.getProperties()) {
if (!Objects.equals(property.getValue(this), property.getValue(obj))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = 1;
EntityDescriptor descriptor = mixing.getDescriptor(getClass());
for (Property property : descriptor.getProperties()) {
Object element = property.getValue(this);
result = 31 * result + (element == null ? 0 : element.hashCode());
}
return result;
}
}