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

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

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

Strings.repeat介绍

[英]Returns a string consisting of a specific number of concatenated copies of an input string. For example, repeat("hey", 3) returns the string "heyheyhey".
[中]返回由输入字符串的特定数量的串联副本组成的字符串。例如,repeat(“hey”,3)返回字符串“heyheyhey”。

代码示例

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

private static String indentString(int indent)
  {
    return Strings.repeat(INDENT, indent);
  }
}

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

private static String indentString(int indent)
{
  return Strings.repeat("    ", indent);
}

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

/**
 * Format an indeterminate progress bar: [       <=>       ]
 */
public static String formatProgressBar(int width, int tick)
{
  int markerWidth = 3; // must be odd >= 3 (1 for each < and > marker, the rest for "="
  int range = width - markerWidth; // "lower" must fall within this range for the marker to fit within the bar
  int lower = tick % range;
  if (((tick / range) % 2) == 1) { // are we going or coming back?
    lower = range - lower;
  }
  return repeat(" ", lower) +
      "<" + repeat("=", markerWidth - 2) + ">" +
      repeat(" ", width - (lower + markerWidth));
}

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

private void print(Integer indentLevel, String value)
  {
    out.println(Strings.repeat(INDENT, indentLevel) + value);
  }
}

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

private static String center(String s, int maxWidth, int padding)
{
  int width = consoleWidth(s);
  checkState(width <= maxWidth, "string width is greater than max width");
  int left = (maxWidth - width) / 2;
  int right = maxWidth - (left + width);
  return repeat(" ", left + padding) + s + repeat(" ", right + padding);
}

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

private void appendLine(String template, Object... arguments)
  {
    result.append(repeat("\t", level)).append(format(template + "\n", arguments));
  }
}

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

private static String align(String s, int maxWidth, int padding, boolean right)
{
  int width = consoleWidth(s);
  checkState(width <= maxWidth, "string width is greater than max width");
  String large = repeat(" ", (maxWidth - width) + padding);
  String small = repeat(" ", padding);
  return right ? (large + s + small) : (small + s + large);
}

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

private static String indentString(int indent)
{
  return Strings.repeat("    ", indent);
}

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

private List<String> createVarcharValuesNoNull()
  {
    List<String> values = new ArrayList<>();
    for (int i = 0; i < ROWS; ++i) {
      values.add(Strings.repeat("0", 4));
    }
    return values;
  }
}

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

public void testRepeat_null() {
 try {
  Strings.repeat(null, 5);
  fail();
 } catch (NullPointerException expected) {
 }
}

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

private void output(int indent, String message, Object... args)
  {
    String formattedMessage = format(message, args);
    result.append(format("%s%s\n", Strings.repeat("    ", indent), formattedMessage));
  }
}

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

@Test
public void testArraySize()
{
  int size = toIntExact(MAX_FUNCTION_MEMORY.toBytes() + 1);
  assertInvalidFunction(
      "array_distinct(ARRAY['" +
          Strings.repeat("x", size) + "', '" +
          Strings.repeat("y", size) + "', '" +
          Strings.repeat("z", size) +
          "'])",
      EXCEEDED_FUNCTION_MEMORY_LIMIT);
}

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

private List<String> createVarcharValuesWithNulls()
  {
    Random random = new Random();
    List<String> values = new ArrayList<>();
    for (int i = 0; i < ROWS; ++i) {
      if (random.nextBoolean()) {
        values.add(Strings.repeat("0", 4));
      }
      else {
        values.add(null);
      }
    }
    return values;
  }
}

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

public void testRepeat() {
 String input = "20";
 assertEquals("", Strings.repeat(input, 0));
 assertEquals("20", Strings.repeat(input, 1));
 assertEquals("2020", Strings.repeat(input, 2));
 assertEquals("202020", Strings.repeat(input, 3));
 assertEquals("", Strings.repeat("", 4));
 for (int i = 0; i < 100; ++i) {
  assertEquals(2 * i, Strings.repeat(input, i).length());
 }
 try {
  Strings.repeat("x", -1);
  fail();
 } catch (IllegalArgumentException expected) {
 }
 try {
  // Massive string
  Strings.repeat("12345678", (1 << 30) + 3);
  fail();
 } catch (ArrayIndexOutOfBoundsException expected) {
 }
}

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

