Java Code Examples for com.google.common.base.Strings#padStart()

The following examples show how to use com.google.common.base.Strings#padStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a StringColumn with the year and quarter from this column concatenated into a String
 * that will sort lexicographically in temporal order.
 *
 * <p>This simplifies the production of plots and tables that aggregate values into standard
 * temporal units (e.g., you want monthly data but your source data is more than a year long and
 * you don't want months from different years aggregated together).
 */
default StringColumn yearQuarter() {
  StringColumn newColumn = StringColumn.create(this.name() + " year & quarter");
  for (int r = 0; r < this.size(); r++) {
    int c1 = this.getIntInternal(r);
    if (DateColumn.valueIsMissing(c1)) {
      newColumn.appendMissing();
    } else {
      String yq = String.valueOf(PackedLocalDate.getYear(c1));
      yq = yq + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getQuarter(c1)), 2, '0');
      newColumn.append(yq);
    }
  }
  return newColumn;
}
 
Example 2
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a StringColumn with the year and week-of-year derived from this column concatenated
 * into a String that will sort lexicographically in temporal order.
 *
 * <p>This simplifies the production of plots and tables that aggregate values into standard
 * temporal units (e.g., you want monthly data but your source data is more than a year long and
 * you don't want months from different years aggregated together).
 */
default StringColumn hourMinute() {
  StringColumn newColumn = StringColumn.create(this.name() + " hour & minute");
  for (int r = 0; r < this.size(); r++) {
    long c1 = this.getLongInternal(r);
    if (DateTimeColumn.valueIsMissing(c1)) {
      newColumn.append(StringColumnType.missingValueIndicator());
    } else {
      String hm = Strings.padStart(String.valueOf(getHour(c1)), 2, '0');
      hm = hm + ":" + Strings.padStart(String.valueOf(getMinute(c1)), 2, '0');
      newColumn.append(hm);
    }
  }
  return newColumn;
}
 
Example 3
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a StringColumn with the year and week-of-year derived from this column concatenated
 * into a String that will sort lexicographically in temporal order.
 *
 * <p>This simplifies the production of plots and tables that aggregate values into standard
 * temporal units (e.g., you want monthly data but your source data is more than a year long and
 * you don't want months from different years aggregated together).
 */
default StringColumn yearWeek() {
  StringColumn newColumn = StringColumn.create(this.name() + " year & month");
  for (int r = 0; r < this.size(); r++) {
    long c1 = this.getLongInternal(r);
    if (DateTimeColumn.valueIsMissing(c1)) {
      newColumn.append(StringColumnType.missingValueIndicator());
    } else {
      String ym = String.valueOf(getYear(c1));
      ym = ym + "-" + Strings.padStart(String.valueOf(getWeekOfYear(c1)), 2, '0');
      newColumn.append(ym);
    }
  }
  return newColumn;
}
 
Example 4
Source File: TimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a StringColumn with the hour and minute-of-hour derived from this column concatenated
 * into a String that will sort lexicographically in temporal order.
 *
 * <p>This simplifies the production of plots and tables that aggregate values into standard
 * temporal units
 */
default StringColumn hourMinute() {
  StringColumn newColumn = StringColumn.create(this.name() + " hour & minute");
  for (int r = 0; r < this.size(); r++) {
    int c1 = this.getIntInternal(r);
    if (TimeColumn.valueIsMissing(c1)) {
      newColumn.append(StringColumnType.missingValueIndicator());
    } else {
      String hm = Strings.padStart(String.valueOf(PackedLocalTime.getHour(c1)), 2, '0');
      hm = hm + "-" + Strings.padStart(String.valueOf(PackedLocalTime.getMinute(c1)), 2, '0');
      newColumn.append(hm);
    }
  }
  return newColumn;
}
 
Example 5
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a StringColumn with the year and day-of-year derived from this column concatenated into
 * a String that will sort lexicographically in temporal order.
 *
 * <p>This simplifies the production of plots and tables that aggregate values into standard
 * temporal units (e.g., you want monthly data but your source data is more than a year long and
 * you don't want months from different years aggregated together).
 */
default StringColumn yearDay() {
  StringColumn newColumn = StringColumn.create(this.name() + " year & month");
  for (int r = 0; r < this.size(); r++) {
    int c1 = this.getIntInternal(r);
    if (DateColumn.valueIsMissing(c1)) {
      newColumn.appendMissing();
    } else {
      String ym = String.valueOf(PackedLocalDate.getYear(c1));
      ym = ym + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getDayOfYear(c1)), 3, '0');
      newColumn.append(ym);
    }
  }
  return newColumn;
}
 
Example 6
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a StringColumn with the year and week-of-year derived from this column concatenated
 * into a String that will sort lexicographically in temporal order.
 *
 * <p>This simplifies the production of plots and tables that aggregate values into standard
 * temporal units (e.g., you want monthly data but your source data is more than a year long and
 * you don't want months from different years aggregated together).
 */
default StringColumn yearWeek() {
  StringColumn newColumn = StringColumn.create(this.name() + " year & month");
  for (int r = 0; r < this.size(); r++) {
    int c1 = this.getIntInternal(r);
    if (DateColumn.valueIsMissing(c1)) {
      newColumn.appendMissing();
    } else {
      String ym = String.valueOf(PackedLocalDate.getYear(c1));
      ym = ym + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getWeekOfYear(c1)), 2, '0');
      newColumn.append(ym);
    }
  }
  return newColumn;
}
 
