View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   
6   package com.hack23.cia.model.core.impl;
7   
8   import java.io.Serializable;
9   
10  import javax.persistence.MappedSuperclass;
11  import javax.persistence.Transient;
12  
13  import org.apache.commons.lang.builder.EqualsBuilder;
14  import org.apache.commons.lang.builder.HashCodeBuilder;
15  
16  /***
17   * The Class BaseEntity.
18   */
19  @MappedSuperclass
20  public abstract class BaseEntity implements Serializable {
21  
22      /*** The Constant serialVersionUID. */
23      private static final long serialVersionUID = 1L;
24  
25      /* (non-Javadoc)
26       * @see java.lang.Object#equals(java.lang.Object)
27       */
28      @Override
29     public final boolean equals(final Object obj) {
30      if (obj == null) { return false; }
31      if (obj == this) { return true; }
32      if (obj.getClass() != getClass()) {
33        return false;
34      }
35      final BaseEntity baseEntity = (BaseEntity) obj;
36      return new EqualsBuilder().appendSuper(true)
37                    .append(getId(), baseEntity.getId())
38                    .append(getVersion(), baseEntity.getVersion())                 
39                    .isEquals();
40  
41      }
42  
43      /***
44       * Gets the id.
45       *
46       * @return the id
47       */
48      @Transient
49      public abstract Long getId();
50  
51      /***
52       * Gets the version.
53       *
54       * @return the version
55       */
56      @Transient
57      public abstract Long getVersion();
58  
59      /* (non-Javadoc)
60       * @see java.lang.Object#hashCode()
61       */
62      @Override
63      public final int hashCode() {
64          return HashCodeBuilder.reflectionHashCode(getClass().getName() + ":" + getId() + ":" + getVersion());
65      }
66  
67      /***
68       * Checks if is new.
69       *
70       * @return true, if is new
71       */
72      @Transient
73      public boolean isNew() {
74          return this.getId() == null;
75      }
76  }