/** * <p>Centers a String in a larger String of size {@code size} * using the space character (' ').</p> * * <p>If the size is less than the String length, the String is returned. * A {@code null} String returns {@code null}. * A negative size is treated as zero.</p> * * <p>Equivalent to {@code center(str, size, " ")}.</p> * * <pre> * StringUtils.center(null, *) = null * StringUtils.center("", 4) = " " * StringUtils.center("ab", -1) = "ab" * StringUtils.center("ab", 4) = " ab " * StringUtils.center("abcd", 2) = "abcd" * StringUtils.center("a", 4) = " a " * </pre> * * @param str the String to center, may be null * @param size the int size of new String, negative treated as zero * @return centered String, {@code null} if null String input */ public static String center(final String str, final int size) { return center(str, size, ' '); }
private static String centerString(String msg) { return "*" + StringUtils.center(msg, 78, " ") + "*"; }
private static String buildMessage( Deque<StackTraceElement[]> openCloseTraces ) { if ( openCloseTraces.isEmpty() ) { return StringUtils.EMPTY; } int separatorLength = 80; String paddingString = "="; ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream( out ); printStream.println(); printStream.println( "Last " + STATEMENT_TRACK_HISTORY_MAX_SIZE + " statements open/close stack traces are:" ); int element = 0; for ( StackTraceElement[] traceElements : openCloseTraces ) { printStream.println( StringUtils.center( "*StackTrace " + element + "*", separatorLength, paddingString ) ); for ( StackTraceElement traceElement : traceElements ) { printStream.println( "\tat " + traceElement ); } printStream.println( StringUtils.center( "", separatorLength, paddingString ) ); printStream.println(); element++; } printStream.println( "All statement open/close stack traces printed." ); return out.toString(); }
String formatCell(Object value, int column) { int width = columnWidths.get(column); TextTableAlignment align = columnAlignments.get(column); Function<Object, String> formatter = columnFormatters.get(column); String content = formatter.apply(value); if (content.length() > width) { // truncate content = StringUtils.abbreviate(content, width); } else if (content.length() < width) { // add padding switch (align) { case LEFT: content = StringUtils.rightPad(content, width); break; case RIGHT: content = StringUtils.leftPad(content, width); break; case CENTER: content = StringUtils.center(content, width); break; } } return content; }
@Test public void testCenter_StringIntChar() { assertNull(StringUtils.center(null, -1, ' ')); assertNull(StringUtils.center(null, 4, ' ')); assertEquals(" ", StringUtils.center("", 4, ' ')); assertEquals("ab", StringUtils.center("ab", 0, ' ')); assertEquals("ab", StringUtils.center("ab", -1, ' ')); assertEquals("ab", StringUtils.center("ab", 1, ' ')); assertEquals(" ", StringUtils.center("", 4, ' ')); assertEquals(" ab ", StringUtils.center("ab", 4, ' ')); assertEquals("abcd", StringUtils.center("abcd", 2, ' ')); assertEquals(" a ", StringUtils.center("a", 4, ' ')); assertEquals(" a ", StringUtils.center("a", 5, ' ')); assertEquals("xxaxx", StringUtils.center("a", 5, 'x')); }
@Test public void testCenter_StringInt() { assertNull(StringUtils.center(null, -1)); assertNull(StringUtils.center(null, 4)); assertEquals(" ", StringUtils.center("", 4)); assertEquals("ab", StringUtils.center("ab", 0)); assertEquals("ab", StringUtils.center("ab", -1)); assertEquals("ab", StringUtils.center("ab", 1)); assertEquals(" ", StringUtils.center("", 4)); assertEquals(" ab ", StringUtils.center("ab", 4)); assertEquals("abcd", StringUtils.center("abcd", 2)); assertEquals(" a ", StringUtils.center("a", 4)); assertEquals(" a ", StringUtils.center("a", 5)); }
@Override public void print(TableModel model) { TextTableFormat format = model.format(); format.configure(model); out.println(file); // print table name String name = model.name(); name = " " + name + " "; int size = Math.max(name.length() + 2, format.tableWidth(cellSeparator)); name = StringUtils.center(name, size, nameSeparator); out.println(name); // print cells model.table().cellSet().forEach(cell -> printCell(model, format, cell)); out.println(); out.println(); }
@Test public void testCenter_StringIntString() { assertNull(StringUtils.center(null, 4, null)); assertNull(StringUtils.center(null, -1, " ")); assertNull(StringUtils.center(null, 4, " ")); assertEquals(" ", StringUtils.center("", 4, " ")); assertEquals("ab", StringUtils.center("ab", 0, " ")); assertEquals("ab", StringUtils.center("ab", -1, " ")); assertEquals("ab", StringUtils.center("ab", 1, " ")); assertEquals(" ", StringUtils.center("", 4, " ")); assertEquals(" ab ", StringUtils.center("ab", 4, " ")); assertEquals("abcd", StringUtils.center("abcd", 2, " ")); assertEquals(" a ", StringUtils.center("a", 4, " ")); assertEquals("yayz", StringUtils.center("a", 4, "yz")); assertEquals("yzyayzy", StringUtils.center("a", 7, "yz")); assertEquals(" abc ", StringUtils.center("abc", 7, null)); assertEquals(" abc ", StringUtils.center("abc", 7, "")); }
logPrefix = StringUtils.center(logPrefix, 40, '-');
private void printNodeFrame(StringBuilder stringBuilder, int frameSize, ClassLoaderNode classLoaderNode, boolean includeDelegateLines, boolean doNotAddHierarchyLines) { if (includeDelegateLines) { stringBuilder.append(repeat(" ", frameSize / 2)).append(repeat("-", frameSize / 2)).append("> "); } stringBuilder.append(repeat("-", frameSize)).append(lineSeparator()); if (includeDelegateLines) { stringBuilder.append(center(doNotAddHierarchyLines ? " " : "|", frameSize)).append(repeat(" ", 2)); } stringBuilder.append("|").append(center(classLoaderNode.getId(), frameSize - 2)).append("|").append(lineSeparator()); if (includeDelegateLines) { stringBuilder.append(center(doNotAddHierarchyLines ? " " : "|", frameSize)).append(repeat(" ", 2)); } stringBuilder.append(repeat("-", frameSize)).append(lineSeparator()); }
/** * Prints a quick summary of the plan to screen. * * @param plans - List of NodePlans. */ static private void printToScreen(List<NodePlan> plans) { System.out.println("\nPlan :\n"); System.out.println(StringUtils.repeat("=", 80)); System.out.println( StringUtils.center("Source Disk", 30) + StringUtils.center("Dest.Disk", 30) + StringUtils.center("Size", 10) + StringUtils.center("Type", 10)); for (NodePlan plan : plans) { for (Step step : plan.getVolumeSetPlans()) { System.out.println(String.format("%s %s %s %s", StringUtils.center(step.getSourceVolume().getPath(), 30), StringUtils.center(step.getDestinationVolume().getPath(), 30), StringUtils.center(step.getSizeString(step.getBytesToMove()), 10), StringUtils.center(step.getDestinationVolume().getStorageType(), 10))); } } System.out.println(StringUtils.repeat("=", 80)); }
boolean isLastNode = classLoaderNodes.empty(); if (!isFirst.get()) { stringBuilder.append(center("^", frameSize)).append(lineSeparator()); stringBuilder.append(center("|", frameSize)).append(lineSeparator()); for (int i = 0; i < classLoaderNode.getDelegateNodes().size(); i++) { ClassLoaderNode delegateNode = classLoaderNode.getDelegateNodes().get(i); stringBuilder.append(center("|", frameSize)).append(lineSeparator()); boolean isLastItem = i == classLoaderNode.getDelegateNodes().size() - 1; printNodeFrame(stringBuilder, frameSize, delegateNode, true,
private String formatHeaderValue(String value, int size) { String result = substring(value, 0, size); result = center(String.format("- - + %s + - -", result), size, ' '); return result; }
/** @param testName Name of the test that we are starting. */ public static void printStartOfTest(final Logger logger, final String testName) { // Format final String paddedName = StringUtils.center("STARTING " + testName, 37); // Print logger.warn("-----------------------------------------"); logger.warn("* *"); logger.warn("* " + paddedName + " *"); logger.warn("* *"); logger.warn("-----------------------------------------"); }
@Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { String str = Objects.toString(var, ""); int size = 80; if (args.length > 0) { size = NumberUtils.toInt(args[0], 80); } return StringUtils.center(str, size); }
@Override public void testFinished ( Description description ) throws Exception { String label = MessageFormat.format ( " /end: {0} ", description.getDisplayName () ); out.println ( " " + StringUtils.center ( label, 110, "=-" ) + "\n" ); }
@Override public void testStarted ( Description description ) throws Exception { String label = MessageFormat.format ( " {0} ", description.getDisplayName () ); out.println ( "\n\n " + StringUtils.center ( label, 110, "=-" ) ); }
private void printHeader() { System.out.println(StringUtils.repeat("*", WIDTH)); System.out.println("*" + StringUtils.repeat(" ", WIDTH - 2) + "*"); System.out.println("*" + StringUtils.center("LOADED WIKIBRAIN DATA:", WIDTH - 2) + "*"); System.out.println("*" + StringUtils.repeat(" ", WIDTH - 2) + "*"); System.out.println("*" + StringUtils.center(" Default language: " + env.getLanguages().getDefaultLanguage(), WIDTH - 2) + "*"); System.out.println("*" + StringUtils.center(" Loaded languages: " + env.getLanguages(), WIDTH - 2) + "*"); System.out.println("*" + StringUtils.repeat(" ", WIDTH - 2) + "*"); System.out.println(StringUtils.repeat("-", WIDTH)); printRow("component", "language", "count", "errors", "modified"); System.out.println(StringUtils.repeat("-", WIDTH)); }
public String title(String title, char ch) { String little = StringUtils.repeat(ch, 2); return String.format( "%s%s%s%s%n%s", separator(ch), little, StringUtils.center(title, LINE_LENGTH - 4), little, separator(ch)); }
public static String center(ActionContext actionContext){ Thing self = actionContext.getObject("self"); String str = (String) self.doAction("getStr", actionContext); String padStr = (String) self.doAction("getPadStr", actionContext); Integer size = (Integer) self.doAction("getSize", actionContext); return StringUtils.center(str, size, padStr); }