Example usage for com.google.common.base Strings isNullOrEmpty

List of usage examples for com.google.common.base Strings isNullOrEmpty

Introduction

In this page you can find the example usage for com.google.common.base Strings isNullOrEmpty.

Prototype

public static boolean isNullOrEmpty(@Nullable String string) 

Source Link

Document

Returns true if the given string is null or is the empty string.

Usage

From source file:jp.tomorrowkey.irkit4j.RemoteIRKit.java

@VisibleForTesting
static Messages getMessages(String clientKey, boolean clear, HttpClient httpClient) throws IOException {
    if (Strings.isNullOrEmpty(clientKey))
        throw new IllegalArgumentException("clientKey must not be null");
    if (httpClient == null)
        throw new IllegalArgumentException("httpClient must not be null");

    try {// w  w w  .ja v a2  s .  c o m
        String url = BASE_URL + "/messages";
        HttpRequest httpRequest = new HttpRequest(HttpRequestMethod.GET, url,
                new HttpParameter("clientkey", clientKey));
        if (clear)
            httpRequest.addParameters(new HttpParameter("clear", "1"));
        HttpResponse httpResponse = httpClient.request(httpRequest);
        int statusCode = httpResponse.getStatusCode();
        String content = httpResponse.getContent();
        if (statusCode == HttpStatusCode.OK) {
            return GSON.fromJson(content, Messages.class);
        } else {
            throw new UnexpectedStatusCodeException(statusCode, content);
        }
    } finally {
        httpClient.disconnect();
    }
}

From source file:com.dangdang.config.service.easyzk.support.localoverride.LocalOverrideFileLoader.java

public static Map<String, String> loadLocalProperties(String rootNode, String group) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(rootNode) && !Strings.isNullOrEmpty(group),
            "rootNode or group cannot be empty.");

    Map<String, String> properties = null;
    final String localOverrideFile = findLocalOverrideFile();
    InputStream in = null;//  www . j a  va  2 s  .c o m
    try {
        in = LocalOverrideFileLoader.class.getClassLoader().getResourceAsStream(localOverrideFile);
        if (in != null) {
            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            final DocumentBuilder builder = factory.newDocumentBuilder();

            final Document doc = builder.parse(in);
            final Element factoriesNode = Preconditions.checkNotNull(doc.getDocumentElement(),
                    "Root xml node node-factories not exists.");

            Node factoryNode = findChild(factoriesNode, "node-factory", "root", rootNode);
            if (factoriesNode != null) {
                Node nodeGroup = findChild(factoryNode, "group", "id", group);
                if (nodeGroup != null) {
                    NodeList childNodes = nodeGroup.getChildNodes();
                    int nodeCount = childNodes.getLength();
                    if (nodeCount > 0) {
                        properties = Maps.newHashMap();
                        for (int i = 0; i < nodeCount; i++) {
                            Node item = childNodes.item(i);
                            if (item.hasAttributes()) {
                                NamedNodeMap attributes = item.getAttributes();
                                Node keyAttr = attributes.getNamedItem("key");
                                if (keyAttr != null) {
                                    String propKey = keyAttr.getNodeValue();
                                    String propVal = item.getFirstChild().getNodeValue();
                                    if (propKey != null && propVal != null) {
                                        properties.put(propKey, propVal);
                                    }
                                }

                            }

                        }
                    }
                }
            }
        }
    } catch (ParserConfigurationException e) {
        throw Throwables.propagate(e);
    } catch (SAXException e) {
        throw Throwables.propagate(e);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // IGNORE
            }
        }
    }
    return properties;
}

From source file:com.google.protoeditor.parsing.AbstractProtoParser.java

/**
 * Returns the fully qualified name for the given identifier.
 *
 * @param outerName Name of the enclosing namespace, like message name.
 * @param name Name of the inner identifier.
 * @return  Fully qualified name for the identifier.
 *//*from  www.j  a  va2  s  .  c  o  m*/
protected static String toFQName(String outerName, String name) {
    return Strings.isNullOrEmpty(outerName) ? name : outerName + "." + name;
}

From source file:org.excalibur.core.util.YesNoEnum.java

public static YesNoEnum valueOfFrom(String value) {
    if (Strings.isNullOrEmpty(value)) {
        return YesNoEnum.NO;
    }/*  www  .  j ava2s. c  o m*/

    for (YesNoEnum val : values()) {
        if (val.getId() == value.charAt(0)) {
            return val;
        }
    }

    throw new IllegalArgumentException("Invalid value: " + value);
}

