public static Predicate<Iterable<?>> empty() { return sizeEquals(0); }
@Test public void testMapSize() { Assert.assertTrue(CollectionFunctionals.<String>mapSizeEquals(2).apply(ImmutableMap.of("x", "1", "y", "2"))); Assert.assertFalse(CollectionFunctionals.<String>mapSizeEquals(2).apply(null)); Assert.assertTrue(CollectionFunctionals.mapSizeEquals(0).apply(ImmutableMap.of())); Assert.assertFalse(CollectionFunctionals.mapSizeEquals(0).apply(null)); }
public static <K> Predicate<Map<K,?>> mapNotEmpty() { return Predicates.not(CollectionFunctionals.<K>mapEmptyOrNull()); }
waitForApplicationTasks(app); Asserts.assertThat(app.getChildren(), CollectionFunctionals.empty()); Asserts.assertThat((Iterable<?>)result, CollectionFunctionals.sizeEquals(1)); Asserts.assertThat(app.getChildren(), CollectionFunctionals.sizeEquals(1)); Entity child = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(child.getId(), Iterables.getOnlyElement((Iterable<?>)result));
@Test public void testAllAndAny() { Assert.assertEquals(CollectionFunctionals.all(Predicates.equalTo(1)).apply( MutableList.of(1, 1, 1)), true); Assert.assertEquals(CollectionFunctionals.all(Predicates.equalTo(1)).apply( MutableList.<Integer>of()), true); Assert.assertEquals(CollectionFunctionals.all(Predicates.equalTo(1)).apply( MutableList.of(1, 0, 1)), false); Assert.assertEquals(CollectionFunctionals.all(Predicates.equalTo(1)).apply( MutableList.of(0, 0, 0)), false); Assert.assertEquals(CollectionFunctionals.any(Predicates.equalTo(1)).apply( MutableList.of(1, 1, 1)), true); Assert.assertEquals(CollectionFunctionals.any(Predicates.equalTo(1)).apply( MutableList.<Integer>of()), false); Assert.assertEquals(CollectionFunctionals.any(Predicates.equalTo(1)).apply( MutableList.of(1, 0, 1)), true); Assert.assertEquals(CollectionFunctionals.any(Predicates.equalTo(1)).apply( MutableList.of(0, 0, 0)), false); }
private <T> void propagateMasterAttribute(AttributeSensor<T> att) { enrichers().add(Enrichers.builder() .aggregating(att) .publishing(att) .computing(IfFunctions.<Collection<T>>ifPredicate(CollectionFunctionals.notEmpty()) .apply(CollectionFunctionals.firstElement()) .defaultValue(null)) .entityFilter(MySqlClusterUtils.IS_MASTER) .build()); }
/** as {@link #testingAttributeEventually(Entity, AttributeSensor, Predicate, Duration) for multiple entities */ public static <T> Task<Boolean> testingAttributeEventually(Iterable<Entity> entities, AttributeSensor<T> sensor, Predicate<T> condition, Duration timeout) { return DependentConfiguration.builder().attributeWhenReadyFromMultiple(entities, sensor, condition) .postProcess(Functions.constant(true)) .timeout(timeout) .onTimeoutReturn(false) .onUnmanagedReturn(false) .postProcessFromMultiple(CollectionFunctionals.all(Predicates.equalTo(true))) .build(); }
/** * Create a predicate that matches any entity who has an exact match for the given location * (i.e. {@code entity.getLocations().contains(location)}). */ public static <T> Predicate<Entity> locationsIncludes(Location location) { return locationsSatisfy(CollectionFunctionals.contains(location)); }
public static Predicate<RegisteredType> alias(final String alias) { return aliases(CollectionFunctionals.any(Predicates.equalTo(alias))); } public static Predicate<RegisteredType> aliases(final Predicate<? super Iterable<String>> filter) {
public static Predicate<Iterable<?>> notEmpty() { return Predicates.not(emptyOrNull()); }
@Test public void testFirstElement() { Assert.assertEquals(CollectionFunctionals.firstElement().apply(null), null); Assert.assertEquals(CollectionFunctionals.firstElement().apply(ImmutableList.of("a")), "a"); Assert.assertEquals(CollectionFunctionals.firstElement().apply(ImmutableList.of("a", "b", "c")), "a"); }
/** as {@link #requiringAttributeEventually(Entity, AttributeSensor, Predicate, Duration) for multiple entities */ public static <T> Task<Boolean> requiringAttributeEventually(Iterable<Entity> entities, AttributeSensor<T> sensor, Predicate<T> condition, Duration timeout) { return DependentConfiguration.builder().attributeWhenReadyFromMultiple(entities, sensor, condition) .postProcess(Functions.constant(true)) .timeout(timeout) .onTimeoutThrow() .onUnmanagedThrow() .postProcessFromMultiple(CollectionFunctionals.all(Predicates.equalTo(true))) .build(); }
protected void waitForMessage(final String id) { Asserts.eventuallyOnNotify(messages, CollectionFunctionals.contains(id), TIMEOUT); } protected void releaseAndWaitForMonitorableJob(final String id) {
public static Predicate<RegisteredType> tag(final Object tag) { return tags(CollectionFunctionals.any(Predicates.equalTo(tag))); } public static Predicate<RegisteredType> tags(final Predicate<? super Iterable<Object>> filter) {
@Test public void testListEmptyOrNull() { Assert.assertTrue(CollectionFunctionals.emptyOrNull().apply(null)); Assert.assertTrue(CollectionFunctionals.emptyOrNull().apply(ImmutableList.of())); Assert.assertFalse(CollectionFunctionals.emptyOrNull().apply(ImmutableList.of("x"))); }
@Test public void testListSize() { Assert.assertTrue(CollectionFunctionals.sizeEquals(2).apply(ImmutableList.of("x", "y"))); Assert.assertFalse(CollectionFunctionals.sizeEquals(2).apply(null)); Assert.assertTrue(CollectionFunctionals.sizeEquals(0).apply(ImmutableList.of())); Assert.assertFalse(CollectionFunctionals.sizeEquals(0).apply(null)); }
public static <K> Predicate<Map<K,?>> mapEmptyOrNull() { return Predicates.<Map<K,?>>or(Predicates.isNull(), CollectionFunctionals.<K>mapSizeEquals(0)); }
@Test public void testMapEmptyOrNull() { Assert.assertTrue(CollectionFunctionals.mapEmptyOrNull().apply(null)); Assert.assertTrue(CollectionFunctionals.mapEmptyOrNull().apply(ImmutableMap.of())); Assert.assertFalse(CollectionFunctionals.<String>mapEmptyOrNull().apply(ImmutableMap.of("mykey", "myval"))); }
public static Predicate<Iterable<?>> emptyOrNull() { return Predicates.or(Predicates.isNull(), sizeEquals(0)); }