@Override public <BT extends BeanType<T>> Stream<BT> reduce(Class<T> beanType, Stream<BT> candidates) { check("beanType", beanType).notNull(); check("candidates", candidates).notNull(); return candidates.filter(candidate -> { if (!beanType.isAssignableFrom(candidate.getBeanType())) { return false; } String typeName; AnnotationMetadata annotationMetadata = candidate.getAnnotationMetadata(); // here we resolved the declared Qualifier of the bean Optional<String> beanQualifier = annotationMetadata.findDeclaredAnnotation(Named.class).flatMap(namedAnnotationValue -> namedAnnotationValue.getValue(String.class)); typeName = beanQualifier.orElseGet(() -> { if (candidate instanceof NameResolver) { Optional<String> resolvedName = ((NameResolver) candidate).resolveName(); return resolvedName.orElse(candidate.getBeanType().getSimpleName()); } return candidate.getBeanType().getSimpleName(); }); return typeName.equalsIgnoreCase(name) || typeName.equalsIgnoreCase(name + beanType.getSimpleName()); } ); }
/** * Construct a {@link UserExecutorConfiguration} for the given {@link io.micronaut.scheduling.executor.ExecutorType}. * * @param type The type * @param num The number of threads for {@link io.micronaut.scheduling.executor.ExecutorType#FIXED} or the parallelism for * {@link io.micronaut.scheduling.executor.ExecutorType#WORK_STEALING} or the core pool size for {@link io.micronaut.scheduling.executor.ExecutorType#SCHEDULED} * @return The configuration */ public static UserExecutorConfiguration of(ExecutorType type, int num) { ArgumentUtils.check("type", type).notNull(); UserExecutorConfiguration configuration = of(type); configuration.type = type; switch (type) { case FIXED: configuration.nThreads = num; break; case SCHEDULED: configuration.corePoolSize = num; break; case WORK_STEALING: configuration.parallelism = num; break; default: } return configuration; }
@Override public <BT extends BeanType<T>> Stream<BT> reduce(Class<T> beanType, Stream<BT> candidates) { check("beanType", beanType).notNull(); check("candidates", candidates).notNull(); return candidates.filter(candidate -> { if (!beanType.isAssignableFrom(candidate.getBeanType())) { return false; } String typeName; AnnotationMetadata annotationMetadata = candidate.getAnnotationMetadata(); // here we resolved the declared Qualifier of the bean Optional<String> beanQualifier = annotationMetadata.findDeclaredAnnotation(Named.class).flatMap(namedAnnotationValue -> namedAnnotationValue.getValue(String.class)); typeName = beanQualifier.orElseGet(() -> { if (candidate instanceof NameResolver) { Optional<String> resolvedName = ((NameResolver) candidate).resolveName(); return resolvedName.orElse(candidate.getBeanType().getSimpleName()); } return candidate.getBeanType().getSimpleName(); }); return typeName.equalsIgnoreCase(name) || typeName.equalsIgnoreCase(name + beanType.getSimpleName()); } ); }
/** * Constructs a new event for the given data. * * @param event The event * @param data The data * @param <ET> The data type * @return The event instance */ static <ET> Event<ET> of(Event event, ET data) { ArgumentUtils.check("data", data).notNull(); return new DefaultEvent<>(data) .id(event.getId()) .comment(event.getComment()) .name(event.getName()) .retry(event.getRetry()); } }
/** * Construct a {@link UserExecutorConfiguration} for the given {@link io.micronaut.scheduling.executor.ExecutorType}. * * @param type The type * @return The configuration */ public static UserExecutorConfiguration of(ExecutorType type) { ArgumentUtils.check("type", type).notNull(); UserExecutorConfiguration configuration = new UserExecutorConfiguration(); configuration.type = type; return configuration; }
@Override public ScheduledFuture<?> scheduleAtFixedRate(Duration initialDelay, Duration period, Runnable command) { check("period", period).notNull(); check("command", command).notNull(); long initialDelayMillis = initialDelay != null ? initialDelay.toMillis() : 0; return executorService.scheduleAtFixedRate( command, initialDelayMillis, period.toMillis(), TimeUnit.MILLISECONDS ); }
@Override public ScheduledFuture<?> scheduleWithFixedDelay(Duration initialDelay, Duration delay, Runnable command) { check("delay", delay).notNull(); check("command", command).notNull(); long initialDelayMillis = initialDelay != null ? initialDelay.toMillis() : 0; return executorService.scheduleWithFixedDelay( command, initialDelayMillis, delay.toMillis(), TimeUnit.MILLISECONDS ); }
/** * Constructs a new event for the given data. * * @param data The data * @param <ET> The data type * @return The event instance */ static <ET> Event<ET> of(ET data) { ArgumentUtils.check("data", data).notNull(); return new DefaultEvent<>(data); }
@Override public <V> ScheduledFuture<V> schedule(String cron, Callable<V> command) { if (StringUtils.isEmpty(cron)) { throw new IllegalArgumentException("Blank cron expression not allowed"); } check("command", command).notNull(); Supplier<Duration> delaySupplier = buildCronDelaySupplier(cron); return new ReschedulingTask<>(command, this, delaySupplier); }
@Override public ScheduledFuture<?> schedule(String cron, Runnable command) { if (StringUtils.isEmpty(cron)) { throw new IllegalArgumentException("Blank cron expression not allowed"); } check("command", command).notNull(); Supplier<Duration> delaySupplier = buildCronDelaySupplier(cron); return new ReschedulingTask<>(() -> { command.run(); return null; }, this, delaySupplier); }
/** * Finds an available TCP port. * * @param minPortRange The minimum port range * @param maxPortRange The maximum port range * @return The available port */ public static int findAvailableTcpPort(int minPortRange, int maxPortRange) { ArgumentUtils.check(() -> minPortRange > MIN_PORT_RANGE) .orElseFail("Port minimum value must be greater than " + MIN_PORT_RANGE); ArgumentUtils.check(() -> maxPortRange >= minPortRange) .orElseFail("Max port range must be greater than minimum port range"); ArgumentUtils.check(() -> maxPortRange <= MAX_PORT_RANGE) .orElseFail("Port maximum value must be less than " + MAX_PORT_RANGE); int currentPort = nextPort(minPortRange, maxPortRange); while (!isTcpPortAvailable(currentPort)) { currentPort = nextPort(minPortRange, maxPortRange); } return currentPort; }
/** * Constructs a new event for the given data. * * @param event The event * @param data The data * @param <ET> The data type * @return The event instance */ static <ET> Event<ET> of(Event event, ET data) { ArgumentUtils.check("data", data).notNull(); return new DefaultEvent<>(data) .id(event.getId()) .comment(event.getComment()) .name(event.getName()) .retry(event.getRetry()); } }
/** * Perform a check on an argument. * * @param check The check * @return The {@link ArgumentCheck} */ public static ArgumentCheck check(Check check) { return new ArgumentCheck(check); }
/** * Constructs a new event for the given data. * * @param data The data * @param <ET> The data type * @return The event instance */ static <ET> Event<ET> of(ET data) { ArgumentUtils.check("data", data).notNull(); return new DefaultEvent<>(data); }
/** * Perform a check on an argument. * * @param name The name of the argument * @param value The value of the argument * @param <T> The value type * @return The {@link ArgumentCheck} */ public static <T> ArgumentCheck check(String name, T value) { return new ArgumentCheck<>(name, value); }