List of usage examples for com.google.common.base Strings repeat
public static String repeat(String string, int count)
From source file:io.github.cubedtear.jcubit.logging.LogFormatter.java
/** * Formats the given message.// ww w . java 2 s . com * * @param level The level of the message. * @param format The format string. * @param message The log message. * @param t The throwable thrown. * @param loggerName The name of the logger. * @return The formatted log message. * @see LogFormatter */ public static String formatLogMessage(@NotNull LogLevel level, @NotNull String format, @Nullable String message, @Nullable Throwable t, @Nullable String loggerName) { boolean inCondition = false; boolean conditionMet = false; StringBuilder out = new StringBuilder(); char[] chars = format.toCharArray(); for (int i = 0; i < chars.length; i++) { boolean notLast = i < chars.length - 1; char c = chars[i]; // Check for ending the condition environment if (inCondition && c == DELIMITER_RIGHT) { inCondition = false; conditionMet = false; continue; } // Skip if the condition is unmet if (inCondition && !conditionMet) continue; if (c == BEGIN_TOKEN && notLast) { // Check if we are in a replace-token i++; c = chars[i]; switch (c) { case BEGIN_TOKEN: out.append(BEGIN_TOKEN); break; case TOKEN_MESSAGE: out.append(message); break; case TOKEN_DATE: if (chars[i + 1] == DELIMITER_LEFT) { i++; String dFormat = ""; while (i < chars.length - 1) { i++; c = chars[i]; if (c == DELIMITER_RIGHT) break; dFormat += c; } if (!dateFormats.containsKey(dFormat)) { dateFormats.put(dFormat, new SimpleDateFormat(dFormat)); } out.append(dateFormats.get(dFormat).format(new Date())); } else { out.append(DF.format(new Date())); } break; case TOKEN_LEVEL: out.append(level.name()); break; case TOKEN_FULL_LEVEL: out.append("[").append(level.name()).append("]"); out.append(Strings.repeat(" ", LogLevel.maxNameLength - level.name().length())); break; case TOKEN_THROWABLE: if (t != null) { out.append(Throwables.getStackTraceAsString(t)); } break; case TOKEN_THREAD: out.append(Thread.currentThread().getName()); break; case TOKEN_NAME: out.append(loggerName); break; } } else if (c == BEGIN_CONDITION) { // Check if we are starting a condition environment inCondition = true; // Read the condition String condition = ""; while (i < chars.length - 1) { // Safe-check not to get an ArrayIndexOutOfBoundsException i++; c = chars[i]; if (c == DELIMITER_LEFT) break; // If we reach the beginning if the environment, stop condition += c; } // Parse the condition if (condition.equalsIgnoreCase(String.valueOf(TOKEN_THROWABLE))) { conditionMet = t != null; } else if (condition.startsWith(String.valueOf(TOKEN_LEVEL)) || condition.startsWith(String.valueOf(TOKEN_FULL_LEVEL))) { String cLevel = condition.substring(1); conditionMet = LogLevel.getLevel(cLevel) == level; } else if (condition.equalsIgnoreCase(String.valueOf(TOKEN_MESSAGE))) { conditionMet = !Strings.isNullOrEmpty(message); } else if (condition.equalsIgnoreCase(String.valueOf(TOKEN_NAME))) { conditionMet = !Strings.isNullOrEmpty(loggerName); } } else { out.append(c); } } return out.toString(); }
From source file:org.opendaylight.infrautils.utils.TablePrinter.java
private static String columnSeparator() { String space = Strings.repeat(" ", SPACE_BETWEEN_COLUMNS); return space + "|" + space; }
From source file:com.palantir.typescript.text.AutoEditStrategy.java
private String createIndentationText(int indentation) { checkArgument(indentation >= 0);//from ww w. j a v a 2 s. c o m int tabs = 0; int spaces = 0; if (this.spacesForTabs) { spaces = indentation; } else { tabs = indentation / this.tabWidth; spaces = indentation % this.tabWidth; } return Strings.repeat("\t", tabs) + Strings.repeat(" ", spaces); }
From source file:google.registry.tools.GetSchemaTreeCommand.java
private void printSubclasses(Class<?> parent, int indent) { for (Class<?> clazz : superclassToSubclasses.get(parent)) { System.out.println(/*from w ww. ja va 2s .c o m*/ new StringBuilder(Strings.repeat(" ", indent)).append("- ").append(getPrintableName(clazz)) .append(clazz.isAnnotationPresent(EntitySubclass.class) ? " (subclass)" : "")); printSubclasses(clazz, indent + 2); printTree(clazz, indent + 2); } }
From source file:com.eucalyptus.auth.login.HmacLoginModuleSupport.java
protected String normalize(final String signature) { final String urldecoded = urldecode(signature); final String decoded = urldecoded.replace(' ', '+'); // url decoding could remove valid b64 characters final String sanitized = sanitize(decoded); final String normalized; int lastBlockLength = sanitized.length() % 4; if (lastBlockLength > 0) { normalized = sanitized + Strings.repeat("=", 4 - lastBlockLength); } else {//from ww w .j a v a 2 s . c o m normalized = sanitized; } return B64.standard.encString(B64.standard.dec(normalized)); }
From source file:com.comphenix.protocol.utility.ChatExtensions.java
/** * Print a flower box around a given message. * @param message - the message to print. * @param marginChar - the character to use as margin. * @param marginWidth - the width (in characters) of the left and right margin. * @param marginHeight - the height (in characters) of the top and buttom margin. * @return Flowerboxed message//from ww w.ja v a 2 s. c o m */ public static String[] toFlowerBox(String[] message, String marginChar, int marginWidth, int marginHeight) { String[] output = new String[message.length + marginHeight * 2]; int width = getMaximumLength(message); // Margins String topButtomMargin = Strings.repeat(marginChar, width + marginWidth * 2); String leftRightMargin = Strings.repeat(marginChar, marginWidth); // Add left and right margin for (int i = 0; i < message.length; i++) { output[i + marginHeight] = leftRightMargin + Strings.padEnd(message[i], width, ' ') + leftRightMargin; } // Insert top and bottom margin for (int i = 0; i < marginHeight; i++) { output[i] = topButtomMargin; output[output.length - i - 1] = topButtomMargin; } return output; }
From source file:org.carrot2.core.test.assertions.ClusterListAssertion.java
private List<String> labelList(List<String> list, int indent, List<Cluster> clusters) { for (Cluster c : clusters) { list.add(Strings.repeat(" ", indent) + c.getLabel()); labelList(list, indent + 1, c.getSubclusters()); }// w w w. j a va 2s . c om return list; }
From source file:co.cask.cdap.cli.command.system.HelpCommand.java
/** * Prints the given command pattern with text wrapping at the given column width. It prints multiple lines as: * * <pre>{@code//from w w w. ja v a2 s.c om * command sub-command <arg1> <arg2> * <arg3> [<optional-long-arg4>] * } * </pre> * * @param pattern the command pattern to print * @param output the {@link PrintStream} to write to * @param colWidth width of the column * @param prefixSpaces number of spaces as the prefix for each line printed */ private void printPattern(String pattern, PrintStream output, int colWidth, int prefixSpaces) { String prefix = Strings.repeat(" ", prefixSpaces); colWidth -= prefixSpaces; if (pattern.length() <= colWidth) { output.printf("%s%s", prefix, pattern); output.println(); return; } // Find the first <argument>, it is used for alignment in second line and onward. int startIdx = pattern.indexOf('<'); // If no '<', it shouldn't reach here (it should be a short command). If it does, just print it. if (startIdx < 0) { output.printf("%s%s", prefix, pattern); output.println(); return; } // First line should at least include the first <argument> int idx = pattern.lastIndexOf('>', colWidth) + 1; if (idx <= 0) { idx = pattern.indexOf('>', startIdx) + 1; if (idx <= 0) { idx = pattern.length(); } } // Make sure we include the closing ] of an optional argument if (idx < pattern.length() && pattern.charAt(idx) == ']') { idx++; } output.printf("%s%s", prefix, pattern.substring(0, idx)); output.println(); if ((idx + 1) < pattern.length()) { // For rest of the line, align them to the startIdx wrappedPrint(pattern.substring(idx + 1), output, colWidth, startIdx + prefixSpaces); } }
From source file:ezbake.deployer.utilities.Utilities.java
private static String ppSet(Set<?> set, int depth) { List<String> entries = Lists.newArrayList(); final String indent = Strings.repeat(" ", depth); for (Object item : set) { entries.add(indent + ppObject(item, depth)); }//w w w . j a v a 2 s.c o m return entries.isEmpty() ? "{}" : s("{\n%s\n%s}", Joiner.on(",\n").join(entries), Strings.repeat(" ", depth - 1)); }
From source file:com.github.jsdossier.Main.java
@VisibleForTesting static void run(String[] args, FileSystem fileSystem) { Flags flags = Flags.parse(args, fileSystem); Config config = null;/*w w w . j a v a 2 s . co m*/ try (InputStream stream = newInputStream(flags.config)) { config = Config.load(stream, fileSystem); } catch (IOException e) { e.printStackTrace(System.err); System.exit(-1); } if (flags.printConfig) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); String header = " Configuration "; int len = header.length(); String pad = Strings.repeat("=", len / 2); System.err.println(pad + header + pad); System.err.println(gson.toJson(config.toJson())); System.err.println(Strings.repeat("=", 79)); System.exit(1); } Iterable<String> standardFlags = STANDARD_FLAGS; if (config.isStrict()) { standardFlags = transform(standardFlags, new Function<String, String>() { @Override public String apply(String input) { return input.replace("--jscomp_warning", "--jscomp_error"); } }); } ImmutableList<String> compilerFlags = ImmutableList.<String>builder() .addAll(transform(config.getSources(), toFlag("--js="))) .addAll(transform(config.getModules(), toFlag("--js="))) .addAll(transform(config.getExterns(), toFlag("--externs="))) .add("--language_in=" + config.getLanguage().getName()).addAll(standardFlags).build(); PrintStream nullStream = new PrintStream(ByteStreams.nullOutputStream()); args = compilerFlags.toArray(new String[compilerFlags.size()]); Logger log = Logger.getLogger(Main.class.getPackage().getName()); log.setLevel(Level.WARNING); log.addHandler(new Handler() { @Override public void publish(LogRecord record) { System.err.printf("[%s][%s] %s\n", record.getLevel(), record.getLoggerName(), record.getMessage()); } @Override public void flush() { } @Override public void close() { } }); Main main = new Main(args, nullStream, System.err, config); main.runCompiler(); }