From source file:com.google.devtools.common.options.InvocationPolicyParser.java

/**
 * Parses InvocationPolicy in either of the accepted formats. Returns an empty policy if no policy
 * is provided.//from   ww  w . ja va 2 s  .  co m
 *
 * @throws com.google.devtools.common.options.OptionsParsingException if the value of
 *     --invocation_policy is invalid.
 */
public static InvocationPolicy parsePolicy(String policy) throws OptionsParsingException {
    if (Strings.isNullOrEmpty(policy)) {
        return InvocationPolicy.getDefaultInstance();
    }

    try {
        try {
            // First try decoding the policy as a base64 encoded binary proto.
            return InvocationPolicy
                    .parseFrom(BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(policy)));
        } catch (IllegalArgumentException e) {
            // If the flag value can't be decoded from base64, try decoding the policy as a text
            // formatted proto.
            InvocationPolicy.Builder builder = InvocationPolicy.newBuilder();
            TextFormat.merge(policy, builder);
            return builder.build();
        }
    } catch (InvalidProtocolBufferException | TextFormat.ParseException e) {
        throw new OptionsParsingException("Malformed value of --invocation_policy: " + policy, e);
    }
}

From source file:monasca.api.app.validation.MetricNameValidation.java

/**
 * Validates the {@code metricName} for the character constraints.
 * /*from www  .java2 s  .co m*/
 * @throws WebApplication if validation fails
 */
public static void validate(String metricName, boolean nameRequiredFlag) {

    // General validations

    if (Strings.isNullOrEmpty(metricName)) {
        if (nameRequiredFlag) {
            throw Exceptions.unprocessableEntity("Metric name is required");
        } else {
            return;
        }
    }

    if (metricName.length() > CreateMetricCommand.MAX_NAME_LENGTH)
        throw Exceptions.unprocessableEntity("Metric name %s must be %d characters or less", metricName,
                CreateMetricCommand.MAX_NAME_LENGTH);
    if (!VALID_METRIC_NAME.matcher(metricName).matches())
        throw Exceptions.unprocessableEntity("Metric name %s may not contain: > < = { } ( ) ' \" \\ , ; &",
                metricName);
}

From source file:org.apache.isis.core.metamodel.facets.object.parseable.ParserUtil.java

public static String parserNameFromConfiguration(final Class<?> type, final IsisConfiguration configuration) {
    final String key = PARSER_NAME_KEY_PREFIX + type.getCanonicalName() + PARSER_NAME_KEY_SUFFIX;
    final String parserName = configuration.getString(key);
    return !Strings.isNullOrEmpty(parserName) ? parserName : null;
}

From source file:com.google.cloud.tools.appengine.cloudsdk.internal.args.Args.java

/**
 * Produces the flag form of a string value, separated with an equals character.
 *
 * @return {@code [--name=value]} or {@code []} if value is null.
 *//*from  w w w  .  jav a 2s  .co m*/
static List<String> stringWithEq(String name, String value) {
    if (!Strings.isNullOrEmpty(value)) {
        return Collections.singletonList("--" + name + "=" + value);
    }
    return Collections.emptyList();
}

From source file:org.xdi.oxd.licenser.server.service.ValidationService.java

public LdapLicenseId getLicenseId(String licenseId) {
    try {//from w w w  .j av  a 2s  .c o  m
        if (!Strings.isNullOrEmpty(licenseId)) {
            final LdapLicenseId byId = licenseIdService.getById(licenseId);
            if (byId != null) {
                return byId;
            }
        }
        LOG.error("License ID is blank" + licenseId);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
    throw new WebApplicationException(ErrorService.response("Failed to find License ID with id: " + licenseId));
}

From source file:org.haiku.haikudepotserver.support.HttpRequestClientIdentifierSupplier.java

@Override
public Optional<String> get() {
    if (null != request) {
        String result = request.getHeader(HttpHeaders.X_FORWARDED_FOR);

        if (!Strings.isNullOrEmpty(result)) {
            return Optional.of(result);
        }/* w ww.  j  a va 2  s  . co  m*/

        result = request.getRemoteAddr();

        if (!Strings.isNullOrEmpty(result)) {
            return Optional.of(result);
        }
    }

    return Optional.empty();
}