aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationCollector.java
blob: 940d435f7c7e9e08fa5347eba54b677c99e725b5 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.fasterxml.jackson.databind.introspect;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.fasterxml.jackson.databind.util.Annotations;

/**
 * Helper class used to collect annotations to be stored as
 * {@link com.fasterxml.jackson.databind.util.Annotations} (like {@link AnnotationMap}).
 *
 * @since 2.9
 */
public abstract class AnnotationCollector
{
    protected final static Annotations NO_ANNOTATIONS = new NoAnnotations();

    /**
     * Optional data to carry along
     */
    protected final Object _data;

    protected AnnotationCollector(Object d) {
        _data = d;
    }

    public static Annotations emptyAnnotations() { return NO_ANNOTATIONS; }

    public static AnnotationCollector emptyCollector() {
        return EmptyCollector.instance;
    }

    public static AnnotationCollector emptyCollector(Object data) {
        return new EmptyCollector(data);
    }

    public abstract Annotations asAnnotations();
    public abstract AnnotationMap asAnnotationMap();

    public Object getData() {
        return _data;
    }

    /*
    /**********************************************************
    /* API
    /**********************************************************
     */

    public abstract boolean isPresent(Annotation ann);

    public abstract AnnotationCollector addOrOverride(Annotation ann);

    /*
    /**********************************************************
    /* Collector implementations
    /**********************************************************
     */

    static class EmptyCollector extends AnnotationCollector
    {
        public final static EmptyCollector instance = new EmptyCollector(null);

        EmptyCollector(Object data) { super(data); }

        @Override
        public Annotations asAnnotations() {
            return NO_ANNOTATIONS;
        }
 
        @Override
        public AnnotationMap asAnnotationMap() {
            return new AnnotationMap();
        }

        @Override
        public boolean isPresent(Annotation ann) { return false; }

        @Override
        public AnnotationCollector addOrOverride(Annotation ann) {
            return new OneCollector(_data, ann.annotationType(), ann);
        }
    }

    static class OneCollector extends AnnotationCollector
    {
        private Class<?> _type;
        private Annotation _value;

        public OneCollector(Object data,
                Class<?> type, Annotation value) {
            super(data);
            _type = type;
            _value = value;
        }

        @Override
        public Annotations asAnnotations() {
            return new OneAnnotation(_type, _value);
        }

        @Override
        public AnnotationMap asAnnotationMap() {
            return AnnotationMap.of(_type, _value);
        }

        @Override
        public boolean isPresent(Annotation ann) {
            return ann.annotationType() == _type;
        }
        
        @Override
        public AnnotationCollector addOrOverride(Annotation ann) {
            final Class<?> type = ann.annotationType();
            // true override? Just replace in-place, return
            if (_type == type) {
                _value = ann;
                return this;
            }
            return new NCollector(_data, _type, _value, type, ann);
        }
    }

    static class NCollector extends AnnotationCollector
    {
        protected final HashMap<Class<?>,Annotation> _annotations;

        public NCollector(Object data,
                Class<?> type1, Annotation value1,
                Class<?> type2, Annotation value2) {
            super(data);
            _annotations = new HashMap<>();
            _annotations.put(type1, value1);
            _annotations.put(type2, value2);
        }

        @Override
        public Annotations asAnnotations() {
            if (_annotations.size() == 2) {
                Iterator<Map.Entry<Class<?>,Annotation>> it = _annotations.entrySet().iterator();
                Map.Entry<Class<?>,Annotation> en1 = it.next(), en2 = it.next();
                return new TwoAnnotations(en1.getKey(), en1.getValue(),
                        en2.getKey(), en2.getValue());
            }
            return new AnnotationMap(_annotations);
        }

        @Override
        public AnnotationMap asAnnotationMap() {
            AnnotationMap result = new AnnotationMap();
            for (Annotation ann : _annotations.values()) {
                result.add(ann);
            }
            return result;
        }

        @Override
        public boolean isPresent(Annotation ann) {
            return _annotations.containsKey(ann.annotationType());
        }

        @Override
        public AnnotationCollector addOrOverride(Annotation ann) {
            _annotations.put(ann.annotationType(), ann);
            return this;
        }
    }

    /*
    /**********************************************************
    /* Annotations implementations
    /**********************************************************
     */

    /**
     * Immutable implementation for case where no annotations are associated with
     * an annotatable entity.
     *
     * @since 2.9
     */
    public static class NoAnnotations
        implements Annotations, java.io.Serializable
    {
        private static final long serialVersionUID = 1L;

        NoAnnotations() { }

        @Override
        public <A extends Annotation> A get(Class<A> cls) {
            return null;
        }

        @Override
        public boolean has(Class<?> cls) {
            return false;
        }

        @Override
        public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
            return false;
        }

        @Override
        public int size() {
            return 0;
        }
    }

    public static class OneAnnotation
        implements Annotations, java.io.Serializable
    {
        private static final long serialVersionUID = 1L;

        private final Class<?> _type;
        private final Annotation _value;

        public OneAnnotation(Class<?> type, Annotation value) {
            _type = type;
            _value = value;
        }

        @SuppressWarnings("unchecked")
        @Override
        public <A extends Annotation> A get(Class<A> cls) {
            if (_type == cls) {
                return (A) _value;
            }
            return null;
        }

        @Override
        public boolean has(Class<?> cls) {
            return (_type == cls);
        }

        @Override
        public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
            for (Class<?> cls : annoClasses) {
                if (cls == _type) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public int size() {
            return 1;
        }
    }

    public static class TwoAnnotations
        implements Annotations, java.io.Serializable
    {
        private static final long serialVersionUID = 1L;
    
        private final Class<?> _type1, _type2;
        private final Annotation _value1, _value2;
    
        public TwoAnnotations(Class<?> type1, Annotation value1,
                Class<?> type2, Annotation value2) {
            _type1 = type1;
            _value1 = value1;
            _type2 = type2;
            _value2 = value2;
        }

        @SuppressWarnings("unchecked")
        @Override
        public <A extends Annotation> A get(Class<A> cls) {
            if (_type1 == cls) {
                return (A) _value1;
            }
            if (_type2 == cls) {
                return (A) _value2;
            }
            return null;
        }

        @Override
        public boolean has(Class<?> cls) {
            return (_type1 == cls) || (_type2 == cls);
        }

        @Override
        public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
            for (Class<?> cls : annoClasses) {
                if ((cls == _type1) || (cls == _type2)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public int size() {
            return 2;
        }
    }
}