Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
32   243   25   2
16   105   0.78   4
16     1.56  
4    
 
 
  AbstractEntity       Line # 40 29 22 25.9% 0.25862068
  AbstractEntity.IdComparator       Line # 155 1 1 0% 0.0
  AbstractEntity.CreateDateComparator       Line # 169 1 1 0% 0.0
  AbstractEntity.LastModifiedDateComparator       Line # 187 1 1 0% 0.0
 
No Tests
 
1    /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10    * the Free Software Foundation; either version 2 of the License, or
11    * (at your option) any later version.
12    *
13    * This program is distributed in the hope that it will be useful,
14    * but WITHOUT ANY WARRANTY; without even the implied warranty of
15    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16    * GNU General Public License for more details.
17    */
18   
19    package org.itracker.model;
20   
21    import java.io.Serializable;
22    import java.util.Comparator;
23    import java.util.Date;
24   
25    import org.apache.commons.lang.builder.CompareToBuilder;
26    import org.apache.commons.lang.builder.EqualsBuilder;
27    import org.apache.commons.lang.builder.HashCodeBuilder;
28   
29    /**
30    * This is a POJO Business Domain Object. Hibernate Bean.
31    *
32    * <p>
33    * All entities are Java Beans and should inherit this class to make sure they
34    * are Serializable and Cloneable and have the following fields : an id, a
35    * creation date and a last modifiation date.
36    * </p>
37    *
38    * @author ready
39    */
 
40    public abstract class AbstractEntity implements Entity {
41   
42    /**
43    *
44    */
45    private static final long serialVersionUID = 1L;
46   
47    public static final Comparator<Entity> ID_COMPARATOR = new IdComparator();
48   
49    public static final Comparator<AbstractEntity> CREATE_DATE_COMPARATOR = new CreateDateComparator();
50   
51    public static final Comparator<AbstractEntity> LAST_MODIFIED_DATE_COMPARATOR = new LastModifiedDateComparator();
52   
53    /** System ID */
54    private Integer id;
55   
56    /** Creation date and time. */
57    private Date createDate = new Date();
58   
59    /** Last modification date and time. */
60    private Date lastModifiedDate = new Date();
61   
62    /**
63    * Default constructor (required by Hibernate).
64    */
 
65  40 toggle public AbstractEntity() {
66    }
67   
 
68  8 toggle public Integer getId() {
69  8 return id;
70    }
71   
 
72  23 toggle public void setId(Integer id) {
73  23 this.id = id;
74    }
75   
76    /**
77    * @return creation time stamp or <tt>null</tt> for transient entities
78    */
 
79  0 toggle public Date getCreateDate() {
80  0 if (null == createDate)
81  0 createDate = new Date();
82  0 return new Date(createDate.getTime());
83    }
84   
85    /**
86    * Sets the creation date and time.
87    *
88    * <p>
89    * The persistence framework automatically sets this property when a new
90    * entity is persisted. <br>
91    * Note that the value is managed by the persistence framework and may be
92    * generated by the database in the future.
93    * </p>
94    *
95    * <p>
96    * The creation time stamp should never change once initialized.
97    * </p>
98    *
99    * @param dateTime
100    * creation time stamp
101    */
 
102  2 toggle public void setCreateDate(Date dateTime) {
103  2 if (null == dateTime)
104  0 return;
105  2 this.createDate = new Date(dateTime.getTime());
106    }
107   
108    /**
109    * @return last modification time stamp or <tt>null</tt> for transient
110    * entities
111    */
 
112  0 toggle public Date getLastModifiedDate() {
113  0 if (null == this.lastModifiedDate)
114  0 this.lastModifiedDate = new Date();
115  0 return new Date(lastModifiedDate.getTime());
116    }
117   
118    /**
119    * Sets the last modification date and time.
120    *
121    * <p>
122    * The persistence framework automatically sets this property to the same
123    * value as the createDate property when persisting a new entity and
124    * automatically updates it when saving an existing one. <br>
125    * Note that the value is managed by the persistence framework and may be
126    * generated by the database in the future.
127    * </p>
128    *
129    * @param dateTime
130    * last modification time stamp
131    */
 
132  1 toggle public void setLastModifiedDate(Date dateTime) {
133  1 if (null == dateTime)
134  0 return;
135  1 this.lastModifiedDate = new Date(dateTime.getTime());
136    }
137   
138    /**
139    * Returns whether this instance represents a new transient instance.
140    *
141    * @return <tt>true</tt> if <code>id</code> is <tt>null</tt>
142    */
 
143  0 toggle public boolean isNew() {
144  0 return (this.getId() == null);
145    }
146   
 
147  38 toggle @Override
148    public Object clone() throws CloneNotSupportedException {
149  38 return super.clone();
150    }
151   
152    /**
153    * Compares 2 instances by ID.
154    */
 
155    protected static class IdComparator implements Comparator<Entity>, Serializable {
156   
157    /**
158    *
159    */
160    private static final long serialVersionUID = 1L;
161   
 
162  0 toggle public int compare(Entity a, Entity b) {
163  0 return new CompareToBuilder().append(a.getId(), b.getId())
164    .toComparison();
165    }
166   
167    }
168   
 
169    protected static class CreateDateComparator implements
170    Comparator<AbstractEntity>, Serializable {
171   
172    /**
173    *
174    */
175    private static final long serialVersionUID = 1L;
176   
 
177  0 toggle public int compare(AbstractEntity a, AbstractEntity b) {
178  0 return new CompareToBuilder().append(a.getCreateDate(),
179    b.getCreateDate()).toComparison();
180    }
181   
182    }
183   
184    /**
185    * Compares 2 instances by last modified date.
186    */
 
187    protected static class LastModifiedDateComparator implements
188    Comparator<AbstractEntity>, Serializable {
189   
190    /**
191    *
192    */
193    private static final long serialVersionUID = 1L;
194   
 
195  0 toggle public int compare(AbstractEntity a, AbstractEntity b) {
196  0 return new CompareToBuilder().append(a.getLastModifiedDate(),
197    b.getLastModifiedDate()).toComparison();
198    }
199   
200    }
201   
 
202  0 toggle @Override
203    public final boolean equals(Object obj) {
204   
205  0 if (this == obj) {
206  0 return true;
207    }
208  0 if (isNew() || null == obj) {
209  0 return false;
210    }
211   
212  0 if (getClass().equals(obj.getClass())) {
213  0 Entity o = (Entity) obj;
214  0 return new EqualsBuilder()
215    .append(getId(), o.getId()).isEquals();
216   
217    }
218   
219  0 return false;
220   
221    }
222   
 
223  0 toggle public final int compareTo(Entity o) {
224  0 if (this.equals(o)) {
225  0 return 0;
226    }
227  0 return new CompareToBuilder().append(getClass(), o.getClass(), AbstractEntity.CLASS_COMPARATOR).append(
228    getId(), o.getId()).toComparison();
229    }
230   
 
231  0 toggle @Override
232    public final int hashCode() {
233  0 return new HashCodeBuilder().append(getClass()).append(getId()).toHashCode();
234   
235    }
236   
237    private static final Comparator<Class<?>> CLASS_COMPARATOR = new Comparator<Class<?>>() {
 
238  0 toggle public int compare(Class<?> o1, Class<?> o2) {
239  0 return new CompareToBuilder().append(o1.getSimpleName(), o2.getSimpleName()).append(o1.hashCode(), hashCode()).toComparison();
240    }
241    };
242   
243    }