Skip to content

Commit b8d832a

Browse files
committed
Add Stats utility
1 parent abe7c0f commit b8d832a

File tree

4 files changed

+135
-10
lines changed

4 files changed

+135
-10
lines changed

src/main/java/ru/noties/enhance/ApiInfoStore.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import javax.annotation.Nonnull;
44
import javax.annotation.Nullable;
55
import java.io.File;
6+
import java.util.HashMap;
7+
import java.util.Map;
68

79
public abstract class ApiInfoStore {
810

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

16+
static class TypeVersion extends ApiInfo {
17+
18+
final Map<String, ApiInfo> fields = new HashMap<>(3);
19+
final Map<String, ApiInfo> methods = new HashMap<>(3);
20+
21+
TypeVersion(ApiVersion since, ApiVersion deprecated) {
22+
super(since, deprecated);
23+
}
24+
}
25+
1426
@Nullable
1527
public abstract ApiInfo type(@Nonnull String type);
1628

@@ -19,4 +31,7 @@ public static ApiInfoStore create(@Nonnull File apiVersions) {
1931

2032
@Nullable
2133
public abstract ApiInfo method(@Nonnull String type, @Nonnull String signature);
34+
35+
@Nonnull
36+
public abstract Map<String, TypeVersion> info();
2237
}

src/main/java/ru/noties/enhance/ApiInfoStoreImpl.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@
1515

1616
class ApiInfoStoreImpl extends ApiInfoStore {
1717

18-
private static class TypeVersion extends ApiInfo {
19-
20-
private final Map<String, ApiInfo> fields = new HashMap<>(3);
21-
private final Map<String, ApiInfo> methods = new HashMap<>(3);
22-
23-
private TypeVersion(ApiVersion since, ApiVersion deprecated) {
24-
super(since, deprecated);
25-
}
26-
}
27-
2818
private final Map<String, TypeVersion> map;
2919

3020
ApiInfoStoreImpl(@Nonnull File apiVersions) {
@@ -55,6 +45,12 @@ public ApiInfo method(@Nonnull String type, @Nonnull String signature) {
5545
: null;
5646
}
5747

48+
@Nonnull
49+
@Override
50+
public Map<String, TypeVersion> info() {
51+
return map;
52+
}
53+
5854
private static class Parser {
5955

6056
private static final String NAME = "name";

src/main/java/ru/noties/enhance/Enhance.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
import java.io.File;
99
import java.io.IOException;
1010
import java.io.InputStreamReader;
11+
import java.util.ArrayList;
12+
import java.util.List;
1113
import java.util.Locale;
14+
import java.util.Map;
1215

1316
import static ru.noties.enhance.Log.log;
17+
import static ru.noties.enhance.Stats.printStatsFor;
1418

1519
public class Enhance {
1620

@@ -64,6 +68,10 @@ public static void main(String[] args) {
6468
log("[Enhance] parsing api-versions.xml");
6569

6670
final ApiInfoStore store = ApiInfoStore.create(sdkHelper.apiVersions());
71+
// if (true) {
72+
// printStatsFor(ApiVersion.latest(), store.info());
73+
// return;
74+
// }
6775

6876
final File sdkSources = sdkHelper.source();
6977

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package ru.noties.enhance;
2+
3+
import javax.annotation.Nonnull;
4+
import java.util.*;
5+
6+
abstract class Stats {
7+
8+
static void printStatsFor(@Nonnull ApiVersion version, @Nonnull Map<String, ApiInfoStore.TypeVersion> info) {
9+
10+
// filter
11+
final Map<String, ApiInfoStore.TypeVersion> filtered = new HashMap<>();
12+
13+
for (Map.Entry<String, ApiInfoStore.TypeVersion> types : info.entrySet()) {
14+
15+
final ApiInfoStore.TypeVersion original = types.getValue();
16+
final ApiInfoStore.TypeVersion typeVersion = new ApiInfoStore.TypeVersion(original.since, original.deprecated);
17+
18+
for (Map.Entry<String, ApiInfo> fields : original.fields.entrySet()) {
19+
if (shouldEmit(version, fields.getValue())) {
20+
typeVersion.fields.put(fields.getKey(), fields.getValue());
21+
}
22+
}
23+
24+
for (Map.Entry<String, ApiInfo> methods : original.methods.entrySet()) {
25+
if (shouldEmit(version, methods.getValue())) {
26+
typeVersion.methods.put(methods.getKey(), methods.getValue());
27+
}
28+
}
29+
30+
if (shouldEmit(version, original)
31+
|| (typeVersion.fields.size() > 0 || typeVersion.methods.size() > 0)) {
32+
filtered.put(types.getKey(), typeVersion);
33+
}
34+
}
35+
36+
final StringBuilder builder = new StringBuilder();
37+
38+
for (String type : sorted(filtered.keySet())) {
39+
builder.setLength(0);
40+
builder.append("```diff\n");
41+
42+
final ApiInfoStore.TypeVersion typeVersion = filtered.get(type);
43+
appendDiffed(builder, version, typeVersion);
44+
builder
45+
.append(type)
46+
.append('\n');
47+
48+
final Map<String, ApiInfo> fields = typeVersion.fields;
49+
final Map<String, ApiInfo> methods = typeVersion.methods;
50+
51+
for (String field : sorted(fields.keySet())) {
52+
if (appendDiffed(builder, version, fields.get(field))) {
53+
builder.append(" ")
54+
.append(field)
55+
.append("\n");
56+
}
57+
}
58+
59+
for (String method : sorted(methods.keySet())) {
60+
if (appendDiffed(builder, version, methods.get(method))) {
61+
builder.append(" ")
62+
.append(method)
63+
.append("\n");
64+
}
65+
}
66+
67+
builder.append("```\n\n");
68+
System.out.println(builder);
69+
}
70+
}
71+
72+
private static boolean shouldEmit(@Nonnull ApiVersion version, @Nonnull ApiInfo info) {
73+
return version == info.since || version == info.deprecated;
74+
}
75+
76+
private static List<String> sorted(@Nonnull Collection<String> collection) {
77+
final List<String> list = new ArrayList<>(collection);
78+
Collections.sort(list);
79+
return list;
80+
}
81+
82+
private static boolean appendDiffed(
83+
@Nonnull StringBuilder builder,
84+
@Nonnull ApiVersion version,
85+
@Nonnull ApiInfo info) {
86+
87+
// priority for deprecated (some nodes are both added and deprecated in the same version)
88+
89+
boolean result = false;
90+
91+
if (version == info.deprecated) {
92+
builder.append('-');
93+
result = true;
94+
}
95+
96+
if (version == info.since) {
97+
builder.append('+');
98+
result = true;
99+
}
100+
101+
return result;
102+
}
103+
104+
private Stats() {
105+
}
106+
}

0 commit comments

Comments
 (0)