本文整理汇总了Java中com.google.common.base.Strings.padStart方法的典型用法代码示例。如果您正苦于以下问题:Java Strings.padStart方法的具体用法?Java Strings.padStart怎么用?Java Strings.padStart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.base.Strings
的用法示例。
在下文中一共展示了Strings.padStart方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doFilter
import com.google.common.base.Strings; //导入方法依赖的package包/类
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// retrieve userId and set
if (request instanceof HttpServletRequest) {
Principal principal = ((HttpServletRequest) request).getUserPrincipal();
if (principal != null) {
String ppal = principal.getName();
if (hashAlgorithm == null || "none".equalsIgnoreCase(hashAlgorithm)) {
// no hash
} else if ("hashcode".equalsIgnoreCase(hashAlgorithm)) {
// simply hashcode
ppal = Strings.padStart(Integer.toHexString(ppal.hashCode()), 8, '0');
} else {
// hexadecimal hash
try {
MessageDigest digest = MessageDigest.getInstance(hashAlgorithm);
ppal = BaseEncoding.base16().encode(digest.digest(ppal.getBytes()));
} catch (NoSuchAlgorithmException e) {
throw new ServletException(e);
}
}
// add to MDC and request attribute
MDC.put(mdcName, ppal);
request.setAttribute(attributeName, ppal);
}
}
try {
chain.doFilter(request, response);
} finally {
MDC.remove(mdcName);
}
}
示例2: pad
import com.google.common.base.Strings; //导入方法依赖的package包/类
private static String pad(int padding) {
return Strings.padStart("", padding * 4, ' ');
}
示例3: toUnicodeKeyword
import com.google.common.base.Strings; //导入方法依赖的package包/类
/**
* 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) + "'";
}
示例4: getRandomZeroPaddedNumber
import com.google.common.base.Strings; //导入方法依赖的package包/类
private static String getRandomZeroPaddedNumber(int min, int max, int length) {
return Strings.padStart(
String.format("%d", random.getRandomInteger(min, max)),
length,
'0'
);
}
示例5: asZeroPaddedHexString
import com.google.common.base.Strings; //导入方法依赖的package包/类
public String asZeroPaddedHexString(int expectedLength) {
return Strings.padStart(asHexString(), expectedLength, '0');
}
示例6: formatCell
import com.google.common.base.Strings; //导入方法依赖的package包/类
private static String formatCell(String value, int maxWidth) {
return Strings.padStart(value, maxWidth + 1, ' ') + " ";
}
示例7: sendModuleSummary
import com.google.common.base.Strings; //导入方法依赖的package包/类
/**
* Sends a module summary (similar to the Maven Reactor Summary)
*/
public void sendModuleSummary() {
if(logger == null) return;
List<String> summaryMessages = new ArrayList<>();
summaryMessages.add(Strings.repeat("-", 75));
summaryMessages.add("Module Summary: ");
summaryMessages.add(" ");
// for averages
double successCount = 0;
double totalTime = 0;
for(Module m : getModules()) {
String name = Strings.padEnd(m.getName() + " ", 25, '.');
String state = (m.isEnabled() ? ConsoleColor.GREEN + "SUCCESS" : ConsoleColor.RED + "FAILURE") + ConsoleColor.RESET;
double time = (double) m.getEnableTime() / 1000;
// averages
if(m.isEnabled()) successCount++;
totalTime += time;
String timeString = "[ " + Strings.padStart(StringUtil.applyDecimalLength(time, 3), 6, ' ') + " s]";
String error = m.isEnabled() ? "" : " (" + (m.getErrorReason() != null ? m.getErrorReason().getClass().getSimpleName() : "Not Enabled") + ")";
summaryMessages.add(name + " " + state + " " + timeString + error);
}
summaryMessages.add(" ");
String totalTimeString = NumberUtil.round(totalTime, 4);
summaryMessages.add("Total time: " + totalTimeString + "s");
// success
double successRate = successCount / getModules().size() * 100;
String successRateString = (successRate <= 25 ? ConsoleColor.DARK_RED
: successRate <= 50 ? ConsoleColor.RED
: successRate <= 75 ? ConsoleColor.GOLD
: successRate <= 99 ? ConsoleColor.YELLOW
: ConsoleColor.GREEN
) + NumberUtil.round(successRate, 4) + "%" + ConsoleColor.RESET;
// time and footer
String averageTimeString = NumberUtil.round(totalTime / getModules().size(), 4);
summaryMessages.add("Success rate: " + successRateString + " | Average time: " + averageTimeString + "s");
summaryMessages.add(Strings.repeat("-", 75));
// send all messages
summaryMessages.forEach(s -> logger.info(s));
}
示例8: padFloat
import com.google.common.base.Strings; //导入方法依赖的package包/类
private String padFloat(float f){
String formatted = format.format(f);
return Strings.padStart(formatted, 7, ' ');
}
示例9: padInt
import com.google.common.base.Strings; //导入方法依赖的package包/类
private String padInt(float f){
String formatted = intFormat.format(f);
return Strings.padStart(formatted, 5, ' ');
}
示例10: toHex
import com.google.common.base.Strings; //导入方法依赖的package包/类
/**
* 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');
}