/
AbstractAnnotationBasedJsonParsableFactory.java
65 lines (58 loc) · 2.76 KB
/
AbstractAnnotationBasedJsonParsableFactory.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
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.signals.base;
import java.lang.annotation.Annotation;
/**
* Responsible to build an {@link AnnotationBasedJsonParsable<T>} from a given annotation.
*
* @param <T> The superclass of the class that should be parsed by an annotation based json parsable created by this
* factory.
* @param <A> The type of the annotation that holds the information to build an annotation based json parsable.
*/
public abstract class AbstractAnnotationBasedJsonParsableFactory<T, A extends Annotation> {
/**
* Builds an {@link AnnotationBasedJsonParsable<T>} from the given annotation.
*
* @param annotation the annotation that holds the information to build an annotation based json parsable.
* @param classToParse the class that should be deserialized.
* @return the annotation based json parsable.
*/
AnnotationBasedJsonParsable<T> fromAnnotation(final A annotation, final Class<? extends T> classToParse) {
final String methodName = getMethodNameFor(annotation);
final String key = getKeyFor(annotation);
final String v1FallbackKey = getV1FallbackKeyFor(annotation);
return new AnnotationBasedJsonParsable<>(key, v1FallbackKey, classToParse, methodName);
}
/**
* The fallback key for API v1 deserialization strategies. If there is no fallback required this method returns
* the same as {@link #getKeyFor(java.lang.annotation.Annotation)}.
*
* @param annotation the annotation that holds the information to build an annotation based json parsable.
* @return the fallback key for v1 deserialization strategies or the v2 key if no fallback is required.
*/
protected abstract String getV1FallbackKeyFor(A annotation);
/**
* The key for v2 deserialization strategies.
*
* @param annotation the annotation that holds the information to build an annotation based json parsable.
* @return the key for v2 deserialization strategies.
*/
protected abstract String getKeyFor(A annotation);
/**
* The name of the method used for deserialization.
*
* @param annotation the annotation that holds the information to build an annotation based json parsable.
* @return the name of the method used for deserialization.
*/
protected abstract String getMethodNameFor(A annotation);
}