1 package fr.ifremer.tutti.persistence.entities;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.base.MoreObjects;
26 import org.apache.commons.lang3.ObjectUtils;
27 import org.nuiton.util.CollectionUtil;
28
29 import java.io.Serializable;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.Objects;
33
34
35
36
37
38
39
40 public abstract class TuttiEntityBean implements Serializable, TuttiEntity {
41
42 private static final long serialVersionUID = 1L;
43
44 protected String id;
45
46 protected Integer intId;
47
48 @Override
49 public String getId() {
50 return id;
51 }
52
53 @Override
54 public void setId(String id) {
55 this.id = id;
56 intId = null;
57 }
58
59 @Override
60 public void setId(Integer id) {
61 intId = id;
62 this.id = id == null ? null : String.valueOf(id);
63 }
64
65 @Override
66 public Integer getIdAsInt() {
67 if (intId == null && id != null) {
68 intId = Integer.valueOf(id);
69 }
70 return intId;
71 }
72
73 @Override
74 public boolean equals(Object o) {
75 if (this == o) {
76 return true;
77 }
78 if (o == null) {
79 return false;
80 }
81 if (ObjectUtils.notEqual(o.getClass(), getClass())) {
82
83 return false;
84 }
85 if (!(o instanceof TuttiEntityBean)) return false;
86
87 TuttiEntityBean that = (TuttiEntityBean) o;
88
89 return Objects.equals(getId(), that.getId());
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hashCode(getId());
95 }
96
97 @Override
98 public String toString() {
99 return MoreObjects.toStringHelper(this)
100 .add(PROPERTY_ID, getId())
101 .toString();
102 }
103
104 protected <B> B getChild(Collection<B> child, int index) {
105 return CollectionUtil.getOrNull(child, index);
106 }
107
108 protected <B> B getChild(List<B> child, int index) {
109 return CollectionUtil.getOrNull(child, index);
110 }
111 }