I have a custom annotation as follows.
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnApiVersionConditional.class)
public @interface ConditionalOnApiVersion {
int[] value() default 5;
String property();
}
OnApiVersionConditional is,
public class OnApiVersionConditional implements Condition {
@Override
public boolean matches(final ConditionContext context, final AnnotatedTypeMetadata metadata) {
final MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(ConditionalOnApiVersion.class.getName());
attributes.get("value");
final String inputVersion = context.getEnvironment().getProperty("userInputVersion");
}
In my Bean annotation,
@Bean
@ConditionalOnApiVersion(value = {6, 7}, property = "userInputVersion")
There are beans with single version matching also, like
@Bean
@ConditionalOnApiVersion(value = 8, property = "userInputVersion")
I would like to validate the userInput version from the property file to the available Beans supported versions. Not sure, how can i get the value, iterate and compare with userInoutVersion. The value could be 8 or {6,7} as an int array. Not sure, how can i iterate the value to check if any of the value is matching with the input version.
final List apiVersions = attributes.get("value").stream().collect(Collectors.toList());
Question:
How to iterate attributes.get("value") and compare with userInputVersion?
attributes.get("value") returns a List of Object.
I tried the below code,
final List<Object> apiVersions = attributes.get("value").stream().collect(Collectors.toList());
boolean result = apiVersions.stream().anyMatch(version -> (int)version == Integer.parseInt(userInputVersion));
But getting the below error int eh 2nd line anyMatch,
java.lang.ClassCastException: [I cannot be cast to java.lang.Integer
Thanks
code, please, not blockquote) and indicate which line in your source throws the exception.int[]. If you want to be flexible you'd want to pass aString[]. You're also not getting aListback, you're getting an array back.