@Test
public void testDeallocate()
    throws Exception
{
  try (Connection connection = createConnection()) {
    for (int i = 0; i < 200; i++) {
      try {
        connection.prepareStatement("SELECT '" + repeat("a", 300) + "'").close();
      }
      catch (Exception e) {
        throw new RuntimeException("Failed at " + i, e);
      }
    }
  }
}

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

public void testReallySimpleFingerprints() {
 assertEquals(8581389452482819506L, fingerprint("test".getBytes(UTF_8)));
 // 32 characters long
 assertEquals(-4196240717365766262L, fingerprint(Strings.repeat("test", 8).getBytes(UTF_8)));
 // 256 characters long
 assertEquals(3500507768004279527L, fingerprint(Strings.repeat("test", 64).getBytes(UTF_8)));
}

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

/**
 * Test for Guava issue 1061: http://code.google.com/p/guava-libraries/issues/detail?id=1061
 *
 * <p>CharStreams.copy was failing to clear its CharBuffer after each read call, which effectively
 * reduced the available size of the buffer each time a call to read didn't fill up the available
 * space in the buffer completely. In general this is a performance problem since the buffer size
 * is permanently reduced, but with certain Reader implementations it could also cause the buffer
 * size to reach 0, causing an infinite loop.
 */
public void testCopyWithReaderThatDoesNotFillBuffer() throws IOException {
 // need a long enough string for the buffer to hit 0 remaining before the copy completes
 String string = Strings.repeat("0123456789", 100);
 StringBuilder b = new StringBuilder();
 // the main assertion of this test is here... the copy will fail if the buffer size goes down
 // each time it is not filled completely
 long copied = CharStreams.copy(newNonBufferFillingReader(new StringReader(string)), b);
 assertEquals(string, b.toString());
 assertEquals(string.length(), copied);
}

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

private void reallyTestAllMatches(CharMatcher matcher, CharSequence s) {
 assertTrue(matcher.matches(s.charAt(0)));
 assertEquals(0, matcher.indexIn(s));
 assertEquals(0, matcher.indexIn(s, 0));
 assertEquals(1, matcher.indexIn(s, 1));
 assertEquals(-1, matcher.indexIn(s, s.length()));
 assertEquals(s.length() - 1, matcher.lastIndexIn(s));
 assertTrue(matcher.matchesAnyOf(s));
 assertTrue(matcher.matchesAllOf(s));
 assertFalse(matcher.matchesNoneOf(s));
 assertEquals("", matcher.removeFrom(s));
 assertEquals(Strings.repeat("z", s.length()), matcher.replaceFrom(s, 'z'));
 assertEquals(Strings.repeat("ZZ", s.length()), matcher.replaceFrom(s, "ZZ"));
 assertEquals("", matcher.trimFrom(s));
 assertEquals(s.length(), matcher.countIn(s));
}

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

private static void printStatement(String sql)
{
  println(sql.trim());
  println("");
  ParsingOptions parsingOptions = new ParsingOptions(AS_DOUBLE /* anything */);
  Statement statement = SQL_PARSER.createStatement(sql, parsingOptions);
  println(statement.toString());
  println("");
  println(SqlFormatter.formatSql(statement, Optional.empty()));
  println("");
  assertFormattedSql(SQL_PARSER, statement);
  println(repeat("=", 60));
  println("");
}

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

private DataTypeTest mysqlCharTypeTest()
{
  return DataTypeTest.create()
      .addRoundTrip(charDataType("char", 1), "")
      .addRoundTrip(charDataType("char", 1), "a")
      .addRoundTrip(charDataType(1), "")
      .addRoundTrip(charDataType(1), "a")
      .addRoundTrip(charDataType(8), "abc")
      .addRoundTrip(charDataType(8), "12345678")
      .addRoundTrip(charDataType(255), repeat("a", 255));
}

相关文章