-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Entity.java
157 lines (139 loc) · 3.9 KB
/
Entity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright 2006-2022 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.batch.core;
import java.io.Serializable;
import org.springframework.util.ClassUtils;
/**
* Batch Domain Entity class. Any class that should be uniquely identifiable
* from another should subclass from Entity. See Domain
* Driven Design, by Eric Evans, for more information on this pattern
* and the difference between Entities and Value Objects.
*
* @author Lucas Ward
* @author Dave Syer
*
*/
@SuppressWarnings("serial")
public class Entity implements Serializable {
private Long id;
private volatile Integer version;
/**
* Default constructor for {@link Entity}.
*
* The ID defaults to zero.
*/
public Entity() {
super();
}
/**
* The constructor for the {@link Entity} where the ID is established.
*
* @param id The ID for the entity.
*/
public Entity(Long id) {
super();
//Commented out because StepExecutions are still created in a disconnected
//manner. The Repository should create them, then this can be uncommented.
//Assert.notNull(id, "Entity id must not be null.");
this.id = id;
}
/**
* @return The ID associated with the {@link Entity}.
*/
public Long getId() {
return id;
}
/**
* @param id The ID for the {@link Entity}.
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the version.
*/
public Integer getVersion() {
return version;
}
/**
* Public setter for the version. Needed only by repository methods.
* @param version The version to set.
*/
public void setVersion(Integer version) {
this.version = version;
}
/**
* Increment the version number.
*/
public void incrementVersion() {
if (version == null) {
version = 0;
} else {
version = version + 1;
}
}
/**
* Creates a string representation of the {@code Entity},
* including the {@code id}, {@code version}, and class name.
*/
@Override
public String toString() {
return String.format("%s: id=%d, version=%d", ClassUtils.getShortName(getClass()), id, version);
}
/**
* Attempt to establish identity based on {@code id} if both exist. If either {@code id}
* does not exist, use {@code Object.equals()}.
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other == null) {
return false;
}
if (!(other instanceof Entity)) {
return false;
}
Entity entity = (Entity) other;
if (id == null || entity.getId() == null) {
return false;
}
return id.equals(entity.getId());
}
/**
* Use {@code id}, if it exists, to establish a hash code. Otherwise fall back to
* {@code Object.hashCode()}. It is based on the
* same information as {@code equals}, so, if that
* changes, this will. Note that this follows the contract of {@code Object.hashCode()}
* but will cause problems for anyone adding an unsaved {@link Entity} to a
* {@code Set} because {@code Set.contains()} almost certainly returns false for the
* {@link Entity} after it is saved. Spring Batch does not store any of its
* entities in sets as a matter of course, so this is internally consistent.
* Clients should not be exposed to unsaved entities.
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
if (id == null) {
return super.hashCode();
}
return 39 + 87 * id.hashCode();
}
}