|
| 1 | +package de.exlll.configlib; |
| 2 | + |
| 3 | +import de.exlll.configlib.annotation.Comment; |
| 4 | + |
| 5 | +import java.lang.reflect.AnnotatedElement; |
| 6 | +import java.lang.reflect.Field; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import static java.util.stream.Collectors.toMap; |
| 13 | + |
| 14 | +/** |
| 15 | + * Instances of this class contain all comments of a {@link Configuration} class |
| 16 | + * and its fields. |
| 17 | + */ |
| 18 | +public final class Comments { |
| 19 | + private final List<String> classComments; |
| 20 | + private final Map<String, List<String>> fieldComments; |
| 21 | + |
| 22 | + private Comments(List<String> classComments, |
| 23 | + Map<String, List<String>> fieldComments) { |
| 24 | + this.classComments = classComments; |
| 25 | + this.fieldComments = fieldComments; |
| 26 | + } |
| 27 | + |
| 28 | + static Comments ofClass(Class<?> cls) { |
| 29 | + List<String> classComments = getComments(cls); |
| 30 | + Map<String, List<String>> fieldComments = Arrays |
| 31 | + .stream(cls.getDeclaredFields()) |
| 32 | + .filter(Comments::isCommented) |
| 33 | + .collect(toMap(Field::getName, Comments::getComments)); |
| 34 | + return new Comments(classComments, fieldComments); |
| 35 | + } |
| 36 | + |
| 37 | + private static boolean isCommented(AnnotatedElement element) { |
| 38 | + return element.isAnnotationPresent(Comment.class); |
| 39 | + } |
| 40 | + |
| 41 | + private static List<String> getComments(AnnotatedElement element) { |
| 42 | + Comment comment = element.getAnnotation(Comment.class); |
| 43 | + return (comment != null) |
| 44 | + ? Arrays.asList(comment.value()) |
| 45 | + : Collections.emptyList(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns if the {@code Configuration} this {@code Comments} object belongs to |
| 50 | + * has class comments. |
| 51 | + * |
| 52 | + * @return true, if {@code Configuration} has class comments. |
| 53 | + */ |
| 54 | + public boolean hasClassComments() { |
| 55 | + return !classComments.isEmpty(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns if the {@code Configuration} this {@code Comments} object belongs to |
| 60 | + * has field comments. |
| 61 | + * |
| 62 | + * @return true, if {@code Configuration} has field comments. |
| 63 | + */ |
| 64 | + public boolean hasFieldComments() { |
| 65 | + return !fieldComments.isEmpty(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns a list of class comments. |
| 70 | + * |
| 71 | + * @return list of class comments |
| 72 | + */ |
| 73 | + public List<String> getClassComments() { |
| 74 | + return classComments; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns lists of field comments mapped by field name. |
| 79 | + * |
| 80 | + * @return lists of field comments by field name |
| 81 | + */ |
| 82 | + public Map<String, List<String>> getFieldComments() { |
| 83 | + return fieldComments; |
| 84 | + } |
| 85 | +} |
0 commit comments