+ //
+ // PRIVATE METHODS
+ //
+
+ private Collection<Command> findEligibleCommands(String name) {
+ ImmutableSet.Builder<Command> eligibleCommands = ImmutableSet.builder();
+ for (Command command : commands) {
+ if (command.getName().toLowerCase().startsWith(name.toLowerCase())) {
+ eligibleCommands.add(command);
+ }
+ for (String alias : command.getAliases()) {
+ if (alias.toLowerCase().startsWith(name.toLowerCase())) {
+ eligibleCommands.add(command);
+ }
+ }
+ }
+ return eligibleCommands.build();
+ }
+
+ /**
+ * Writes the given line followed by an LF to the {@link #writer}.
+ *
+ * @param line
+ * The line to write
+ * @throws IOException
+ * if an I/O error occurs
+ */
+ private void writeLine(String line) throws IOException {
+ writer.write(line + "\n");
+ writer.flush();
+ }
+
+ /**
+ * Converts large numbers into a human-friendly format, by showing SI prefixes
+ * for ×1024 (K), ×1048576 (M), and ×1073741824 (G).
+ *
+ * @param number
+ * The number to convert
+ * @return The converted number
+ */
+ static String f(long number) {
+ if (number >= (1 << 30)) {
+ return String.format("%.1fG", number / (double) (1 << 30));
+ }
+ if (number >= (1 << 20)) {
+ return String.format("%.1fM", number / (double) (1 << 20));
+ }
+ if (number >= (1 << 10)) {
+ return String.format("%.1fK", number / (double) (1 << 10));
+ }
+ return String.format("%dB", number);