1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.persistence.jpa.entity;
20
21 import com.fasterxml.jackson.annotation.JsonIdentityInfo;
22 import com.fasterxml.jackson.annotation.ObjectIdGenerators;
23 import java.util.Objects;
24 import org.apache.syncope.core.persistence.api.entity.Entity;
25 import org.apache.syncope.core.persistence.api.entity.Implementation;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "key")
30 public abstract class AbstractEntity implements Entity {
31
32 private static final long serialVersionUID = -9017214159540857901L;
33
34 protected static final Logger LOG = LoggerFactory.getLogger(AbstractEntity.class);
35
36 protected void checkType(final Object object, final Class<?> clazz) {
37 if (object != null && !clazz.isInstance(object)) {
38 throw new ClassCastException("Expected " + clazz.getName() + ", got " + object.getClass().getName());
39 }
40 }
41
42 protected void checkImplementationType(final Implementation object, final String expected) {
43 if (object != null && !object.getType().equals(expected)) {
44 throw new ClassCastException("Expected " + expected + ", got " + object.getType());
45 }
46 }
47
48 @Override
49 public boolean equals(final Object obj) {
50 if (this == obj) {
51 return true;
52 }
53 if (!(obj instanceof AbstractEntity)) {
54 return false;
55 }
56 AbstractEntity entity = (AbstractEntity) obj;
57 return Objects.equals(getKey(), entity.getKey());
58 }
59
60 @Override
61 public int hashCode() {
62 return Objects.hash(getKey());
63 }
64
65 @Override
66 public String toString() {
67 return new StringBuilder().append(getClass().getSimpleName()).
68 append('[').append(getKey()).append(']').toString();
69 }
70 }