下面列出了java.util.TreeMap#forEach ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void sendFallback(CommandContext context) {
String botName = Settings.instance().bot().name();
StringBuilder sb = new StringBuilder();
sb.append(CommandIcons.ERROR).append(context.translated("help.fallback")).append("\n\n```markdown\n");
sb.append("< ").append(botName.equals("Kyoko")
? context.translated("help.header.title")
: String.format(context.translated("help.header.title.cust"), botName)).append(" >");
TreeMap<CommandCategory, List<String>> categories = getCategories(context.guild());
categories.forEach((category, commands) -> {
if (!commands.isEmpty()) {
commands.sort(Ordering.usingToString());
sb.append("\n\n# ");
sb.append(context.translated("help.category." + category.name().toLowerCase())).append(" - (").append(commands.size()).append(")\n");
sb.append(Joiner.on(", ").join(commands));
}
});
sb.append("\n```");
context.send(sb.toString());
}
private void sendEmbed(CommandContext context) {
String botName = Settings.instance().bot().name();
String botIcon = Settings.instance().bot().icon();
var eb = context.normalEmbed();
eb.title(botIcon + " " + (botName.equals("Kyoko")
? context.translated("help.header.title")
: String.format(context.translated("help.header.title.cust"), botName)));
eb.description(String.format(context.translated("help.header.subtitle"), Constants.SITE_BASE + "/commands"));
TreeMap<CommandCategory, List<String>> categories = getCategories(context.guild());
categories.forEach((category, commands) -> {
if (!commands.isEmpty()) {
commands.sort(Ordering.usingToString());
eb.field(context.translated("help.category." + category.name().toLowerCase())
+ " - (" + commands.size() + ")", "`" + Joiner.on("`, `").join(commands) + "`", false);
}
});
context.send(eb.build());
}
/**
* Shows the help text for one or more commands.
*
* @param pattern
* the command, or a fragment of it
*/
@MCRCommand(syntax = "help {0}", help = "Show the help text for the commands beginning with {0}.", order = 10)
public static void listKnownCommandsBeginningWithPrefix(String pattern) {
TreeMap<String, List<org.mycore.frontend.cli.MCRCommand>> matchingCommands = MCRCommandManager
.getKnownCommands().entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().stream()
.filter(cmd -> cmd.getSyntax().contains(pattern) || cmd.getHelpText().contains(pattern))
.collect(Collectors.toList()), (k, v) -> k, TreeMap::new));
matchingCommands.entrySet().removeIf(e -> e.getValue().isEmpty());
if (matchingCommands.isEmpty()) {
MCRCommandLineInterface.output("Unknown command:" + pattern);
} else {
MCRCommandLineInterface.output("");
matchingCommands.forEach((grp, cmds) -> {
outputGroup(grp);
cmds.forEach(org.mycore.frontend.cli.MCRCommand::outputHelp);
});
}
}
@Override
public Map<String, String> readProperties() {
if (!IS_WINDOWS) {
return null;
}
try {
// Check if key exists
if (!Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, CONFIG_KEY)) {
return null;
}
// Read values from registry
TreeMap<String, Object> map = Advapi32Util.registryGetValues(WinReg.HKEY_CURRENT_USER, CONFIG_KEY);
Map<String, String> values = new HashMap<>();
map.forEach((String k, Object oV) -> {
String v = Objects.toString(oV, null);
values.put(k.trim(), v != null ? v.trim() : null);
}
);
LogService.getRoot().fine(() -> String.format("Successfully enforced %d settings from the Windows registry.", values.size()));
return values;
} catch (Throwable e) {
LogService.getRoot().log(Level.WARNING, "Failed to access the Windows registry.", e);
return null;
}
}
/** */
private double classify(List<LabeledVector> neighbors, Vector v, boolean weighted) {
Map<Double, Double> clsVotes = new HashMap<>();
for (LabeledVector neighbor : neighbors) {
TreeMap<Double, Double> probableClsLb = ((ProbableLabel)neighbor.label()).clsLbls;
double distance = distanceMeasure.compute(v, neighbor.features());
// we predict class label, not the probability vector (it need here another math with counting of votes)
probableClsLb.forEach((label, probability) -> {
double cnt = clsVotes.containsKey(label) ? clsVotes.get(label) : 0;
clsVotes.put(label, cnt + probability * getClassVoteForVector(weighted, distance));
});
}
return getClassWithMaxVotes(clsVotes);
}
public void test_forEach() throws Exception {
TreeMap<String, String> map = new TreeMap<>();
map.put("one", "1");
map.put("two", "2");
map.put("three", "3");
TreeMap<String, String> output = new TreeMap<>();
map.forEach((k, v) -> output.put(k,v));
assertEquals(map, output);
HashSet<String> setOutput = new HashSet<>();
map.keySet().forEach((k) -> setOutput.add(k));
assertEquals(map.keySet(), setOutput);
setOutput.clear();
map.values().forEach((v) -> setOutput.add(v));
assertEquals(new HashSet<>(map.values()), setOutput);
HashSet<Map.Entry<String,String>> entrySetOutput = new HashSet<>();
map.entrySet().forEach((v) -> entrySetOutput.add(v));
assertEquals(map.entrySet(), entrySetOutput);
}
public DoubleArrayTrieMap(TreeMap<String, T> treeMap) {
ArrayList<String> keys = new ArrayList<>(treeMap.size());
ArrayList<T> values = new ArrayList<>(treeMap.size());
treeMap.forEach((a, b) -> {
keys.add(a);
values.add(b);
});
this.dat = new DoubleArrayTrie(keys);
this.values = values;
}
private void updateAttributesLabelsList(final VerticalLayout attributesLayout,
final Map<String, String> attributes) {
final TreeMap<String, String> sortedAttributes = new TreeMap<>((key1, key2) -> key1.compareToIgnoreCase(key2));
sortedAttributes.putAll(attributes);
sortedAttributes.forEach((key, value) -> {
final HorizontalLayout conAttributeLayout = SPUIComponentProvider.createNameValueLayout(key.concat(" : "),
value == null ? "" : value);
//After Vaadin 8 migration: Enable tooltip again, currently it is set to [null] to avoid cross site scripting.
conAttributeLayout.setDescription(null);
conAttributeLayout.addStyleName("label-style");
attributesLayout.addComponent(conAttributeLayout);
});
}
@SuppressWarnings("deprecation")
@Override
public void execute(@NotNull CommandContext context) {
StringBuilder sb = new StringBuilder();
TreeMap<CommandCategory, List<Command>> categories = Arrays.stream(CommandCategory.values()).collect(Collectors.toMap(c -> c, c -> new ArrayList<>(), (a, b) -> b, TreeMap::new));
commandManager.registered().stream().filter(command -> command.getCategory() != null).forEach(command -> categories.get(command.getCategory()).add(command));
categories.forEach((category, commands) -> {
if (commands.isEmpty()) return;
sb.append("<h2>").append(context.translated("help.category." + category.name().toLowerCase())).append("</h2>\n\n");
sb.append("\t<div class=\"table-responsive-md\">\n" +
"\t\t<table class=\"table table-dark\">\n" +
"\t\t\t<thead>\n" +
"\t\t\t\t<tr>\n" +
"\t\t\t\t\t<th style=\"width: 25%\" scope=\"col\">").append(context.translated("generic.command")).append("</th>\n" +
"\t\t\t\t\t<th style=\"width: 25%\" scope=\"col\">").append(context.translated("generic.aliases")).append("</th>\n" +
"\t\t\t\t\t<th style=\"width: 25%\" scope=\"col\">").append(context.translated("generic.description")).append("</th>\n" +
"\t\t\t\t\t<th style=\"width: 25%\" scope=\"col\">").append(StringEscapeUtils.escapeHtml4(context.translated("generic.usage"))).append("</th>\n" +
"\t\t\t\t</tr>\n" +
"\t\t\t</thead>\n" +
"\t\t\t<tbody>\n");
commands.forEach(cmd -> {
sb.append("\t\t\t\t<tr>\n\t\t\t\t\t<td>")
.append(cmd.getName()).append("</td>\n\t\t\t\t\t<td>")
.append(cmd.getAliases() == null || cmd.getAliases().length == 0 ? "(" + context.translated("generic.none") + ")" :
Joiner.on(", ").join(cmd.getAliases())).append("</td>\n\t\t\t\t\t<td>")
.append(context.translated(cmd.getDescription()).replaceAll("(\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])", "<a href=\"$1\">$1</a>")).append("</td>\n\t\t\t\t\t<td><code>")
.append("ky!").append(cmd.getName()).append(" ").append(StringEscapeUtils.escapeHtml4(context.translated(cmd.getUsage()))).append("</code></td>\n\t\t\t\t</tr>\n");
});
sb.append("\t\t\t</tbody>\n" +
"\t\t</table>\n" +
"\t</div>\n");
});
Path file = Paths.get("docs_generated_" + StringUtil.prettyPeriod(System.currentTimeMillis()).replace(":", "") + ".html");
try {
Files.write(file, sb.toString().getBytes(Charsets.UTF_8), StandardOpenOption.CREATE_NEW);
context.send(CommandIcons.SUCCESS + "Docs generated! `" + file.toFile().getName() + "`");
} catch (IOException e) {
context.send(CommandIcons.ERROR + "Error writing file: `" + e.getMessage() + "`");
logger.error("Error writing file!", e);
}
}