/** * The inverse of {@link #isEmpty(CharSequence)}. * * @param s the String to check * @return true if non empty, false otherwise. * * @see #isEmpty(CharSequence) * @see #isBlank(CharSequence) * @see #isNonBlank(CharSequence) */ public static boolean isNonEmpty(CharSequence s) { return !isEmpty(s); }
/** throws IllegalArgument if string not empty; cf. guava Preconditions.checkXxxx */ public static void checkNonEmpty(CharSequence s, String message) { if (isEmpty(s)) throw new IllegalArgumentException(message); }
/** removes the first suffix in the list which is present at the end of string * and returns that string; ignores subsequent suffixes if a matching one is found; * returns the original string if no suffixes are at the end */ public static String removeFromEnd(String string, String ...suffixes) { if (isEmpty(string)) return string; for (String suffix : suffixes) if (string.endsWith(suffix)) return string.substring(0, string.length() - suffix.length()); return string; }
/** removes the first prefix in the list which is present at the start of string * and returns that string; ignores subsequent prefixes if a matching one is found; * returns the original string if no prefixes match */ public static String removeFromStart(String string, String ...prefixes) { if (isEmpty(string)) return string; for (String prefix : prefixes) if (string.startsWith(prefix)) return string.substring(prefix.length()); return string; }
/** as removeFromStart, but repeats until all such suffixes are gone */ public static String removeAllFromStart(String string, String ...prefixes) { boolean anotherLoopNeeded = true; while (anotherLoopNeeded) { if (isEmpty(string)) return string; anotherLoopNeeded = false; for (String prefix : prefixes) if (string.startsWith(prefix)) { string = string.substring(prefix.length()); anotherLoopNeeded = true; break; } } return string; }
/** as removeFromEnd, but repeats until all such suffixes are gone */ public static String removeAllFromEnd(String string, String ...suffixes) { boolean anotherLoopNeeded = true; while (anotherLoopNeeded) { if (isEmpty(string)) return string; anotherLoopNeeded = false; for (String suffix : suffixes) if (string.endsWith(suffix)) { string = string.substring(0, string.length() - suffix.length()); anotherLoopNeeded = true; break; } } return string; }
/** if s is wrapped in double quotes containing no unescaped double quotes */ public static boolean isWrappedInDoubleQuotes(String s) { if (Strings.isEmpty(s)) return false; if (!s.startsWith("\"") || !s.endsWith("\"")) return false; return (s.substring(1, s.length()-1).replace("\\\\", "").replace("\\\"", "").indexOf("\"")==-1); }
/** if s is wrapped in single quotes containing no unescaped single quotes */ public static boolean isWrappedInSingleQuotes(String s) { if (Strings.isEmpty(s)) return false; if (!s.startsWith("\'") || !s.endsWith("\'")) return false; return (s.substring(1, s.length()-1).replace("\\\\", "").replace("\\\'", "").indexOf("\'")==-1); }
/** * Checks if the given string is empty or only consists of whitespace. * * @param s the String to check * @return true if blank, empty or null, false otherwise. * * @see #isEmpty(CharSequence) * @see #isNonEmpty(CharSequence) * @see #isNonBlank(CharSequence) */ public static boolean isBlank(CharSequence s) { return isEmpty(s) || CharMatcher.WHITESPACE.matchesAllOf(s); }
public static boolean containsLiteral(CharSequence input, CharSequence fragment) { if (input==null) return false; if (isEmpty(fragment)) return true; int lastValidStartPos = input.length()-fragment.length(); char f0 = fragment.charAt(0); i: for (int i=0; i<=lastValidStartPos; i++) { char ii = input.charAt(i); if (ii==f0) { for (int j=1; j<fragment.length(); j++) { if (input.charAt(i+j)!=fragment.charAt(j)) continue i; } return true; } } return false; }
void verifyConfig(ProxySslConfig proxySslConfig) { if(Strings.isEmpty(proxySslConfig.getCertificateDestination()) && Strings.isEmpty(proxySslConfig.getCertificateSourceUrl())){ throw new IllegalStateException("ProxySslConfig can't have a null certificateDestination and null certificateSourceUrl. One or both need to be set"); } }
/** transforms e.g. [ "-Dname=Bob Johnson", "-Dnet.worth=$100" ] to * a java string "\"-Dname=Bob Johnson\" \"-Dnet.worth=\$100\"" -- * which can be inserted into a bash command where it will be picked up as 2 params */ public static String doubleQuoteLiteralsForBash(String... args) { StringBuilder result = new StringBuilder(); for (String arg: args) { if (!Strings.isEmpty(result)) result.append(" "); result.append("\""); result.append(escapeLiteralForDoubleQuotedBash(arg)); result.append("\""); } return result.toString(); }
public HostAndPort getSshHostAndPort() { String host = getConfig(SSH_HOST); if (host == null || Strings.isEmpty(host)) host = address.getHostName(); Integer port = getConfig(SSH_PORT); if (port == null || port == 0) port = 22; return HostAndPort.fromParts(host, port); }
/** true if the URL points to content which must be resolved on the server-side (i.e. classpath) * and which is safe to do so (currently just images, though in future perhaps also javascript and html plugins) * <p> * note we do not let caller access classpath through this mechanism, * just those which are supplied by the platform administrator e.g. as an icon url */ public boolean isUrlServerSideAndSafe(String url) { if (Strings.isEmpty(url)) return false; String ext = Files.getFileExtension(url); if (Strings.isEmpty(ext)) return false; MediaType mime = WebResourceUtils.getImageMediaTypeFromExtension(ext); if (mime==null) return false; return !Urls.isUrlWithProtocol(url) || url.startsWith("classpath:"); }
public Void apply(@Nullable ProcessTaskWrapper<?> input) { if (input.getExitCode()!=0 && input.getStderr().indexOf("WARNING: No knife configuration file found")>=0) { String myConfig = knifeConfigFileOption(); if (Strings.isEmpty(myConfig)) throw new IllegalStateException("Config file for Chef knife must be specified in "+ChefConfig.KNIFE_CONFIG_FILE+" (or valid knife default set up)"); else throw new IllegalStateException("Error reading config file for Chef knife ("+myConfig+") -- does it exist?"); } return null; } };
public static MavenArtifact artifact(String subgroupUnderIoBrooklyn, String artifactId, String packaging, String classifier) { return new MavenArtifact( Strings.isEmpty(subgroupUnderIoBrooklyn) ? "io.brooklyn" : "io.brooklyn."+subgroupUnderIoBrooklyn, artifactId, packaging, classifier, BrooklynVersion.get()); }
private int scpToServer(Map<String,?> props, File local, String remote) { String to = (Strings.isEmpty(getUsername()) ? "" : getUsername()+"@")+getHostAddress()+":"+remote; return scpExec(props, local.getAbsolutePath(), to); }
private int scpFromServer(Map<String,?> props, String remote, File local) { String from = (Strings.isEmpty(getUsername()) ? "" : getUsername()+"@")+getHostAddress()+":"+remote; return scpExec(props, from, local.getAbsolutePath()); }
/** * Returns a unique valid java identifier name based on the input. * * Translated as per {@link #makeValidJavaName(String)} but with {@link String#hashCode()} * appended where necessary to guarantee uniqueness. * * @see #makeValidJavaName(String) */ public static String makeValidUniqueJavaName(String s) { String name = makeValidJavaName(s); if (isEmpty(s) || IS_JAVA_IDENTIFIER_PART.matchesAllOf(s) || CharMatcher.is('\'').matchesNoneOf(s)) { return name; } else { return name + "_" + s.hashCode(); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) private <T extends Entity> brooklyn.entity.proxying.EntitySpec<?> toCoreEntitySpec(Class<T> clazz, String name, Map<String,String> configO) { Map<String, String> config = (configO == null) ? Maps.<String,String>newLinkedHashMap() : Maps.newLinkedHashMap(configO); brooklyn.entity.proxying.EntitySpec<? extends Entity> result; if (clazz.isInterface()) { result = brooklyn.entity.proxying.EntitySpec.create(clazz); } else { // If this is a concrete class, particularly for an Application class, we want the proxy // to expose all interfaces it implements. Class interfaceclazz = (Application.class.isAssignableFrom(clazz)) ? Application.class : Entity.class; Set<Class<?>> additionalInterfaceClazzes = Reflections.getInterfacesIncludingClassAncestors(clazz); result = brooklyn.entity.proxying.EntitySpec.create(interfaceclazz).impl(clazz).additionalInterfaces(additionalInterfaceClazzes); } if (!Strings.isEmpty(name)) result.displayName(name); result.configure( convertFlagsToKeys(result.getImplementation(), config) ); return result; }