@Override @SuppressWarnings("unchecked") public BrooklynPropertiesImpl addFrom(Map map) { putAll(Maps.transformValues(map, StringFunctions.trim())); return this; }
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 static void testPrepend() { Assert.assertEquals(StringFunctions.prepend("Hello ").apply("World"), "Hello World"); }
@Test public void testFormatter() { Assert.assertEquals(StringFunctions.formatter("hello %s").apply("world"), "hello world"); Assert.assertEquals(StringFunctions.formatterForArray("%s %s").apply(new String[] { "hello", "world" }), "hello world"); }
@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"); }
/** checks that all accepted enum values are represented by the given set of explicit values */ public static void checkAllEnumeratedIgnoreCase(String contextMessage, Enum<?>[] enumValues, String ...explicitValues) { MutableSet<String> explicitValuesSet = MutableSet.copyOf(Iterables.transform(Arrays.asList(explicitValues), StringFunctions.toLowerCase())); Set<Enum<?>> missingEnums = MutableSet.of(); for (Enum<?> e: enumValues) { if (explicitValuesSet.remove(e.name().toLowerCase())) continue; if (explicitValuesSet.remove(e.toString().toLowerCase())) continue; if (explicitValuesSet.remove(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, e.name()).toLowerCase())) continue; if (explicitValuesSet.remove(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, e.toString()).toLowerCase())) continue; if (explicitValuesSet.remove(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, e.toString()).toLowerCase())) continue; if (explicitValuesSet.remove(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, e.name()).toLowerCase())) continue; missingEnums.add(e); } if (!missingEnums.isEmpty() || !explicitValuesSet.isEmpty()) { throw new IllegalStateException("Not all options for "+contextMessage+" are enumerated; " + "leftover enums = "+missingEnums+"; " + "leftover values = "+explicitValuesSet); } }
/** returns comparator which compares based on length, with shorter ones first (and null before that); * in event of a tie, it uses the toString order */ public static Ordering<String> lengthComparator() { return Ordering.<Integer>natural().onResultOf(StringFunctions.length()).compound(Ordering.<String>natural()).nullsFirst(); }
/** true if the object *is* a {@link CharSequence} starting with the given prefix */ public static Predicate<Object> isStringStartingWith(final String prefix) { return Predicates.<Object>and(Predicates.instanceOf(CharSequence.class), Predicates.compose(startsWith(prefix), StringFunctions.toStringFunction())); }
@Test public static void testFormatter() { Assert.assertEquals(StringFunctions.formatter("Hello %s").apply("World"), "Hello World"); }
@Test public static void testFormatterForIterable() { Assert.assertEquals(StringFunctions.formatterForIterable("Hello %s").apply(ImmutableList.of("World")), "Hello World"); Assert.assertEquals(StringFunctions.formatterForIterable("Hello").apply(ImmutableList.of()), "Hello"); Assert.assertEquals(StringFunctions.formatterForIterable("Hello").apply(null), "Hello"); }
@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 static void testLowerCase() { Assert.assertEquals(StringFunctions.toLowerCase().apply("Hello World"), "hello world"); }
@Test public static void testLength() { Assert.assertEquals(StringFunctions.length().apply("abc"), (Integer)3); }
public static String getMinimalLauncherPropertiesString() throws IOException { BrooklynProperties p1 = LocalManagementContextForTests.builder(true).buildProperties(); Properties p = new Properties(); p.putAll(Maps.transformValues(p1.asMapWithStringKeys(), StringFunctions.toStringFunction())); Writer w = new StringWriter(); p.store(w, "test"); w.close(); return w.toString()+"\n"; }
.appendAll(Iterables.transform(pgctlLocations, StringFunctions.formatter("test -x %s/pg_ctl"))) .append(installPackage(ImmutableMap.of( "yum", "postgresql"+shortVersion+" postgresql"+shortVersion+"-server",
private List<String> getDockerExecCommand(List<String> commands, Map<String,?> env) { StringBuilder target = new StringBuilder("docker exec ") .append(dockerContainer.getContainerId()) .append(" /bin/bash -c '"); Joiner.on(";").appendTo(target, Iterables.concat(getEnvironemnt(env), Iterables.transform(commands, StringFunctions.trim()))); target.append("'"); return ImmutableList.of(target.toString()); }
@Override public void createSubnet(String subnetId, Cidr subnetCidr) { Iterable<String> opts = Iterables.transform(getDockerNetworkCreateOpts(), StringFunctions.prepend("--opt ")); getEntity().sensors().get(SdnAgent.DOCKER_HOST).runDockerCommand( String.format("network create --driver %s %s --subnet=%s %s", getDockerNetworkDriver(), Joiner.on(' ').join(opts), subnetCidr, subnetId)); }
@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"))); }
throw new IllegalStateException("No networks configured for container"); networks = Lists.transform(networks, StringFunctions.toLowerCase());