Example 7
Source File: TOTPBuilder.java    From oath with MIT License 5 votes vote down vote up
/**
 * Returns the Time-based One-time Password value against an arbitrary
 * {@code time} using the set of parameters configured in this builder. The
 * value will contain {@link #digits(int)} digits.
 *
 * @param time
 *            the time (in milliseconds)
 *
 * @return the Time-based One-time Password value as numeric String in base
 *         10 that includes {@link #digits(int)} digits.
 */
private String generateTOTP(long time) {
    // Calculate the number of time steps between the initial counter time
    // (i.e. T0 = 0 = Unix epoch) and the specified 'time'.
    final long tc = (long) Math.floor(time / timeStep);

    // Using the counter
    // First 8 bytes are for the movingFactor
    // Compliant with base RFC 4226 (HOTP)
    String timeInHex = Strings.padStart(Long.toHexString(tc).toUpperCase(), 16, '0');

    // Step 1: Generate the HMAC-SHA hash.
    byte[] msg = BaseEncoding.base16().decode(timeInHex);
    byte[] hash = computeHmacSha(key, msg);

    // Step 2: Dynamic Truncation as per section 5.3 of RFC 4226.
    // -
    // "... Let OffsetBits be the low-order 4 bits of String[19] (where String = String[0]...String[19]) ..."
    // -
    // "... Let P = String[OffSet]...String[OffSet+3] ... Return the Last 31 bits of P ..."
    int offset = hash[hash.length - 1] & 0xf;
    int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff);

    // Step 3: Compute the TOTP value.
    int otp = binary % ((int) Math.pow(10, digits));

    // Ensure the TOTP value contains the specified number of digits.
    return Strings.padStart(Integer.toString(otp), digits, '0');
}
 
Example 8
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a StringColumn with the year and month from this column concatenated into a String that
 * will sort lexicographically in temporal order.
 *
 * <p>This simplifies the production of plots and tables that aggregate values into standard
 * temporal units (e.g., you want monthly data but your source data is more than a year long and
 * you don't want months from different years aggregated together).
 */
default StringColumn yearMonth() {
  StringColumn newColumn = StringColumn.create(this.name() + " year & month");
  for (int r = 0; r < this.size(); r++) {
    int c1 = this.getIntInternal(r);
    if (DateColumn.valueIsMissing(c1)) {
      newColumn.appendMissing();
    } else {
      String ym = String.valueOf(PackedLocalDate.getYear(c1));
      ym = ym + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getMonthValue(c1)), 2, '0');
      newColumn.append(ym);
    }
  }
  return newColumn;
}
 
Example 9
Source File: DateMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a StringColumn with the year and week-of-year derived from this column concatenated
 * into a String that will sort lexicographically in temporal order.
 *
 * <p>This simplifies the production of plots and tables that aggregate values into standard
 * temporal units (e.g., you want monthly data but your source data is more than a year long and
 * you don't want months from different years aggregated together).
 */
default StringColumn yearWeek() {
  StringColumn newColumn = StringColumn.create(this.name() + " year & month");
  for (int r = 0; r < this.size(); r++) {
    int c1 = this.getIntInternal(r);
    if (DateColumn.valueIsMissing(c1)) {
      newColumn.appendMissing();
    } else {
      String ym = String.valueOf(PackedLocalDate.getYear(c1));
      ym = ym + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getWeekOfYear(c1)), 2, '0');
      newColumn.append(ym);
    }
  }
  return newColumn;
}
 
Example 10
Source File: PackedLocalDate.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static String toDateString(int date) {
  if (date == Integer.MIN_VALUE) {
    return "";
  }

  return getYear(date)
      + "-"
      + Strings.padStart(Byte.toString(getMonthValue(date)), 2, '0')
      + "-"
      + Strings.padStart(Byte.toString(getDayOfMonth(date)), 2, '0');
}
 
