1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.springframework.batch.core; |
18 | |
19 | import java.io.Serializable; |
20 | |
21 | import org.springframework.util.ClassUtils; |
22 | |
23 | /** |
24 | * Batch Domain Entity class. Any class that should be uniquely identifiable |
25 | * from another should subclass from Entity. More information on this pattern |
26 | * and the difference between Entities and Value Objects can be found in Domain |
27 | * Driven Design by Eric Evans. |
28 | * |
29 | * @author Lucas Ward |
30 | * @author Dave Syer |
31 | * |
32 | */ |
33 | public class Entity implements Serializable { |
34 | |
35 | private Long id; |
36 | |
37 | private Integer version; |
38 | |
39 | public Entity() { |
40 | super(); |
41 | } |
42 | |
43 | public Entity(Long id) { |
44 | super(); |
45 | |
46 | //Commented out because StepExecutions are still created in a disconnected |
47 | //manner. The Repository should create them, then this can be uncommented. |
48 | //Assert.notNull(id, "Entity id must not be null."); |
49 | this.id = id; |
50 | } |
51 | |
52 | public Long getId() { |
53 | return id; |
54 | } |
55 | |
56 | public void setId(Long id) { |
57 | this.id = id; |
58 | } |
59 | |
60 | /** |
61 | * @return the version |
62 | */ |
63 | public Integer getVersion() { |
64 | return version; |
65 | } |
66 | |
67 | /** |
68 | * |
69 | */ |
70 | public void incrementVersion() { |
71 | if (version == null) { |
72 | version = new Integer(0); |
73 | } else { |
74 | version = new Integer(version.intValue() + 1); |
75 | } |
76 | } |
77 | |
78 | // @Override |
79 | public String toString() { |
80 | return ClassUtils.getShortName(getClass()) + ": id=" + getId(); |
81 | } |
82 | |
83 | /** |
84 | * Attempt to establish identity based on id if both exist. If either id |
85 | * does not exist use Object.equals(). |
86 | * |
87 | * @see java.lang.Object#equals(java.lang.Object) |
88 | */ |
89 | public boolean equals(Object other) { |
90 | if (other == this) { |
91 | return true; |
92 | } |
93 | if (other == null) { |
94 | return false; |
95 | } |
96 | if (!(other instanceof Entity)) { |
97 | return false; |
98 | } |
99 | Entity entity = (Entity) other; |
100 | if (id == null || entity.getId() == null) { |
101 | return false; |
102 | } |
103 | return id.equals(entity.getId()); |
104 | } |
105 | |
106 | /** |
107 | * Use ID if it exists to establish hash code, otherwise fall back to |
108 | * Object.hashCode(). Based on the same information as equals, so if that |
109 | * changes, this will. N.B. this follows the contract of Object.hashCode(), |
110 | * but will cause problems for anyone adding an unsaved {@link Entity} to a |
111 | * Set because Set.contains() will almost certainly return false for the |
112 | * {@link Entity} after it is saved. Spring Batch does not store any of its |
113 | * entities in Sets as a matter of course, so internally this is consistent. |
114 | * Clients should not be exposed to unsaved entities. |
115 | * |
116 | * @see java.lang.Object#hashCode() |
117 | */ |
118 | public int hashCode() { |
119 | if (id == null) { |
120 | return super.hashCode(); |
121 | } |
122 | return 39 + 87 * id.hashCode(); |
123 | } |
124 | |
125 | } |