com.google.common.base.Strings.nullToEmpty()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(141)

本文整理了Java中com.google.common.base.Strings.nullToEmpty()方法的一些代码示例,展示了Strings.nullToEmpty()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Strings.nullToEmpty()方法的具体详情如下:
包路径:com.google.common.base.Strings
类名称:Strings
方法名:nullToEmpty

Strings.nullToEmpty介绍

[英]Returns the given string if it is non-null; the empty string otherwise.
[中]如果给定字符串非空,则返回该字符串;否则,请输入空字符串。

代码示例

代码示例来源:origin: ctripcorp/apollo

public static boolean isBlank(String str) {
 return Strings.nullToEmpty(str).trim().isEmpty();
}

代码示例来源:origin: prestodb/presto

private static String trimEmptyToNull(String value)
{
  return emptyToNull(nullToEmpty(value).trim());
}

代码示例来源:origin: prestodb/presto

public PrestoNode(String nodeIdentifier, URI httpUri, NodeVersion nodeVersion, boolean coordinator)
{
  nodeIdentifier = emptyToNull(nullToEmpty(nodeIdentifier).trim());
  this.nodeIdentifier = requireNonNull(nodeIdentifier, "nodeIdentifier is null or empty");
  this.httpUri = requireNonNull(httpUri, "httpUri is null");
  this.nodeVersion = requireNonNull(nodeVersion, "nodeVersion is null");
  this.coordinator = coordinator;
}

代码示例来源:origin: prestodb/presto

public static Set<String> parseClientTags(String clientTagsString)
{
  Splitter splitter = Splitter.on(',').trimResults().omitEmptyStrings();
  return ImmutableSet.copyOf(splitter.split(nullToEmpty(clientTagsString)));
}

代码示例来源:origin: prestodb/presto

private Set<String> parseClientTags(HttpServletRequest servletRequest)
{
  Splitter splitter = Splitter.on(',').trimResults().omitEmptyStrings();
  return ImmutableSet.copyOf(splitter.split(nullToEmpty(servletRequest.getHeader(PRESTO_CLIENT_TAGS))));
}

代码示例来源:origin: prestodb/presto

public static Optional<String> defaultCredentialCachePath()
  {
    String value = nullToEmpty(System.getenv("KRB5CCNAME"));
    if (value.startsWith(FILE_PREFIX)) {
      value = value.substring(FILE_PREFIX.length());
    }
    return Optional.ofNullable(emptyToNull(value));
  }
}

代码示例来源:origin: prestodb/presto

private Set<String> parseClientCapabilities(HttpServletRequest servletRequest)
{
  Splitter splitter = Splitter.on(',').trimResults().omitEmptyStrings();
  return ImmutableSet.copyOf(splitter.split(nullToEmpty(servletRequest.getHeader(PRESTO_CLIENT_CAPABILITIES))));
}

代码示例来源:origin: prestodb/presto

public static String keyFromPath(Path path)
{
  checkArgument(path.isAbsolute(), "Path is not absolute: %s", path);
  String key = nullToEmpty(path.toUri().getPath());
  if (key.startsWith(PATH_SEPARATOR)) {
    key = key.substring(PATH_SEPARATOR.length());
  }
  if (key.endsWith(PATH_SEPARATOR)) {
    key = key.substring(0, key.length() - PATH_SEPARATOR.length());
  }
  return key;
}

代码示例来源:origin: prestodb/presto

private static void assertExceptionMessage(String sql, Exception exception, @Language("RegExp") String regex)
{
  if (!nullToEmpty(exception.getMessage()).matches(regex)) {
    fail(format("Expected exception message '%s' to match '%s' for query: %s", exception.getMessage(), regex, sql), exception);
  }
}

代码示例来源:origin: prestodb/presto

private static void assertExceptionMessage(String sql, Exception exception, @Language("RegExp") String regex)
{
  if (!nullToEmpty(exception.getMessage()).matches(regex)) {
    fail(format("Expected exception message '%s' to match '%s' for query: %s", exception.getMessage(), regex, sql), exception);
  }
}

代码示例来源:origin: apache/incubator-druid

@Nullable
public static String nullToEmptyIfNeeded(@Nullable String value)
{
 //CHECKSTYLE.OFF: Regexp
 return replaceWithDefault() ? Strings.nullToEmpty(value) : value;
 //CHECKSTYLE.ON: Regexp
}

代码示例来源:origin: prestodb/presto

private static Slice slice(@Nullable String s)
{
  return utf8Slice(nullToEmpty(s));
}

代码示例来源:origin: prestodb/presto

