/** * Reflection version - use when you need to get a few tags, and no caching is necessary. This * method is used by the caching version to populate the cache. * <p> * Helpful method for swizzling an Enum Tag into its possible value if that value is found in * the passed in Taggable parameter. This cuts down on a lot of duplicate code that we had in * each enum-type Tag. * <p> * * @param <T> * the type of enum tag we're parsing * @param tagType * the enum style tag that we want a possible value from * @param taggable * the source of tags and their values * @return an empty optional if the enum isn't a tag, doesn't have a key, the value isn't found * in taggable, or no enum value matches (ignoring case) the tag's value */ public static <T extends Enum<T>> Optional<T> fromAnnotation(final Class<T> tagType, final Taggable taggable) { if (tagType.getDeclaredAnnotation(Tag.class) != null) { return fromHelper(findTagNameIn(tagType), tagType, taggable); } return Optional.empty(); }
/** * Helpful method for swizzling an interface Tag that includes the contents of an enum tag * through the [with] annotation feature into its possible value if that value is found in the * passed in Taggable parameter and the enumType provided is actually listed in the [with] * attribute. * <p> * See the FromEnumTestCase class for an example of how to use this method * * @param <T> * the type of enum tag we're parsing * @param tagType * the interface tag with a [with] attribute that we want a possible value from * @param enumType * the return value type we want a value from * @param taggable * the source of tags and their values * @return an empty optional if the tagType isn't a tag, doesn't have a key, enumType is not * included in tagType's [with] list, the value isn't found in taggable, or no enum * value in enumType matches (ignoring case) the tag's value */ public static <T extends Enum<T>> Optional<T> from(final Class<?> tagType, final Class<T> enumType, final Taggable taggable) { final Tag tag = tagType.getDeclaredAnnotation(Tag.class); if (tag != null && Stream.of(tag.with()).anyMatch(possible -> possible == enumType)) { return fromHelper(findTagNameIn(tagType), enumType, taggable); } return Optional.empty(); }