View Javadoc
1   package fr.ifremer.quadrige3.ui.core.dto;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  import org.jdesktop.beans.AbstractBean;
26  import org.nuiton.util.CollectionUtil;
27  
28  import java.util.Collection;
29  import java.util.List;
30  import java.util.Objects;
31  
32  /**
33   * Abstract Dali bean.
34   */
35  public abstract class QuadrigeAbstractBean extends AbstractBean implements QuadrigeBean, Comparable<QuadrigeBean> {
36  
37      private static final long serialVersionUID = 1L;
38  
39      protected Integer id;
40  
41      /** {@inheritDoc} */
42      @Override
43      public Integer getId() {
44          return id;
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      public void setId(Integer id) {
50          this.id = id;
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public int hashCode() {
56          if (id != null) {
57              return id;
58          }
59          // specific cases of QuadrigeBean without id
60          else if (this instanceof CodeOnly) {
61              return Objects.hashCode(((CodeOnly) this).getCode());
62          }
63          else {
64              return super.hashCode();
65          }
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public boolean equals(Object o) {
71          if (this == o) {
72              return true;
73          }
74          if (o == null) {
75              return false;
76          }
77  
78          if (!(o instanceof QuadrigeAbstractBean)) {
79              return false;
80          }
81          if (!comparableClass(o.getClass())) {
82              // not same class
83              return false;
84          }
85  
86          QuadrigeAbstractBean that = (QuadrigeAbstractBean) o;
87  
88          return this.hashCode() == that.hashCode();
89      }
90  
91      private boolean comparableClass(Class<?> aClass) {
92          return getClass().equals(aClass)
93                  || getClass().isAssignableFrom(aClass)
94                  || aClass.isAssignableFrom(getClass());
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public final int compareTo(QuadrigeBean o) {
100         return Integer.compare(hashCode(), o.hashCode());
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public String toString() {
106         return String.format("%s: %s", super.toString(), hashCode());
107     }
108 
109     /**
110      * <p>getChild.</p>
111      *
112      * @param child a {@link Collection} object.
113      * @param index a int.
114      * @param <B> a B object.
115      * @return a B object.
116      */
117     protected <B> B getChild(Collection<B> child, int index) {
118         return CollectionUtil.getOrNull(child, index);
119     }
120 
121     /**
122      * <p>getChild.</p>
123      *
124      * @param child a {@link List} object.
125      * @param index a int.
126      * @param <B> a B object.
127      * @return a B object.
128      */
129     protected <B> B getChild(List<B> child, int index) {
130         return CollectionUtil.getOrNull(child, index);
131     }
132 }