/** 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; }
@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; } }
@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)); }