-
-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathObjectIdGenerator.java
More file actions
182 lines (159 loc) · 5.96 KB
/
ObjectIdGenerator.java
File metadata and controls
182 lines (159 loc) · 5.96 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.fasterxml.jackson.annotation;
import java.util.Objects;
/**
* Definition of API used for constructing Object Identifiers
* (as annotated using {@link JsonIdentityInfo}).
* Also defines factory methods used for creating instances
* for serialization, deserialization.
*
* @param <T> Type of Object Identifiers produced.
*/
@SuppressWarnings("serial")
public abstract class ObjectIdGenerator<T>
implements java.io.Serializable
{
/*
/**********************************************************
/* Accessors
/**********************************************************
*/
public abstract Class<?> getScope();
/**
* Method called to check whether this generator instance can
* be used for Object Ids of specific generator type and
* scope; determination is based by passing a configured
* "blueprint" (prototype) instance; from which the actual
* instances are created (using {@link #newForSerialization}).
*
* @return True if this instance can be used as-is; false if not
*/
public abstract boolean canUseFor(ObjectIdGenerator<?> gen);
/**
* Accessor that needs to be overridden to return <code>true</code>
* if the Object Id may be serialized as JSON Object; used by, for example,
* JSOG handling.
* The reason accessor is needed is that handling such Object Ids is
* more complex and may incur additional buffering or performance overhead,
* avoiding of which makes sense for common case of scalar object ids
* (or native object ids some formats support).
*<p>
* Default implementation returns <code>false</code>, so needs to be overridden
* by Object-producing generators.
*
* @since 2.5
*/
public boolean maySerializeAsObject() {
return false;
}
/**
* Accessor that may be called (after verifying (via {@link #maySerializeAsObject()})
* whether given name
*
* @param name Name of property to check
* @param parser Parser that points to property name, in case generator needs
* further verification (note: untyped, because <code>JsonParser</code> is defined
* in `jackson-core`, and this package does not depend on it).
*
* @since 2.5
*/
public boolean isValidReferencePropertyName(String name, Object parser) {
return false;
}
/*
/**********************************************************
/* Factory methods
/**********************************************************
*/
/**
* Factory method to create a blueprint instance for specified
* scope. Generators that do not use scope may return 'this'.
*/
public abstract ObjectIdGenerator<T> forScope(Class<?> scope);
/**
* Factory method called to create a new instance to use for
* serialization: needed since generators may have state
* (next id to produce).
*<p>
* Note that actual type of 'context' is
* <code>com.fasterxml.jackson.databind.SerializerProvider</code>,
* but can not be declared here as type itself (as well as call
* to this object) comes from databind package.
*
* @param context Serialization context object used (of type
* <code>com.fasterxml.jackson.databind.SerializerProvider</code>);
* may be needed by more complex generators to access contextual
* information such as configuration.
*/
public abstract ObjectIdGenerator<T> newForSerialization(Object context);
/**
* Method for constructing key to use for ObjectId-to-POJO maps.
*/
public abstract IdKey key(Object key);
/*
/**********************************************************
/* Methods for serialization
/**********************************************************
*/
/**
* Method used for generating a new Object Identifier to serialize
* for given POJO.
*
* @param forPojo POJO for which identifier is needed
*
* @return Object Identifier to use.
*/
public abstract T generateId(Object forPojo);
/*
/**********************************************************
/* Helper classes
/**********************************************************
*/
/**
* Simple key class that can be used as a key for
* ObjectId-to-POJO mappings, when multiple ObjectId types
* and scopes are used.
*/
public final static class IdKey
implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
/**
* Type of {@link ObjectIdGenerator} used for generating Object Id
*/
public final Class<?> type;
/**
* Scope of the Object Id (may be null, to denote global)
*/
public final Class<?> scope;
/**
* Object for which Object Id was generated: can NOT be null.
*/
public final Object key;
private final int hashCode;
public IdKey(Class<?> type, Class<?> scope, Object key) {
this.type = Objects.requireNonNull(type, "Type must not be null");
// Scope can be null
this.scope = scope;
this.key = Objects.requireNonNull(key, "Key must not be null");
hashCode = Objects.hashCode(key) + Objects.hashCode(type.getName())
^ Objects.hashCode(scope);
}
@Override
public int hashCode() { return hashCode; }
@Override
public boolean equals(Object o)
{
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != getClass()) return false;
IdKey other = (IdKey) o;
return (other.key.equals(key)) && (other.type == type) && (other.scope == scope);
}
@Override
public String toString() {
return String.format("[ObjectId: key=%s, type=%s, scope=%s]", key,
type.getName(),
(scope == null) ? "NONE" : scope.getName());
}
}
}