Example 11
Source File: DateTimeMapFunctions.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a StringColumn with the year and month from this column concatenated into a String that
 * will sort lexicographically in temporal order.
 *
 * <p>This simplifies the production of plots and tables that aggregate values into standard
 * temporal units (e.g., you want monthly data but your source data is more than a year long and
 * you don't want months from different years aggregated together).
 */
default StringColumn yearMonth() {
  StringColumn newColumn = StringColumn.create(this.name() + " year & month");
  for (int r = 0; r < this.size(); r++) {
    long c1 = this.getLongInternal(r);
    if (DateTimeColumn.valueIsMissing(c1)) {
      newColumn.append(StringColumnType.missingValueIndicator());
    } else {
      String ym = String.valueOf(getYear(c1));
      ym = ym + "-" + Strings.padStart(String.valueOf(getMonthValue(c1)), 2, '0');
      newColumn.append(ym);
    }
  }
  return newColumn;
}
 
Example 12
Source File: UnicodeKeywordHelper.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates lexer rule for given keyword in order to handle JavaScript's unicode masks. E.g.:
 *
 * <pre>
 * If :
 * 	( 'i' | '\\' 'u' '0''0''6''9' )
 * 	( 'f' | '\\' 'u' '0''0''6''6' );
 * </pre>
 *
 * @param keyword
 *            the keyword as string, e.g. 'if'
 * @return the lexer body, e.g.
 *
 *         <pre>
 * ( 'i' | '\\' 'u' '0''0''6''9' ) ( 'f' | '\\' 'u' '0''0''6''6' )
 *         </pre>
 */
public static String toUnicodeKeyword(String keyword) {
	if (keyword.equals("async ")) {
		keyword = "async";
	}
	if (isIdentifier(keyword)) {
		StringBuilder result = new StringBuilder(keyword.length() * 30);
		for (char c : keyword.toCharArray()) {
			result.append("\n\t( '");
			result.append(c);
			result.append("' | '\\\\' 'u' ");
			String unicodeEscape = Strings.padStart(Integer.toHexString(c), 4, '0');
			for (char u : unicodeEscape.toCharArray()) {
				if ('0' <= u && u <= '9') {
					result.append("'");
					result.append(u);
					result.append("'");
				} else {
					result.append("( '");
					result.append(u);
					result.append("' | '");
					result.append(Character.toUpperCase(u));
					result.append("' )");
				}
			}
			result.append(" )");
		}
		return result.toString();
	}
	return "'" + AntlrGrammarGenUtil.toAntlrString(keyword) + "'";
}
 
Example 13
Source File: ShardHashAdjuster.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
public String adjust(String shardHash) {
  HashCode hashCode = Hashing.md5().hashBytes(shardHash.getBytes());
  String binary =
      Strings.padStart(new BigInteger(1, hashCode.asBytes()).toString(2), BINARY_LENGTH, '0');
  String adjustedBinary = Strings.padEnd(binary.substring(0, reservedBits), BINARY_LENGTH, '0');
  return Strings.padStart(new BigInteger(adjustedBinary, 2).toString(16), HEX_LENGTH, '0');
}
 
Example 14
Source File: LeftPadString.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void left_pad_string_with_zeros_guava () {
	
	String leftPaddedString = Strings.padStart("levelup", 10, '0');
	
	assertEquals("000levelup", leftPaddedString);
	assertEquals(10, leftPaddedString.length());
	assertThat(leftPaddedString, startsWith("0"));
}
 
Example 15
Source File: CollectorTask.java    From hygieia-core with Apache License 2.0 5 votes vote down vote up
protected void log(String text, long start, Integer count) {
    long end = System.currentTimeMillis();
    String elapsed = ((end - start) / 1000) + "s";
    String token2 = "";
    String token3;
    if (count == null) {
        token3 = Strings.padStart(" " + elapsed, 35 - text.length(), ' ');
    } else {
        token2 = Strings.padStart(" " + count.toString(), 25 - text.length(), ' ');
        token3 = Strings.padStart(" " + elapsed, 10, ' ');
    }
    LOGGER.info(text + token2 + token3);
}
 
Example 16
Source File: StripAttachment.java    From james-project with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting static String prependedPrefix(String prefix) {
    return Strings.padStart(prefix, MIN_LENGTH, PAD_CHAR);
}
 
Example 17
Source File: ObjectFileScrubbers.java    From buck with Apache License 2.0 4 votes vote down vote up
private static void putSpaceLeftPaddedString(ByteBuffer buffer, int len, String value) {
  Preconditions.checkState(value.length() <= len);
  value = Strings.padStart(value, len, ' ');
  buffer.put(value.getBytes(Charsets.US_ASCII));
}
 
Example 18
Source File: FaviconHelper.java    From ServerListPlus with GNU General Public License v3.0 4 votes vote down vote up
private static String toHex(long num) {
    return Strings.padStart(Long.toHexString(num), 16, '0');
}
 
Example 19
Source File: AtriumTools.java    From atrium-odl with Apache License 2.0 2 votes vote down vote up
/**
 * Converts a long value to hex string; 16 wide and sans 0x.
 *
 * @param value
 *            long value
 * @param width
 *            string width; zero padded
 * @return hex string
 */
public static String toHex(long value, int width) {
	return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
}
 
Example 20
Source File: AtriumTools.java    From atrium-odl with Apache License 2.0 2 votes vote down vote up
/**
 * Converts a long value to hex string; 16 wide and sans 0x.
 *
 * @param value
 *            long value
 * @return hex string
 */
public static String toHex(long value) {
	return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
}