private static boolean isPrestoQueryInvalid(SQLException e)
{
  for (Throwable t = e.getCause(); t != null; t = t.getCause()) {
    if (t.toString().contains(".SemanticException:")) {
      return true;
    }
    if (t.toString().contains(".ParsingException:")) {
      return true;
    }
    if (nullToEmpty(t.getMessage()).matches("Function .* not registered")) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: Graylog2/graylog2-server

private String dumpConfiguration(final Map<String, String> configMap) {
  final StringBuilder sb = new StringBuilder();
  sb.append("# Configuration of graylog2-").append(commandName).append(" ").append(version).append(System.lineSeparator());
  sb.append("# Generated on ").append(Tools.nowUTC()).append(System.lineSeparator());
  for (Map.Entry<String, String> entry : configMap.entrySet()) {
    sb.append(entry.getKey()).append('=').append(nullToEmpty(entry.getValue())).append(System.lineSeparator());
  }
  return sb.toString();
}

代码示例来源:origin: prestodb/presto

private static Set<String> possibleIdentifiers()
{
  ImmutableSet.Builder<String> names = ImmutableSet.builder();
  Vocabulary vocabulary = SqlBaseLexer.VOCABULARY;
  for (int i = 0; i <= vocabulary.getMaxTokenType(); i++) {
    String name = nullToEmpty(vocabulary.getLiteralName(i));
    Matcher matcher = IDENTIFIER.matcher(name);
    if (matcher.matches()) {
      names.add(matcher.group(1));
    }
  }
  return names.build();
}

代码示例来源:origin: prestodb/presto

public static String preprocessQuery(Optional<String> catalog, Optional<String> schema, String query)
    throws QueryPreprocessorException
{
  Duration timeout = DEFAULT_PREPROCESSOR_TIMEOUT;
  String timeoutEnvironment = nullToEmpty(System.getenv(ENV_PREPROCESSOR_TIMEOUT)).trim();
  if (!timeoutEnvironment.isEmpty()) {
    timeout = Duration.valueOf(timeoutEnvironment);
  }
  String preprocessorCommand = System.getenv(ENV_PREPROCESSOR);
  if (emptyToNull(preprocessorCommand) == null) {
    return query;
  }
  return preprocessQuery(catalog, schema, query, ImmutableList.of("/bin/sh", "-c", preprocessorCommand), timeout);
}

代码示例来源:origin: google/guava

public void testNullToEmpty() {
 assertEquals("", Strings.nullToEmpty(null));
 assertEquals("", Strings.nullToEmpty(""));
 assertEquals("a", Strings.nullToEmpty("a"));
}

代码示例来源:origin: prestodb/presto

public static HdfsParquetDataSource buildHdfsParquetDataSource(FileSystem fileSystem, Path path, long start, long length, long fileSize, FileFormatDataSourceStats stats)
{
  try {
    FSDataInputStream inputStream = fileSystem.open(path);
    return new HdfsParquetDataSource(new ParquetDataSourceId(path.toString()), fileSize, inputStream, stats);
  }
  catch (Exception e) {
    if (nullToEmpty(e.getMessage()).trim().equals("Filesystem closed") ||
        e instanceof FileNotFoundException) {
      throw new PrestoException(HIVE_CANNOT_OPEN_SPLIT, e);
    }
    throw new PrestoException(HIVE_CANNOT_OPEN_SPLIT, format("Error opening Hive split %s (offset=%s, length=%s): %s", path, start, length, e.getMessage()), e);
  }
}

代码示例来源:origin: prestodb/presto

public void assertFails(@Language("SQL") String sql, @Language("RegExp") String expectedMessageRegExp)
{
  try {
    runner.execute(runner.getDefaultSession(), sql).toTestTypes();
    fail(format("Expected query to fail: %s", sql));
  }
  catch (RuntimeException exception) {
    if (!nullToEmpty(exception.getMessage()).matches(expectedMessageRegExp)) {
      fail(format("Expected exception message '%s' to match '%s' for query: %s", exception.getMessage(), expectedMessageRegExp, sql), exception);
    }
  }
}

代码示例来源:origin: prestodb/presto

public static void fromMetastoreApiStorageDescriptor(StorageDescriptor storageDescriptor, Storage.Builder builder, String tablePartitionName)
{
  SerDeInfo serdeInfo = storageDescriptor.getSerdeInfo();
  if (serdeInfo == null) {
    throw new PrestoException(HIVE_INVALID_METADATA, "Table storage descriptor is missing SerDe info");
  }
  builder.setStorageFormat(StorageFormat.createNullable(serdeInfo.getSerializationLib(), storageDescriptor.getInputFormat(), storageDescriptor.getOutputFormat()))
      .setLocation(nullToEmpty(storageDescriptor.getLocation()))
      .setBucketProperty(HiveBucketProperty.fromStorageDescriptor(storageDescriptor, tablePartitionName))
      .setSkewed(storageDescriptor.isSetSkewedInfo() && storageDescriptor.getSkewedInfo().isSetSkewedColNames() && !storageDescriptor.getSkewedInfo().getSkewedColNames().isEmpty())
      .setSerdeParameters(serdeInfo.getParameters() == null ? ImmutableMap.of() : serdeInfo.getParameters());
}

相关文章