Skip to content

Commit

Permalink
Add Stats utility
Browse files Browse the repository at this point in the history
  • Loading branch information
noties committed Dec 2, 2019
1 parent abe7c0f commit b8d832a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 10 deletions.
15 changes: 15 additions & 0 deletions src/main/java/ru/noties/enhance/ApiInfoStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -11,6 +13,16 @@ public static ApiInfoStore create(@Nonnull File apiVersions) {
return new ApiInfoStoreImpl(apiVersions);
}

static class TypeVersion extends ApiInfo {

final Map<String, ApiInfo> fields = new HashMap<>(3);
final Map<String, ApiInfo> methods = new HashMap<>(3);

TypeVersion(ApiVersion since, ApiVersion deprecated) {
super(since, deprecated);
}
}

@Nullable
public abstract ApiInfo type(@Nonnull String type);

Expand All @@ -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<String, TypeVersion> info();
}
16 changes: 6 additions & 10 deletions src/main/java/ru/noties/enhance/ApiInfoStoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@

class ApiInfoStoreImpl extends ApiInfoStore {

private static class TypeVersion extends ApiInfo {

private final Map<String, ApiInfo> fields = new HashMap<>(3);
private final Map<String, ApiInfo> methods = new HashMap<>(3);

private TypeVersion(ApiVersion since, ApiVersion deprecated) {
super(since, deprecated);
}
}

private final Map<String, TypeVersion> map;

ApiInfoStoreImpl(@Nonnull File apiVersions) {
Expand Down Expand Up @@ -55,6 +45,12 @@ public ApiInfo method(@Nonnull String type, @Nonnull String signature) {
: null;
}

@Nonnull
@Override
public Map<String, TypeVersion> info() {
return map;
}

private static class Parser {

private static final String NAME = "name";
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ru/noties/enhance/Enhance.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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();

Expand Down
106 changes: 106 additions & 0 deletions src/main/java/ru/noties/enhance/Stats.java
Original file line number Diff line number Diff line change
@@ -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<String, ApiInfoStore.TypeVersion> info) {

// filter
final Map<String, ApiInfoStore.TypeVersion> filtered = new HashMap<>();

for (Map.Entry<String, ApiInfoStore.TypeVersion> types : info.entrySet()) {

final ApiInfoStore.TypeVersion original = types.getValue();
final ApiInfoStore.TypeVersion typeVersion = new ApiInfoStore.TypeVersion(original.since, original.deprecated);

for (Map.Entry<String, ApiInfo> fields : original.fields.entrySet()) {
if (shouldEmit(version, fields.getValue())) {
typeVersion.fields.put(fields.getKey(), fields.getValue());
}
}

for (Map.Entry<String, ApiInfo> 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<String, ApiInfo> fields = typeVersion.fields;
final Map<String, ApiInfo> 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<String> sorted(@Nonnull Collection<String> collection) {
final List<String> 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() {
}
}

0 comments on commit b8d832a

Please sign in to comment.