-
Notifications
You must be signed in to change notification settings - Fork 5
/
UniqueEntity.java
80 lines (63 loc) · 1.81 KB
/
UniqueEntity.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
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.models;
import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;
/** Supplies every subclass with a generated UUID, making it unique */
public abstract class UniqueEntity extends Entity implements Serializable {
/** Field name of {@link UniqueEntity} uuid */
public static final String UUID_FIELD_NAME = "uuid";
private final UUID uuid;
protected UniqueEntity() {
uuid = UUID.randomUUID();
}
protected UniqueEntity(UUID uuid) {
this.uuid = uuid == null ? UUID.randomUUID() : uuid;
}
public UUID getUuid() {
return uuid;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UniqueEntity that = (UniqueEntity) o;
return uuid.equals(that.uuid);
}
@Override
public int hashCode() {
return Objects.hash(uuid);
}
@Override
public String toString() {
return "UniqueEntity{" + "uuid=" + uuid + '}';
}
/**
* Abstract class for all builder that build child entities of abstract class {@link UniqueEntity}
*
* @version 0.1
* @since 05.06.20
*/
public abstract static class UniqueEntityCopyBuilder<B extends UniqueEntityBuilder>
implements UniqueEntityBuilder {
private UUID uuid;
protected UniqueEntityCopyBuilder(UniqueEntity entity) {
this.uuid = entity.getUuid();
}
public B uuid(UUID uuid) {
this.uuid = uuid;
return thisInstance();
}
protected UUID getUuid() {
return uuid;
}
protected abstract B thisInstance();
}
protected interface UniqueEntityBuilder {
UniqueEntity build();
}
}