use of oap.dictionary.DictionaryRoot in project oap by oaplatform.
the class DictionaryMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
final List<Path> paths = Files.fastWildcard(Paths.get(sourceDirectory), "*.json").stream().filter(p -> {
final boolean b = !Arrays.stream(exclude).filter(e -> FilenameUtils.wildcardMatchOnSystem(separatorsToUnix(p.toString()), e)).findAny().isPresent();
if (!b)
getLog().debug("exclude " + p);
return b;
}).collect(toList());
getLog().debug("found " + paths);
paths.forEach(path -> {
getLog().info("dictionary " + path + "...");
final DictionaryRoot dictionary = DictionaryParser.parse(path);
final StringBuilder out = new StringBuilder();
final String enumClass = toEnumName(dictionary.name);
out.append("package " + dictionaryPackage + ";\n\n").append("import oap.dictionary.Dictionary;\n\n").append("import java.util.Map;\n").append("import java.util.Optional;\n").append("import java.util.List;\n\n").append("import static java.util.Collections.emptyList;\n").append("import static java.util.Collections.emptyMap;\n").append("import static java.util.Arrays.asList;\n\n").append("public enum " + enumClass + " implements Dictionary {\n");
final Set<String> properties = dictionary.getValues().stream().flatMap(v -> v.getProperties().keySet().stream()).collect(toSet());
final Map<String, Boolean> optional = properties.stream().collect(toMap(k -> k, k -> dictionary.getValues().stream().anyMatch(v -> !v.containsProperty(k))));
final Map<String, Class<?>> types = properties.stream().collect(toMap(k -> k, k -> dictionary.getValues().stream().filter(v -> v.containsProperty(k)).findAny().get().getProperty(k).get().getClass()));
out.append(dictionary.getValues().stream().map(d -> " " + d.getId() + "(" + convert(d.getExternalId(), dictionary.externalIdAs) + ", " + d.isEnabled() + properties(d.getProperties(), properties, optional, types) + ")").collect(joining(",\n")));
final String externalIdType = dictionary.externalIdAs.javaType.getSimpleName();
out.append(";\n\n").append(" private final " + externalIdType + " externalId;\n").append(" private final boolean enabled;\n\n");
for (String property : properties) {
out.append(" private final " + propertyType(property, optional, types) + " " + property + ";\n");
}
out.append("\n");
for (String property : properties) {
out.append(" public final " + propertyType(property, optional, types) + " " + property + "(){return " + property + ";}\n");
}
out.append("\n " + enumClass + "( " + externalIdType + " externalId, boolean enabled");
final String cParameters = properties.stream().map(p -> propertyType(p, optional, types) + " " + p).collect(joining(", "));
out.append(cParameters.length() > 0 ? ", " : "").append(cParameters + " ) {\n").append(" this.externalId = externalId;\n").append(" this.enabled = enabled;\n");
for (String property : properties) {
out.append(" this." + property + " = " + property + ";\n");
}
out.append(" }\n" + "\n" + " public static " + enumClass + " valueOf( int externalId ) {\n" + " switch( externalId ) {\n");
dictionary.getValues().forEach(d -> {
out.append(" case ").append(d.getExternalId()).append(": return ").append(d.getId()).append(";\n");
});
out.append(" default: ");
if (dictionary.containsValueWithId("UNKNOWN")) {
out.append("return UNKNOWN");
} else {
out.append("throw new java.lang.IllegalArgumentException( \"Unknown id \" + externalId )");
}
out.append(";\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public int getOrDefault( String id, int defaultValue ) {\n" + " return defaultValue;\n" + " }\n" + "\n" + " @Override\n" + " public Integer get( String id ) {\n" + " return null;\n" + " }\n" + "\n" + " @Override\n" + " public String getOrDefault( int externlId, String defaultValue ) {\n" + " return defaultValue;\n" + " }\n" + "\n" + " @Override\n" + " public boolean containsValueWithId( String id ) {\n" + " return false;\n" + " }\n" + "\n" + " @Override\n" + " public List<String> ids() {\n" + " return emptyList();\n" + " }\n" + "\n" + " @Override\n" + " public int[] externalIds() {\n" + " return new int[0];\n" + " }\n" + "\n" + " @Override\n" + " public Map<String, Object> getProperties() {\n" + " return emptyMap();\n" + " }\n" + "\n" + " @Override\n" + " public Optional<? extends Dictionary> getValueOpt( String name ) {\n" + " return Optional.empty();\n" + " }\n" + "\n" + " @Override\n" + " public Dictionary getValue( String name ) {\n" + " return null;\n" + " }\n" + "\n" + " @Override\n" + " public Dictionary getValue( int externalId ) {\n" + " return null;\n" + " }\n" + "\n" + " @Override\n" + " public List<? extends Dictionary> getValues() {\n" + " return emptyList();\n" + " }\n" + "\n" + " @Override\n" + " public String getId() {\n" + " return name();\n" + " }\n" + "\n" + " @Override\n" + " public Optional<Object> getProperty( String name ) {\n" + " return Optional.empty();\n" + " }\n" + "\n" + " @Override\n" + " public boolean isEnabled() {\n" + " return enabled;\n" + " }\n" + "\n" + " @Override\n" + " public int getExternalId() {\n" + " return externalId;\n" + " }\n" + " public " + externalIdType + " externalId() {\n" + " return externalId;\n" + " }\n" + "\n" + " @Override\n" + " public boolean containsProperty( String name ) {\n" + " return false;\n" + " }\n").append("}\n");
final Path outPath = Paths.get(outputDirectory, dictionaryPackage.replace(".", "/"), enumClass + ".java");
if (!java.nio.file.Files.exists(outPath) || !Files.readString(outPath).equals(out.toString())) {
Files.writeString(outPath, IoStreams.Encoding.PLAIN, out.toString());
} else {
getLog().debug(outPath + " is not modified.");
}
});
}
Aggregations