View Javadoc
1   package fr.ifremer.tutti.persistence.entities;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * Abstract tutti entity.
36   *
37   * @author Tony Chemit - chemit@codelutin.com
38   * @since 0.1
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              // not sale class
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 }