|
21 | 21 |
|
22 | 22 | import org.springframework.core.annotation.AnnotationUtils;
|
23 | 23 | import org.springframework.core.log.LogAccessor;
|
24 |
| -import org.springframework.pulsar.annotation.PulsarTypeMapping; |
| 24 | +import org.springframework.pulsar.annotation.PulsarMessage; |
25 | 25 | import org.springframework.util.Assert;
|
26 | 26 |
|
27 | 27 | /**
|
28 |
| - * A registry that holds the {@link PulsarTypeMapping @PulsarTypeMapping} annotations and |
29 |
| - * each associated class that is marked with the annotation. |
| 28 | + * A registry that holds the {@link PulsarMessage @PulsarMessage} annotations and each |
| 29 | + * associated class that is marked with the annotation. |
30 | 30 | * <p>
|
31 | 31 | * The annotations are looked up on-demand and the result is cached.
|
32 | 32 | * <p>
|
33 |
| - * Once the cache reaches a {@link #maxNumberOfMappingsCached certain size} (default of |
| 33 | + * Once the cache reaches a {@link #maxNumberOfAnnotationsCached certain size} (default of |
34 | 34 | * {@link #DEFAULT_MAX_CACHE_SIZE}) it is cleared and the annotations will be looked up
|
35 | 35 | * again the next time they are requested.
|
36 | 36 | *
|
37 | 37 | * @author Chris Bono
|
38 | 38 | */
|
39 |
| -class PulsarTypeMappingRegistry { |
| 39 | +class PulsarMessageAnnotationRegistry { |
40 | 40 |
|
41 | 41 | private static final int DEFAULT_MAX_CACHE_SIZE = 1000;
|
42 | 42 |
|
43 |
| - private final int maxNumberOfMappingsCached; |
| 43 | + private final int maxNumberOfAnnotationsCached; |
44 | 44 |
|
45 | 45 | private final LogAccessor logger = new LogAccessor(this.getClass());
|
46 | 46 |
|
47 |
| - private ConcurrentHashMap<Class<?>, Optional<PulsarTypeMapping>> typeMappingsByClass = new ConcurrentHashMap<>(); |
| 47 | + private ConcurrentHashMap<Class<?>, Optional<PulsarMessage>> annotationsByClass = new ConcurrentHashMap<>(); |
48 | 48 |
|
49 |
| - PulsarTypeMappingRegistry() { |
| 49 | + PulsarMessageAnnotationRegistry() { |
50 | 50 | this(DEFAULT_MAX_CACHE_SIZE);
|
51 | 51 | }
|
52 | 52 |
|
53 |
| - PulsarTypeMappingRegistry(int maxNumberOfMappingsCached) { |
54 |
| - Assert.state(maxNumberOfMappingsCached > 0, "maxNumberOfMappingsCached must be > 0"); |
55 |
| - this.maxNumberOfMappingsCached = maxNumberOfMappingsCached; |
| 53 | + PulsarMessageAnnotationRegistry(int maxNumberOfAnnotationsCached) { |
| 54 | + Assert.state(maxNumberOfAnnotationsCached > 0, "maxNumberOfAnnotationsCached must be > 0"); |
| 55 | + this.maxNumberOfAnnotationsCached = maxNumberOfAnnotationsCached; |
56 | 56 | }
|
57 | 57 |
|
58 | 58 | /**
|
59 |
| - * Gets the {@link PulsarTypeMapping @PulsarTypeMapping} on the specified class or |
60 |
| - * empty if the class is not marked with the annotation. |
| 59 | + * Gets the {@link PulsarMessage @PulsarMessage} on the specified class or empty if |
| 60 | + * the class is not marked with the annotation. |
61 | 61 | * @param targetClass the class to check for the annotation
|
62 | 62 | * @return an optional containing the annotation or empty if the class is not marked
|
63 | 63 | * with the annotation.
|
64 | 64 | */
|
65 |
| - Optional<PulsarTypeMapping> getTypeMappingFor(Class<?> targetClass) { |
66 |
| - var optionalTypeMapping = this.typeMappingsByClass.computeIfAbsent(targetClass, this::findTypeMappingOn); |
67 |
| - if (this.typeMappingsByClass.size() > this.maxNumberOfMappingsCached) { |
| 65 | + Optional<PulsarMessage> getAnnotationFor(Class<?> targetClass) { |
| 66 | + var annotation = this.annotationsByClass.computeIfAbsent(targetClass, this::findAnnotationOn); |
| 67 | + if (this.annotationsByClass.size() > this.maxNumberOfAnnotationsCached) { |
68 | 68 | this.logger
|
69 |
| - .info(() -> "Clearing cache - max entries exceeded (%d)".formatted(this.maxNumberOfMappingsCached)); |
70 |
| - this.typeMappingsByClass = new ConcurrentHashMap<>(); |
| 69 | + .info(() -> "Clearing cache - max entries exceeded (%d)".formatted(this.maxNumberOfAnnotationsCached)); |
| 70 | + this.annotationsByClass = new ConcurrentHashMap<>(); |
71 | 71 | }
|
72 |
| - return optionalTypeMapping; |
| 72 | + return annotation; |
73 | 73 | }
|
74 | 74 |
|
75 | 75 | // VisibleForTesting
|
76 |
| - protected Optional<PulsarTypeMapping> findTypeMappingOn(Class<?> targetClass) { |
77 |
| - this.logger.debug(() -> "Looking for @PulsarTypeMapping on " + targetClass); |
78 |
| - PulsarTypeMapping annotation = AnnotationUtils.findAnnotation(targetClass, PulsarTypeMapping.class); |
| 76 | + protected Optional<PulsarMessage> findAnnotationOn(Class<?> targetClass) { |
| 77 | + this.logger.debug(() -> "Looking for @PulsarMessage on " + targetClass); |
| 78 | + PulsarMessage annotation = AnnotationUtils.findAnnotation(targetClass, PulsarMessage.class); |
79 | 79 | return Optional.ofNullable(annotation);
|
80 | 80 | }
|
81 | 81 |
|
|
0 commit comments