/** * 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 prefix from the beginning of string. Returns string if it does not begin with prefix. */ public static String removeFromStart(String string, String prefix) { if (isEmpty(string)) { return string; } else if (!isEmpty(prefix) && string.startsWith(prefix)) { return string.substring(prefix.length()); } else { return string; } }
/** * Removes suffix from the end of the string. Returns string if it does not end with suffix. */ public static String removeFromEnd(String string, String suffix) { if (isEmpty(string)) { return string; } else if (!isEmpty(suffix) && string.endsWith(suffix)) { return string.substring(0, string.length() - suffix.length()); } else { return string; } }
@Override public boolean apply(@Nullable URL url) { return Strings.isEmpty(url.getFile()) || "/".equals(url.getFile()); } });
/** 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); }
private String getExpectedCredentials() { if (Strings.isEmpty(password)) { return username; } else { return username + ":" + password; } }
protected 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"); } }
private Duration parseDuration(String str, Duration defaultValue) { if (Strings.isEmpty(str)) { return defaultValue; } else { return Duration.parse(str); } }
/** merges paths using forward slash as the "local OS file separator", because it is recognised on windows, * making paths more consistent and avoiding problems with backslashes being escaped. * empty segments are omitted. */ public static String mergePaths(String ...items) { char separatorChar = '/'; StringBuilder result = new StringBuilder(); for (String item: items) { if (Strings.isEmpty(item)) continue; if (result.length() > 0 && !isSeparator(result.codePointAt(result.length()-1))) result.append(separatorChar); result.append(item); } return result.toString(); }
public HostAndPort getSshHostAndPort() { String host = getConfig(SSH_HOST); if (host == null || Strings.isEmpty(host)) host = address.getHostName(); Integer port = getPort(); return HostAndPort.fromParts(host, port); }
@Override 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; } };
@Override 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) ? "org.apache.brooklyn" : "org.apache.brooklyn."+subgroupUnderIoBrooklyn, artifactId, packaging, classifier, BrooklynVersion.get()); }
private String getConfigOrDefault(BasicAttributeSensorAndConfigKey<String> key, String def) { String val = entity.getConfig(key); if (Strings.isEmpty(val)) { val = entity.sensors().get(key); if (Strings.isEmpty(val)) { val = def; log.debug(entity + " has no config specified for " + key + "; using default `" + def + "`"); entity.sensors().set(key, val); } } return val; }
@Override public String getSubnetIp() { String containerAddress = dockerContainer.sensors().get(Attributes.SUBNET_ADDRESS); if (Strings.isEmpty(containerAddress)) { String containerId = checkNotNull(dockerContainer.getContainerId(), "containerId"); containerAddress = dockerContainer.getDockerHost() .runDockerCommand("inspect --format={{.NetworkSettings.IPAddress}} " + containerId) .trim(); } return containerAddress; }
private int scpFromServer(Map<String,?> props, String remote, File local) { String from = (Strings.isEmpty(getUsername()) ? "" : getUsername()+"@")+getHostAddress()+":"+remote; return scpExec(props, from, local.getAbsolutePath()); }
@Override protected boolean getDefaultRestartStopsMachine() { if (entity().getDriver() == null) { log.debug("restart of "+entity()+" has no driver - doing machine-level restart"); return true; } if (Strings.isEmpty(entity().getAttribute(Attributes.HOSTNAME))) { log.debug("restart of "+entity()+" has no hostname - doing machine-level restart"); return true; } return false; }
@Override public void init() { LOG.info("Starting virtual network segment id {}", getId()); super.init(); String networkId = config().get(NETWORK_ID); if (Strings.isEmpty(networkId)) networkId = getId(); sensors().set(NETWORK_ID, networkId); setDisplayName(String.format("Virtual Network (%s)", networkId)); ConfigToAttributes.apply(this, SDN_PROVIDER); }