@Override public Predicate<?> apply(Object input) { // TODO Could try to handle deferred supplier as well? checkArgument(input instanceof String, "Constraint regex value must be a string, but got %s (%s)", (input == null ? "null" : input.getClass().getName()), input); return StringPredicates.matchesRegex((String)input); }});
@Override public Predicate<CharSequence> apply(String input) { return containsLiteral(input); } }));
/** * @since 0.9.0 */ public static Predicate<ConfigKey<?>> nameStartsWith(final String prefix) { return nameSatisfies(StringPredicates.startsWith(prefix)); }
@Test public static void testContainsLiteral() { Assert.assertTrue(StringPredicates.containsLiteral("xx").apply("texxxt tessst")); Assert.assertFalse(StringPredicates.containsLiteral("xx").apply("text test")); Assert.assertFalse(StringPredicates.containsLiteral("xx").apply("texXxt tessst")); Assert.assertTrue(StringPredicates.containsLiteralIgnoreCase("xx").apply("texxxt tessst")); Assert.assertFalse(StringPredicates.containsLiteralIgnoreCase("xx").apply("text test")); Assert.assertTrue(StringPredicates.containsLiteralIgnoreCase("xx").apply("texXxt tessst")); Assert.assertTrue(StringPredicates.containsAllLiterals("xx", "ss").apply("texxxt tessst")); Assert.assertFalse(StringPredicates.containsAllLiterals("xx", "tt").apply("texxxt tessst")); }
@Test public static void testStartsWith() { Assert.assertTrue(StringPredicates.startsWith("t").apply("test")); Assert.assertFalse(StringPredicates.startsWith("v").apply("test")); Assert.assertTrue(StringPredicates.isStringStartingWith("t").apply("test")); Assert.assertFalse(StringPredicates.isStringStartingWith("t").apply(true)); }
@Test public static void testMatches() { Assert.assertTrue(StringPredicates.matchesRegex("t.*").apply("test")); Assert.assertFalse(StringPredicates.matchesRegex("v.*").apply("test")); Assert.assertTrue(StringPredicates.matchesGlob("t*").apply("test")); Assert.assertFalse(StringPredicates.matchesGlob("v*").apply("test")); }
protected Predicate<?> getDefaultValueFilter() { if (getConfig(EXCLUDE_BLANK)) return StringPredicates.isNonBlank(); else return Predicates.alwaysTrue(); }
public static void assertStringContainsIgnoreCase(String input, String phrase1ToContain, String ...optionalOtherPhrasesToContain) { if (input==null) fail("Input is null."); if (phrase1ToContain!=null) { assertThat(input, StringPredicates.containsLiteralIgnoreCase(phrase1ToContain)); } for (String otherPhrase: optionalOtherPhrasesToContain) { if (otherPhrase!=null) { assertThat(input, StringPredicates.containsLiteralIgnoreCase(otherPhrase)); } } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private <T,SpecT> List<CatalogItemSummary> getCatalogItemSummariesMatchingRegexFragment( Predicate<RegisteredType> type, String regex, String fragment, boolean allVersions) { List<Predicate<RegisteredType>> filters = new ArrayList(); filters.add(type); if (Strings.isNonEmpty(regex)) filters.add(RegisteredTypePredicates.stringRepresentationMatches(StringPredicates.containsRegex(regex))); if (Strings.isNonEmpty(fragment)) filters.add(RegisteredTypePredicates.stringRepresentationMatches(StringPredicates.containsLiteralIgnoreCase(fragment))); if (!allVersions) filters.add(RegisteredTypePredicates.isBestVersion(mgmt())); filters.add(RegisteredTypePredicates.entitledToSee(mgmt())); ImmutableList<RegisteredType> sortedItems = FluentIterable.from(brooklyn().getTypeRegistry().getMatching(Predicates.and(filters))) .toSortedList(RegisteredTypes.RegisteredTypeNameThenBestFirstComparator.INSTANCE); return Lists.transform(sortedItems, toCatalogItemSummary(ui)); }
@Test public static void testEqualToAny() { Assert.assertTrue(StringPredicates.equalToAny(ImmutableSet.of("1", "2")).apply("2")); Assert.assertFalse(StringPredicates.equalToAny(ImmutableSet.of("1", "2")).apply("3")); }
/** convenience for interpreters, tests if nodes are not excluded, and if not: * for string nodes, true iff the current value starts with the given prefix; * for nodes which are currently maps or lists, * true iff not excluded and the value contains such an entry (key, in the case of map) */ public boolean matchesPrefix(String prefix) { if (isExcluded()) return false; if (getNewValue() instanceof CharSequence) return getNewValue().toString().startsWith(prefix); if (getNewValue() instanceof Map) return Iterables.tryFind(((Map<?,?>)getNewValue()).keySet(), StringPredicates.isStringStartingWith(prefix)).isPresent(); if (getNewValue() instanceof Iterable) return Iterables.tryFind((Iterable<?>)getNewValue(), StringPredicates.isStringStartingWith(prefix)).isPresent(); return false; }
public EntityConfigSummary(ConfigKey<?> config, String label, Double priority, Boolean pinned, Map<String, URI> links) { super(config, label, priority); this.pinned = pinned; this.constraints = !config.getConstraint().equals(Predicates.alwaysTrue()) ? ImmutableList.of((config.getConstraint().getClass().equals(StringPredicates.isNonBlank().getClass()) ? "required" : config.getConstraint().toString())) : ImmutableList.<String>of(); this.links = links != null ? ImmutableMap.copyOf(links) : null; }
public void testRemoveLines() { Assert.assertEquals(Strings.removeLines(Strings.lines("a", "b"), StringPredicates.containsLiteralIgnoreCase("A")), "b"); }
protected List<Location> findSubLocations(Location loc) { if (!loc.hasExtension(AvailabilityZoneExtension.class)) { throw new IllegalStateException("Availability zone extension not supported for location " + loc); } AvailabilityZoneExtension zoneExtension = loc.getExtension(AvailabilityZoneExtension.class); Collection<String> zoneNames = getConfig(AVAILABILITY_ZONE_NAMES); Integer numZones = getConfig(NUM_AVAILABILITY_ZONES); List<Location> subLocations; if (zoneNames == null || zoneNames.isEmpty()) { if (numZones != null) { subLocations = zoneExtension.getSubLocations(numZones); checkArgument(numZones > 0, "numZones must be greater than zero: %s", numZones); if (numZones > subLocations.size()) { throw new IllegalStateException("Number of required zones (" + numZones + ") not satisfied in " + loc + "; only " + subLocations.size() + " available: " + subLocations); } } else { subLocations = zoneExtension.getAllSubLocations(); } } else { // TODO check that these are valid region / availabilityZones? subLocations = zoneExtension.getSubLocationsByName(StringPredicates.equalToAny(zoneNames), zoneNames.size()); if (zoneNames.size() > subLocations.size()) { throw new IllegalStateException("Number of required zones (" + zoneNames.size() + " - " + zoneNames + ") not satisfied in " + loc + "; only " + subLocations.size() + " available: " + subLocations); } } LOG.info("Returning {} sub-locations: {}", subLocations.size(), Iterables.toString(subLocations)); return subLocations; }
@Override public HttpExecutor getHttpExecutor(Map<?, ?> props) { HttpExecutor httpExecutor; String httpExecutorClass = (String) props.get(HTTP_EXECUTOR_CLASS_CONFIG); if (httpExecutorClass != null) { Map<String,Object> httpExecutorProps = MutableMap.of(); Map<?, ?> executorProps = Maps.filterKeys(props, StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_CONFIG_PREFIX)); if (executorProps.size() > 0) { for (Entry<?, ?> entry: executorProps.entrySet()) { String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_CONFIG_PREFIX); httpExecutorProps.put(keyName, entry.getValue()); } } try { httpExecutor = (HttpExecutor) new ClassLoaderUtils(getClass()).loadClass(httpExecutorClass).getConstructor(Map.class).newInstance(httpExecutorProps); } catch (Exception e) { throw Exceptions.propagate(e); } } else { LOG.info(HTTP_EXECUTOR_CLASS_CONFIG + " parameter not provided. Using the default implementation " + HttpExecutorImpl.class.getName()); httpExecutor = HttpExecutorImpl.newInstance(); } return httpExecutor; } }
protected Predicate<Sensor<?>> resolveMapMatchingConfig() { String regex = getConfig(MAP_MATCHING); final Predicate<CharSequence> namePredicate = StringPredicates.matchesRegex(regex); return new Predicate<Sensor<?>>() { @Override public boolean apply(Sensor<?> input) { return input != null && namePredicate.apply(input.getName()); } }; } }
public static <T extends CharSequence> Predicate<T> containsAllLiterals(final String... fragments) { List<Predicate<CharSequence>> fragmentPredicates = Lists.newArrayList(); for (String fragment : fragments) { fragmentPredicates.add(containsLiteral(fragment)); } return Predicates.and(fragmentPredicates); }
@VisibleForTesting static List<String> getIpAddressWebsites() { String file = new ResourceUtils(LocalhostExternalIpLoader.class) .getResourceAsString("classpath://org/apache/brooklyn/location/geo/external-ip-address-resolvers.txt"); Iterable<String> lines = Splitter.on('\n') .omitEmptyStrings() .trimResults() .split(file); List<String> urls = Lists.newArrayList(Iterables.filter(lines, Predicates.not(StringPredicates.startsWith("#")))); Collections.shuffle(urls); return urls; }
@Test public void testConstraintAsArray() { String name = "minRam"; String constraint = "required"; SpecParameter<?> input = parseSpecParameterDefinition(ImmutableMap.of( "name", name, "constraints", ImmutableList.of(constraint))); ConfigKey<?> type = input.getConfigKey(); assertConstraint(type.getConstraint(), StringPredicates.isNonBlank()); }
@Test public void testSshConfigFromDefault() throws Exception { RecordingSshTool.setCustomResponse(".*myCommand.*", new RecordingSshTool.CustomResponse(0, "myResponse", null)); String bp = loadYaml("config-type-coercion-test.yaml", "location:", " localhost:", " sshToolClass: "+RecordingSshTool.class.getName()); // remove all lines referring to "exact" -- that's useful for expository and running in UI // but it will fail (timeout) if the port isn't available so not good in tests bp = Strings.removeLines(bp, StringPredicates.containsLiteralIgnoreCase("exact")); Entity app = createAndStartApplication(bp); waitForApplicationTasks(app); Map<?, ?> props = RecordingSshTool.getLastExecCmd().env; Assert.assertEquals(props.get("RANGE_PORT_SENSOR"), "20003"); Asserts.assertStringContains((String)props.get("RANGE_PORT_CONFIG"), "{\"start\"", "20003"); Assert.assertEquals(props.get("INT_PORT_CONFIG"), "20001"); Assert.assertEquals(props.get("INT_PORT_DEFAULT_CONFIG"), "30001"); Assert.assertEquals(props.get("RANGE_PORT_DEFAULT_SENSOR"), "30003"); // NB: change in Oct 2016, default values are now coerced just like explicit value // (previous to Oct 2016 this would have returned just "30003+", no json) Asserts.assertStringContains((String)props.get("RANGE_PORT_DEFAULT_CONFIG"), "{\"start\"", "30003"); }