protected Function<Iterable<?>, ?> createReducerFunction(String reducerName, Map<String, ?> parameters){ if(Objects.equals(reducerName, "joiner")){ String separator = (String) parameters.get("separator"); return StringFunctions.joiner(separator == null ? ", " : separator); } if (Objects.equals(reducerName, "formatString")){ String format = Preconditions.checkNotNull((String)parameters.get("format"), "format"); return StringFunctions.formatterForIterable(format); } throw new IllegalStateException("unknown function: " + reducerName); }
@Test public void testJoiner() { Assert.assertEquals(StringFunctions.joiner(" ").apply(Arrays.asList("hello", "world")), "hello world"); Assert.assertEquals(StringFunctions.joinerForArray(" ").apply(new String[] { "hello", "world" }), "hello world"); }
@Test public static void testJoiner() { Assert.assertEquals(StringFunctions.joiner(",").apply(ImmutableList.of("a", "b", "c")), "a,b,c"); }
@SuppressWarnings("unchecked") @Test public void testCombiningEnricher() throws Exception { origApp.enrichers().add(Enrichers.builder() .combining(METRIC1, METRIC2) .from(origEntity) .computing(StringFunctions.joiner(",")) .publishing(METRIC2) .build()); TestApplication newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.sensors().set(METRIC1, "myval"); newEntity.sensors().set(METRIC2, "myval2"); EntityAsserts.assertAttributeEventually(newApp, METRIC2, Predicates.or(Predicates.equalTo("myval,myval2"), Predicates.equalTo("myval2,myval"))); }
@Test public void testAggregatingMembersEnricher() throws Exception { origApp.start(ImmutableList.of(origLoc)); origCluster.resize(2); origApp.enrichers().add(Enrichers.builder() .aggregating(METRIC1) .from(origCluster) .fromMembers() .computing(StringFunctions.joiner(",")) .publishing(METRIC2) .build()); TestApplication newApp = rebind(); DynamicCluster newCluster = (DynamicCluster) Iterables.find(newApp.getChildren(), Predicates.instanceOf(DynamicCluster.class)); int i = 1; for (Entity member : newCluster.getMembers()) { ((EntityInternal)member).sensors().set(METRIC1, "myval"+(i++)); } EntityAsserts.assertAttributeEventually(newApp, METRIC2, Predicates.or(Predicates.equalTo("myval1,myval2"), Predicates.equalTo("myval2,myval1"))); }