From b8d832a060b6a489ca3f3081a46214b3e3df7bcb Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Mon, 2 Dec 2019 17:26:38 +0300 Subject: [PATCH] Add Stats utility --- .../java/ru/noties/enhance/ApiInfoStore.java | 15 +++ .../ru/noties/enhance/ApiInfoStoreImpl.java | 16 +-- src/main/java/ru/noties/enhance/Enhance.java | 8 ++ src/main/java/ru/noties/enhance/Stats.java | 106 ++++++++++++++++++ 4 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 src/main/java/ru/noties/enhance/Stats.java diff --git a/src/main/java/ru/noties/enhance/ApiInfoStore.java b/src/main/java/ru/noties/enhance/ApiInfoStore.java index dff6b37..8df6c82 100644 --- a/src/main/java/ru/noties/enhance/ApiInfoStore.java +++ b/src/main/java/ru/noties/enhance/ApiInfoStore.java @@ -3,6 +3,8 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.io.File; +import java.util.HashMap; +import java.util.Map; public abstract class ApiInfoStore { @@ -11,6 +13,16 @@ public static ApiInfoStore create(@Nonnull File apiVersions) { return new ApiInfoStoreImpl(apiVersions); } + static class TypeVersion extends ApiInfo { + + final Map fields = new HashMap<>(3); + final Map methods = new HashMap<>(3); + + TypeVersion(ApiVersion since, ApiVersion deprecated) { + super(since, deprecated); + } + } + @Nullable public abstract ApiInfo type(@Nonnull String type); @@ -19,4 +31,7 @@ public static ApiInfoStore create(@Nonnull File apiVersions) { @Nullable public abstract ApiInfo method(@Nonnull String type, @Nonnull String signature); + + @Nonnull + public abstract Map info(); } diff --git a/src/main/java/ru/noties/enhance/ApiInfoStoreImpl.java b/src/main/java/ru/noties/enhance/ApiInfoStoreImpl.java index cee57c9..89c134d 100644 --- a/src/main/java/ru/noties/enhance/ApiInfoStoreImpl.java +++ b/src/main/java/ru/noties/enhance/ApiInfoStoreImpl.java @@ -15,16 +15,6 @@ class ApiInfoStoreImpl extends ApiInfoStore { - private static class TypeVersion extends ApiInfo { - - private final Map fields = new HashMap<>(3); - private final Map methods = new HashMap<>(3); - - private TypeVersion(ApiVersion since, ApiVersion deprecated) { - super(since, deprecated); - } - } - private final Map map; ApiInfoStoreImpl(@Nonnull File apiVersions) { @@ -55,6 +45,12 @@ public ApiInfo method(@Nonnull String type, @Nonnull String signature) { : null; } + @Nonnull + @Override + public Map info() { + return map; + } + private static class Parser { private static final String NAME = "name"; diff --git a/src/main/java/ru/noties/enhance/Enhance.java b/src/main/java/ru/noties/enhance/Enhance.java index ebacc40..b6626d2 100644 --- a/src/main/java/ru/noties/enhance/Enhance.java +++ b/src/main/java/ru/noties/enhance/Enhance.java @@ -8,9 +8,13 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; +import java.util.Map; import static ru.noties.enhance.Log.log; +import static ru.noties.enhance.Stats.printStatsFor; public class Enhance { @@ -64,6 +68,10 @@ public static void main(String[] args) { log("[Enhance] parsing api-versions.xml"); final ApiInfoStore store = ApiInfoStore.create(sdkHelper.apiVersions()); +// if (true) { +// printStatsFor(ApiVersion.latest(), store.info()); +// return; +// } final File sdkSources = sdkHelper.source(); diff --git a/src/main/java/ru/noties/enhance/Stats.java b/src/main/java/ru/noties/enhance/Stats.java new file mode 100644 index 0000000..ad5b2d6 --- /dev/null +++ b/src/main/java/ru/noties/enhance/Stats.java @@ -0,0 +1,106 @@ +package ru.noties.enhance; + +import javax.annotation.Nonnull; +import java.util.*; + +abstract class Stats { + + static void printStatsFor(@Nonnull ApiVersion version, @Nonnull Map info) { + + // filter + final Map filtered = new HashMap<>(); + + for (Map.Entry types : info.entrySet()) { + + final ApiInfoStore.TypeVersion original = types.getValue(); + final ApiInfoStore.TypeVersion typeVersion = new ApiInfoStore.TypeVersion(original.since, original.deprecated); + + for (Map.Entry fields : original.fields.entrySet()) { + if (shouldEmit(version, fields.getValue())) { + typeVersion.fields.put(fields.getKey(), fields.getValue()); + } + } + + for (Map.Entry methods : original.methods.entrySet()) { + if (shouldEmit(version, methods.getValue())) { + typeVersion.methods.put(methods.getKey(), methods.getValue()); + } + } + + if (shouldEmit(version, original) + || (typeVersion.fields.size() > 0 || typeVersion.methods.size() > 0)) { + filtered.put(types.getKey(), typeVersion); + } + } + + final StringBuilder builder = new StringBuilder(); + + for (String type : sorted(filtered.keySet())) { + builder.setLength(0); + builder.append("```diff\n"); + + final ApiInfoStore.TypeVersion typeVersion = filtered.get(type); + appendDiffed(builder, version, typeVersion); + builder + .append(type) + .append('\n'); + + final Map fields = typeVersion.fields; + final Map methods = typeVersion.methods; + + for (String field : sorted(fields.keySet())) { + if (appendDiffed(builder, version, fields.get(field))) { + builder.append(" ") + .append(field) + .append("\n"); + } + } + + for (String method : sorted(methods.keySet())) { + if (appendDiffed(builder, version, methods.get(method))) { + builder.append(" ") + .append(method) + .append("\n"); + } + } + + builder.append("```\n\n"); + System.out.println(builder); + } + } + + private static boolean shouldEmit(@Nonnull ApiVersion version, @Nonnull ApiInfo info) { + return version == info.since || version == info.deprecated; + } + + private static List sorted(@Nonnull Collection collection) { + final List list = new ArrayList<>(collection); + Collections.sort(list); + return list; + } + + private static boolean appendDiffed( + @Nonnull StringBuilder builder, + @Nonnull ApiVersion version, + @Nonnull ApiInfo info) { + + // priority for deprecated (some nodes are both added and deprecated in the same version) + + boolean result = false; + + if (version == info.deprecated) { + builder.append('-'); + result = true; + } + + if (version == info.since) { + builder.append('+'); + result = true; + } + + return result; + } + + private Stats() { + } +}