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 | * Public setter for the version needed only by repository methods. |
69 | * @param version the version to set |
70 | */ |
71 | public void setVersion(Integer version) { |
72 | this.version = version; |
73 | } |
74 | |
75 | /** |
76 | * Increment the version number |
77 | */ |
78 | public void incrementVersion() { |
79 | if (version == null) { |
80 | version = 0; |
81 | } else { |
82 | version = version + 1; |
83 | } |
84 | } |
85 | |
86 | @Override |
87 | public String toString() { |
88 | return ClassUtils.getShortName(getClass()) + ": id=" + getId(); |
89 | } |
90 | |
91 | /** |
92 | * Attempt to establish identity based on id if both exist. If either id |
93 | * does not exist use Object.equals(). |
94 | * |
95 | * @see java.lang.Object#equals(java.lang.Object) |
96 | */ |
97 | @Override |
98 | public boolean equals(Object other) { |
99 | if (other == this) { |
100 | return true; |
101 | } |
102 | if (other == null) { |
103 | return false; |
104 | } |
105 | if (!(other instanceof Entity)) { |
106 | return false; |
107 | } |
108 | Entity entity = (Entity) other; |
109 | if (id == null || entity.getId() == null) { |
110 | return false; |
111 | } |
112 | return id.equals(entity.getId()); |
113 | } |
114 | |
115 | /** |
116 | * Use ID if it exists to establish hash code, otherwise fall back to |
117 | * Object.hashCode(). Based on the same information as equals, so if that |
118 | * changes, this will. N.B. this follows the contract of Object.hashCode(), |
119 | * but will cause problems for anyone adding an unsaved {@link Entity} to a |
120 | * Set because Set.contains() will almost certainly return false for the |
121 | * {@link Entity} after it is saved. Spring Batch does not store any of its |
122 | * entities in Sets as a matter of course, so internally this is consistent. |
123 | * Clients should not be exposed to unsaved entities. |
124 | * |
125 | * @see java.lang.Object#hashCode() |
126 | */ |
127 | public int hashCode() { |
128 | if (id == null) { |
129 | return super.hashCode(); |
130 | } |
131 | return 39 + 87 * id.hashCode(); |
132 | } |
133 | |
134 | } |