0
# Strategic Pattern Implementations
1
2
Core behavioral interfaces and strategy implementations that enable JAXB2 Basics plugins to generate efficient, reflection-free method implementations for equals, hashCode, toString, copy, and merge operations on JAXB-generated classes.
3
4
## Capabilities
5
6
### Behavioral Interfaces
7
8
The main interfaces that JAXB-generated classes implement to provide strategic pattern support.
9
10
#### Equals Functionality
11
12
```java { .api }
13
interface Equals2 {
14
boolean equals(ObjectLocator thisLocator, ObjectLocator thatLocator,
15
Object object, EqualsStrategy2 strategy);
16
}
17
18
// Deprecated legacy interface
19
interface Equals {
20
boolean equals(ObjectLocator thisLocator, ObjectLocator thatLocator,
21
Object object, EqualsStrategy strategy);
22
}
23
```
24
25
#### HashCode Functionality
26
27
```java { .api }
28
interface HashCode2 {
29
int hashCode(ObjectLocator locator, HashCodeStrategy2 strategy);
30
}
31
32
// Deprecated legacy interface
33
interface HashCode {
34
int hashCode(ObjectLocator locator, HashCodeStrategy strategy);
35
}
36
```
37
38
#### ToString Functionality
39
40
```java { .api }
41
interface ToString2 {
42
StringBuilder append(ObjectLocator locator, StringBuilder buffer,
43
ToStringStrategy2 strategy);
44
StringBuilder appendFields(ObjectLocator locator, StringBuilder buffer,
45
ToStringStrategy2 strategy);
46
}
47
48
// Deprecated legacy interface
49
interface ToString {
50
StringBuilder append(ObjectLocator locator, StringBuilder buffer,
51
ToStringStrategy strategy);
52
StringBuilder appendFields(ObjectLocator locator, StringBuilder buffer,
53
ToStringStrategy strategy);
54
}
55
```
56
57
#### Copy Functionality
58
59
```java { .api }
60
interface CopyTo2 {
61
Object createNewInstance();
62
Object copyTo(Object target);
63
Object copyTo(ObjectLocator locator, Object target, CopyStrategy2 strategy);
64
}
65
66
// Deprecated legacy interface
67
interface CopyTo {
68
Object createNewInstance();
69
Object copyTo(Object target);
70
Object copyTo(ObjectLocator locator, Object target, CopyStrategy strategy);
71
}
72
```
73
74
#### Merge Functionality
75
76
```java { .api }
77
interface MergeFrom2 {
78
Object createNewInstance();
79
void mergeFrom(Object left, Object right);
80
void mergeFrom(ObjectLocator leftLocator, ObjectLocator rightLocator,
81
Object left, Object right, MergeStrategy2 strategy);
82
}
83
84
// Deprecated legacy interface
85
interface MergeFrom {
86
Object createNewInstance();
87
void mergeFrom(Object left, Object right);
88
void mergeFrom(ObjectLocator leftLocator, ObjectLocator rightLocator,
89
Object left, Object right, MergeStrategy strategy);
90
}
91
```
92
93
### Strategy Interfaces
94
95
Strategy interfaces define how operations are performed on different data types. The "2" versions include value set tracking for handling optional/nullable fields.
96
97
#### Equals Strategies
98
99
```java { .api }
100
interface EqualsStrategy2 {
101
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
102
boolean left, boolean right, boolean leftSet, boolean rightSet);
103
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
104
byte left, byte right, boolean leftSet, boolean rightSet);
105
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
106
char left, char right, boolean leftSet, boolean rightSet);
107
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
108
double left, double right, boolean leftSet, boolean rightSet);
109
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
110
float left, float right, boolean leftSet, boolean rightSet);
111
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
112
int left, int right, boolean leftSet, boolean rightSet);
113
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
114
long left, long right, boolean leftSet, boolean rightSet);
115
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
116
short left, short right, boolean leftSet, boolean rightSet);
117
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
118
Object left, Object right, boolean leftSet, boolean rightSet);
119
// Array comparison methods for all primitive types and Object arrays
120
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
121
boolean[] left, boolean[] right, boolean leftSet, boolean rightSet);
122
// ... additional array methods for byte[], char[], double[], float[], int[], long[], short[], Object[]
123
}
124
125
// Deprecated legacy interface without value set tracking
126
interface EqualsStrategy {
127
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
128
boolean left, boolean right);
129
boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
130
Object left, Object right);
131
// ... methods for all primitive types and arrays
132
}
133
```
134
135
#### HashCode Strategies
136
137
```java { .api }
138
interface HashCodeStrategy2 {
139
int hashCode(ObjectLocator locator, int hashCode, boolean value, boolean valueSet);
140
int hashCode(ObjectLocator locator, int hashCode, byte value, boolean valueSet);
141
int hashCode(ObjectLocator locator, int hashCode, char value, boolean valueSet);
142
int hashCode(ObjectLocator locator, int hashCode, double value, boolean valueSet);
143
int hashCode(ObjectLocator locator, int hashCode, float value, boolean valueSet);
144
int hashCode(ObjectLocator locator, int hashCode, int value, boolean valueSet);
145
int hashCode(ObjectLocator locator, int hashCode, long value, boolean valueSet);
146
int hashCode(ObjectLocator locator, int hashCode, short value, boolean valueSet);
147
int hashCode(ObjectLocator locator, int hashCode, Object value, boolean valueSet);
148
// Array hash code methods for all primitive types and Object arrays
149
int hashCode(ObjectLocator locator, int hashCode, boolean[] value, boolean valueSet);
150
// ... additional array methods
151
}
152
153
// Deprecated legacy interface
154
interface HashCodeStrategy {
155
int hashCode(ObjectLocator locator, int hashCode, boolean value);
156
int hashCode(ObjectLocator locator, int hashCode, Object value);
157
// ... methods for all primitive types and arrays
158
}
159
```
160
161
#### ToString Strategies
162
163
```java { .api }
164
interface ToStringStrategy2 {
165
StringBuilder appendStart(ObjectLocator locator, Object object, StringBuilder buffer);
166
StringBuilder appendEnd(ObjectLocator locator, Object object, StringBuilder buffer);
167
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
168
StringBuilder buffer, boolean value, boolean valueSet);
169
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
170
StringBuilder buffer, byte value, boolean valueSet);
171
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
172
StringBuilder buffer, char value, boolean valueSet);
173
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
174
StringBuilder buffer, double value, boolean valueSet);
175
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
176
StringBuilder buffer, float value, boolean valueSet);
177
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
178
StringBuilder buffer, int value, boolean valueSet);
179
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
180
StringBuilder buffer, long value, boolean valueSet);
181
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
182
StringBuilder buffer, short value, boolean valueSet);
183
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
184
StringBuilder buffer, Object value, boolean valueSet);
185
// Array field methods for all primitive types and Object arrays
186
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
187
StringBuilder buffer, boolean[] value, boolean valueSet);
188
// ... additional array field methods
189
}
190
191
// Deprecated legacy interface
192
interface ToStringStrategy {
193
StringBuilder appendStart(ObjectLocator locator, Object object, StringBuilder buffer);
194
StringBuilder appendEnd(ObjectLocator locator, Object object, StringBuilder buffer);
195
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
196
StringBuilder buffer, boolean value);
197
StringBuilder appendField(ObjectLocator locator, Object object, String fieldName,
198
StringBuilder buffer, Object value);
199
// ... methods for all primitive types and arrays
200
}
201
```
202
203
#### Copy Strategies
204
205
```java { .api }
206
interface CopyStrategy2 {
207
Boolean shouldBeCopiedAndSet(ObjectLocator locator, boolean valueSet);
208
boolean copy(ObjectLocator locator, boolean value, boolean valueSet);
209
byte copy(ObjectLocator locator, byte value, boolean valueSet);
210
char copy(ObjectLocator locator, char value, boolean valueSet);
211
double copy(ObjectLocator locator, double value, boolean valueSet);
212
float copy(ObjectLocator locator, float value, boolean valueSet);
213
int copy(ObjectLocator locator, int value, boolean valueSet);
214
long copy(ObjectLocator locator, long value, boolean valueSet);
215
short copy(ObjectLocator locator, short value, boolean valueSet);
216
Object copy(ObjectLocator locator, Object value, boolean valueSet);
217
// Array copy methods for all primitive types and Object arrays
218
boolean[] copy(ObjectLocator locator, boolean[] value, boolean valueSet);
219
// ... additional array copy methods
220
}
221
222
// Deprecated legacy interface
223
interface CopyStrategy {
224
boolean copy(ObjectLocator locator, boolean value);
225
Object copy(ObjectLocator locator, Object value);
226
// ... methods for all primitive types and arrays
227
}
228
```
229
230
#### Merge Strategies
231
232
```java { .api }
233
interface MergeStrategy2 {
234
Boolean shouldBeMergedAndSet(ObjectLocator leftLocator, ObjectLocator rightLocator,
235
boolean leftSet, boolean rightSet);
236
boolean merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
237
boolean left, boolean right, boolean leftSet, boolean rightSet);
238
byte merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
239
byte left, byte right, boolean leftSet, boolean rightSet);
240
char merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
241
char left, char right, boolean leftSet, boolean rightSet);
242
double merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
243
double left, double right, boolean leftSet, boolean rightSet);
244
float merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
245
float left, float right, boolean leftSet, boolean rightSet);
246
int merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
247
int left, int right, boolean leftSet, boolean rightSet);
248
long merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
249
long left, long right, boolean leftSet, boolean rightSet);
250
short merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
251
short left, short right, boolean leftSet, boolean rightSet);
252
Object merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
253
Object left, Object right, boolean leftSet, boolean rightSet);
254
// Array merge methods for all primitive types and Object arrays
255
boolean[] merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
256
boolean[] left, boolean[] right, boolean leftSet, boolean rightSet);
257
// ... additional array merge methods
258
}
259
260
// Deprecated legacy interface
261
interface MergeStrategy {
262
boolean merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
263
boolean left, boolean right);
264
Object merge(ObjectLocator leftLocator, ObjectLocator rightLocator,
265
Object left, Object right);
266
// ... methods for all primitive types and arrays
267
}
268
```
269
270
### Default Implementations
271
272
Standard implementations that provide sensible default behavior for all strategy interfaces.
273
274
```java { .api }
275
class DefaultEqualsStrategy implements EqualsStrategy2, EqualsStrategy {
276
public static final DefaultEqualsStrategy INSTANCE = new DefaultEqualsStrategy();
277
// Implements deep equals comparison for objects and arrays
278
// Delegates to Equals/Equals2 interfaces when available
279
}
280
281
class DefaultHashCodeStrategy implements HashCodeStrategy2, HashCodeStrategy {
282
public DefaultHashCodeStrategy();
283
public DefaultHashCodeStrategy(int multiplierNonZeroOddNumber);
284
public static final DefaultHashCodeStrategy INSTANCE = new DefaultHashCodeStrategy();
285
// Implements hash code calculation with configurable multiplier
286
// Delegates to HashCode/HashCode2 interfaces when available
287
}
288
289
class DefaultToStringStrategy implements ToStringStrategy2, ToStringStrategy {
290
public DefaultToStringStrategy();
291
// Constructor with extensive configuration options for field names,
292
// class names, separators, delimiters, etc.
293
public static final DefaultToStringStrategy INSTANCE = new DefaultToStringStrategy();
294
// Implements configurable string representation
295
// Delegates to ToString/ToString2 interfaces when available
296
}
297
298
class DefaultCopyStrategy implements CopyStrategy2, CopyStrategy {
299
public static final DefaultCopyStrategy INSTANCE = new DefaultCopyStrategy();
300
// Implements object copying with support for Cloneable objects
301
// Delegates to CopyTo/CopyTo2 interfaces when available
302
// Provides array copying support
303
}
304
305
class DefaultMergeStrategy implements MergeStrategy2, MergeStrategy {
306
public static final DefaultMergeStrategy INSTANCE = new DefaultMergeStrategy();
307
// Implements null-safe merging logic
308
// Delegates to MergeFrom/MergeFrom2 interfaces when available
309
}
310
```
311
312
### JAXB-Specific Implementations
313
314
Specialized implementations that provide XML-aware behavior for JAXB types like JAXBElement and Collections.
315
316
```java { .api }
317
class JAXBEqualsStrategy extends DefaultEqualsStrategy {
318
public static final JAXBEqualsStrategy INSTANCE = new JAXBEqualsStrategy();
319
// Special handling for JAXBElement comparison (name, declaredType, scope, value)
320
// Iterator-based List comparison for consistent ordering
321
}
322
323
class JAXBHashCodeStrategy extends DefaultHashCodeStrategy {
324
public static final JAXBHashCodeStrategy INSTANCE = new JAXBHashCodeStrategy();
325
// JAXBElement hash code includes name, declaredType, scope, and value
326
// Special List handling (empty lists treated as null)
327
}
328
329
class JAXBToStringStrategy extends DefaultToStringStrategy {
330
public static final JAXBToStringStrategy INSTANCE = new JAXBToStringStrategy();
331
// XML-like formatting for JAXBElement with configurable markers
332
// Special formatting for JAXB-generated classes
333
}
334
335
class JAXBCopyStrategy extends DefaultCopyStrategy {
336
public static final JAXBCopyStrategy INSTANCE = new JAXBCopyStrategy();
337
// DOM Node deep cloning support
338
// JAXBElement copying with proper value copying
339
// ArrayList copying for List types
340
}
341
342
class JAXBMergeStrategy extends DefaultMergeStrategy {
343
public static final JAXBMergeStrategy INSTANCE = new JAXBMergeStrategy();
344
// Collection merging (non-empty collection takes precedence)
345
}
346
347
class JAXBMergeCollectionsStrategy extends JAXBMergeStrategy {
348
public static final JAXBMergeCollectionsStrategy INSTANCE = new JAXBMergeCollectionsStrategy();
349
// Advanced collection merging:
350
// - List merging (concatenation)
351
// - Set merging (union)
352
}
353
```
354
355
### Supporting Types
356
357
```java { .api }
358
interface EnumValue<T> {
359
T enumValue();
360
}
361
```
362
363
## Usage Examples
364
365
### Basic Strategic Implementation
366
367
```java
368
// JAXB-generated class implementing strategic patterns
369
public class Customer implements Equals2, HashCode2, ToString2 {
370
private String name;
371
private String email;
372
private boolean active;
373
374
@Override
375
public boolean equals(ObjectLocator thisLocator, ObjectLocator thatLocator,
376
Object object, EqualsStrategy2 strategy) {
377
if (!(object instanceof Customer)) {
378
return false;
379
}
380
Customer that = (Customer) object;
381
return strategy.equals(LocatorUtils.property(thisLocator, "name", this.name),
382
LocatorUtils.property(thatLocator, "name", that.name),
383
this.name, that.name, (this.name != null), (that.name != null)) &&
384
strategy.equals(LocatorUtils.property(thisLocator, "email", this.email),
385
LocatorUtils.property(thatLocator, "email", that.email),
386
this.email, that.email, (this.email != null), (that.email != null)) &&
387
strategy.equals(LocatorUtils.property(thisLocator, "active", this.active),
388
LocatorUtils.property(thatLocator, "active", that.active),
389
this.active, that.active, true, true);
390
}
391
392
@Override
393
public int hashCode(ObjectLocator locator, HashCodeStrategy2 strategy) {
394
int hashCode = 1;
395
hashCode = strategy.hashCode(LocatorUtils.property(locator, "name", this.name),
396
hashCode, this.name, (this.name != null));
397
hashCode = strategy.hashCode(LocatorUtils.property(locator, "email", this.email),
398
hashCode, this.email, (this.email != null));
399
hashCode = strategy.hashCode(LocatorUtils.property(locator, "active", this.active),
400
hashCode, this.active, true);
401
return hashCode;
402
}
403
404
// Standard method implementations using JAXB strategies
405
@Override
406
public boolean equals(Object object) {
407
return equals(new DefaultRootObjectLocator(this),
408
new DefaultRootObjectLocator(object),
409
object, JAXBEqualsStrategy.INSTANCE);
410
}
411
412
@Override
413
public int hashCode() {
414
return hashCode(new DefaultRootObjectLocator(this), JAXBHashCodeStrategy.INSTANCE);
415
}
416
}
417
```
418
419
### Custom Strategy Implementation
420
421
```java
422
// Custom strategy for case-insensitive string comparison
423
public class CaseInsensitiveEqualsStrategy extends DefaultEqualsStrategy {
424
@Override
425
public boolean equals(ObjectLocator leftLocator, ObjectLocator rightLocator,
426
Object left, Object right, boolean leftSet, boolean rightSet) {
427
if (!leftSet && !rightSet) return true;
428
if (!leftSet || !rightSet) return false;
429
430
if (left instanceof String && right instanceof String) {
431
return ((String) left).equalsIgnoreCase((String) right);
432
}
433
return super.equals(leftLocator, rightLocator, left, right, leftSet, rightSet);
434
}
435
}
436
437
// Usage with custom strategy
438
Customer customer1 = new Customer();
439
customer1.setName("John");
440
441
Customer customer2 = new Customer();
442
customer2.setName("JOHN");
443
444
boolean isEqual = customer1.equals(new DefaultRootObjectLocator(customer1),
445
new DefaultRootObjectLocator(customer2),
446
customer2, new CaseInsensitiveEqualsStrategy());
447
// Returns true with case-insensitive strategy
448
```