/** * Supplies the value to be returned by each poll. * <p> * Note this <em>must</em> use generics, otherwise the return type of subsequent chained * calls will (e.g. to {@link FeedConfig#onException(com.google.common.base.Function)} will * return the wrong type. */ @SuppressWarnings("unchecked") public <newS> FunctionPollConfig<newS, T> supplier(final Supplier<? extends newS> val) { this.callable = Functionals.callable( checkNotNull(val, "supplier") ); return (FunctionPollConfig<newS, T>) this; }
@Test public void testIsSatisfied() throws Exception { Predicate<Integer> isEven = new Predicate<Integer>() { @Override public boolean apply(Integer input) { return (input % 2 == 0); } }; Assert.assertFalse(Functionals.isSatisfied(11, isEven).call()); Assert.assertTrue(Functionals.isSatisfied(22, isEven).call()); }
.poll(new HttpPollConfig<Boolean>(WEB_CONSOLE_ACCESSIBLE) .suburl("/v1/server/healthy") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(Boolean.class))) .poll(new HttpPollConfig<ManagementNodeState>(MANAGEMENT_NODE_STATE) .suburl("/v1/server/ha/state") .onSuccess(Functionals.chain(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(String.class)), Enums.fromStringFunction(ManagementNodeState.class))) .setOnFailureOrException(null)) .computing(Functionals.ifNotEquals(true).value("URL where Brooklyn listens is not answering correctly") ) .build());
@Test public void testIfNotEqual() { IfFunctionsTest.checkTF(Functionals.ifNotEquals(false).value("T").defaultValue("F").build(), "T"); }
@Test public void testIfNoBuilder() { IfFunctionsTest.checkTF(Functionals.ifEquals(false).value("F").ifEquals(true).value("T").defaultValue("?"), "?"); }
public static final EnricherSpec<?> newEnricherForServiceUpIfNotUpIndicatorsEmpty() { // The function means: if the serviceNotUpIndicators is not null, then serviceNotUpIndicators.size()==0; // otherwise return the default value. return Enrichers.builder() .transforming(SERVICE_NOT_UP_INDICATORS).<Object>publishing(Attributes.SERVICE_UP) .suppressDuplicates(true) .computing( Functionals.<Map<String,?>> ifNotEquals(null).<Object>apply(Functions.forPredicate(CollectionFunctionals.<String>mapSizeEquals(0))) .defaultValue(Entities.UNCHANGED) ) .uniqueTag(DEFAULT_ENRICHER_UNIQUE_TAG) .build(); }
@Test public void testIf() { IfFunctionsTest.checkTF(Functionals.ifEquals(false).value("F").ifEquals(true).value("T").defaultValue("?").build(), "?"); }
/** applies f1 to the input, then f2 to that result, then f3 to that result, then f4 to that result */ public static <A,B,C,D,E> Function<A,E> chain(final Function<A,? extends B> f1, final Function<B,? extends C> f2, final Function<C,? extends D> f3, final Function<D,E> f4) { return chain(f1, chain(f2, chain(f3, f4))); }
@Override protected Entity createNode(Location loc, Map<?, ?> flags) { MySqlNode node = (MySqlNode) super.createNode(loc, flags); if (!MySqlClusterUtils.IS_MASTER.apply(node)) { ServiceNotUpLogic.updateNotUpIndicator(node, MySqlSlave.SLAVE_HEALTHY, "Replication not started"); addFeed(FunctionFeed.builder() .entity(node) .period(Duration.FIVE_SECONDS) .poll(FunctionPollConfig.forSensor(MySqlSlave.SLAVE_HEALTHY) .callable(new SlaveStateCallable(node)) .checkSuccess(StringPredicates.isNonBlank()) .onSuccess(new SlaveStateParser(node)) .setOnFailure(false) .description("Polls SHOW SLAVE STATUS")) .build()); node.enrichers().add(Enrichers.builder().updatingMap(Attributes.SERVICE_NOT_UP_INDICATORS) .from(MySqlSlave.SLAVE_HEALTHY) .computing(Functionals.ifNotEquals(true).value("Slave replication status is not healthy") ) .build()); } return node; }
public static <T,U> Callable<U> callable(Function<T,U> f, T x) { return callable(Suppliers.compose(f, Suppliers.ofInstance(x))); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testUpdatingMap2() { entity.enrichers().add(Enrichers.builder() .updatingMap((AttributeSensor)MAP2) .from(LONG1) .computing(Functionals.ifEquals(-1L).value("-1 is not allowed")) .build()); doUpdatingMapChecks(MAP2); }
/** Waits until the passed entity satisfies the supplied predicate. */ public static void waitFor(Entity entity, Predicate<Entity> condition, Duration timeout) { try { String description = "Waiting for " + condition + " on " + entity; Tasks.setBlockingDetails(description); Repeater repeater = Repeater.create(description) .until(isSatisfied(entity, condition)) .limitTimeTo(timeout) .backoffTo(Duration.ONE_SECOND) .rethrowException(); if (!repeater.run()) { throw new IllegalStateException("Timeout waiting for " + condition + " on " + entity); } } finally { Tasks.resetBlockingDetails(); } log.debug("Detected {} for {}", condition, entity); }
/** * @deprecated since 0.9.0; use {@link Functionals#chain(Function, Function, Function)} */ @Deprecated public static <A,B,C,D> Function<A,D> chain(final Function<A,? extends B> f1, final Function<B,? extends C> f2, final Function<C,D> f3) { return Functionals.chain(f1, f2, f3); } }
.computing(Functionals.ifNotEquals(true).value("URL not reachable")) .build());
@Override public V2 call() throws Exception { List<V> prePostProgress = DynamicTasks.queue(parallelTask).get(); return DynamicTasks.queue( Tasks.<V2>builder().displayName("post-processing").description("Applying "+postProcessFromMultiple) .body(Functionals.callable(postProcessFromMultiple, prePostProgress)) .build()).get(); } })
@Test public void testUpdatingMap1() { entity.enrichers().add(Enrichers.builder() .updatingMap(MAP1) .from(LONG1) .computing(Functionals.ifEquals(-1L).value("-1 is not allowed")) .build()); doUpdatingMapChecks(MAP1); }
/** * @deprecated since 0.9.0; use {@link Functionals#chain(Function, Function)} */ @Deprecated public static <A,B,C> Function<A,C> chain(final Function<A,? extends B> f1, final Function<B,C> f2) { return Functionals.chain(f1, f2); }
.uniqueTag("not-up-unless-url-answers") .from(NGINX_URL_ANSWERS_NICELY) .computing(Functionals.ifNotEquals(true).value("URL where nginx listens is not answering correctly (with expected header)") ) .build()); connectServiceUpIsRunning();
private Maybe<Entity> getResolvedConfigInTask(final Entity entity, final ConfigKey<Entity> key) { return Tasks.resolving(Tasks.<Entity>builder().body( Functionals.callable(Suppliers.compose(EntityFunctions.config(key), Suppliers.ofInstance(entity))) ).build()) .as(Entity.class) .context(entity).embedResolutionInTask(true) .getMaybe(); }