Skip to content

Commit 880ab90

Browse files
committed
refactoring, add UniqueNameSet
1 parent 6b480bf commit 880ab90

4 files changed

Lines changed: 55 additions & 95 deletions

File tree

compiler/src/main/java/net/jbock/annotated/AnnotatedMethodsFactory.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@
22

33
import io.jbock.util.Either;
44
import jakarta.inject.Inject;
5-
import net.jbock.common.SnakeName;
65
import net.jbock.common.ValidationFailure;
76
import net.jbock.processor.SourceElement;
87
import net.jbock.validate.ValidateScope;
98

10-
import javax.lang.model.element.Name;
119
import java.util.Comparator;
12-
import java.util.HashMap;
13-
import java.util.HashSet;
1410
import java.util.List;
15-
import java.util.Locale;
16-
import java.util.Map;
1711
import java.util.Optional;
18-
import java.util.Set;
1912

2013
import static java.util.stream.Collectors.toList;
2114
import static net.jbock.common.Constants.instancesOf;
@@ -39,10 +32,8 @@ public class AnnotatedMethodsFactory {
3932

4033
public Either<List<ValidationFailure>, AnnotatedMethods> createAnnotatedMethods() {
4134
return executableElementsFinder.findExecutableElements()
42-
.map(EnumNamesBuilder::builder)
43-
.map(builder -> builder.withSourceElement(sourceElement))
44-
.map(builder -> builder.withEnumNames(createEnumNames(builder.methods())))
45-
.flatMap(EnumNames::createAnnotatedMethods)
35+
.map(methods -> new SourceElementWithMethods(sourceElement, methods))
36+
.flatMap(SourceElementWithMethods::validListOfAnnotatedMethods)
4637
.map(AnnotatedMethodsBuilder::builder)
4738
.map(builder -> builder.withNamedOptions(builder.annotatedMethods()
4839
.flatMap(instancesOf(AnnotatedOption.class))
@@ -67,20 +58,4 @@ private Optional<List<ValidationFailure>> validateAtLeastOneParameterInSuperComm
6758
" when the superCommand attribute is set";
6859
return Optional.of(List.of(sourceElement.fail(message)));
6960
}
70-
71-
private Map<Name, String> createEnumNames(List<Executable> methods) {
72-
Set<String> enumNames = new HashSet<>(methods.size());
73-
Map<Name, String> result = new HashMap<>(methods.size());
74-
for (Executable method : methods) {
75-
String enumName = "_".contentEquals(method.simpleName()) ?
76-
"_1" : // avoid potential keyword issue
77-
SnakeName.create(method.simpleName()).snake('_').toUpperCase(Locale.US);
78-
while (!enumNames.add(enumName)) {
79-
String suffix = enumName.endsWith("1") ? "1" : "_1";
80-
enumName = enumName + suffix;
81-
}
82-
result.put(method.simpleName(), enumName);
83-
}
84-
return result;
85-
}
8661
}

compiler/src/main/java/net/jbock/annotated/EnumNamesBuilder.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

compiler/src/main/java/net/jbock/annotated/EnumNames.java renamed to compiler/src/main/java/net/jbock/annotated/SourceElementWithMethods.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.jbock.annotated;
22

33
import io.jbock.util.Either;
4+
import net.jbock.common.SnakeName;
45
import net.jbock.common.Util;
56
import net.jbock.common.ValidationFailure;
67
import net.jbock.processor.SourceElement;
@@ -9,7 +10,7 @@
910
import javax.lang.model.element.Name;
1011
import javax.lang.model.type.DeclaredType;
1112
import java.util.List;
12-
import java.util.Map;
13+
import java.util.Locale;
1314
import java.util.Optional;
1415

1516
import static io.jbock.util.Either.right;
@@ -22,18 +23,19 @@
2223
import static net.jbock.common.TypeTool.AS_TYPE_ELEMENT;
2324
import static net.jbock.common.Util.checkNoDuplicateAnnotations;
2425

25-
final class EnumNames {
26+
final class SourceElementWithMethods {
2627

27-
private final EnumNamesBuilder.Step2 step2;
28-
private final Map<Name, String> enumNames;
28+
private final SourceElement sourceElement;
29+
private final List<Executable> methods;
30+
private final UniqueNameSet uniqueNameSet = new UniqueNameSet();
2931

30-
EnumNames(EnumNamesBuilder.Step2 step2, Map<Name, String> enumNames) {
31-
this.step2 = step2;
32-
this.enumNames = enumNames;
32+
SourceElementWithMethods(SourceElement sourceElement, List<Executable> methods) {
33+
this.methods = methods;
34+
this.sourceElement = sourceElement;
3335
}
3436

35-
Either<List<ValidationFailure>, List<AnnotatedMethod>> createAnnotatedMethods() {
36-
return methods().stream()
37+
Either<List<ValidationFailure>, List<AnnotatedMethod>> validListOfAnnotatedMethods() {
38+
return methods.stream()
3739
.map(this::createAnnotatedMethod)
3840
.collect(allFailures());
3941
}
@@ -44,7 +46,7 @@ private Either<ValidationFailure, AnnotatedMethod> createAnnotatedMethod(
4446
ExecutableElement method = sourceMethod.method();
4547
return checkNoDuplicateAnnotations(method, methodLevelAnnotations())
4648
.<Either<ValidationFailure, AnnotatedMethod>>map(Either::left)
47-
.orElseGet(() -> right(sourceMethod.annotatedMethod(sourceElement(), enumName)))
49+
.orElseGet(() -> right(sourceMethod.annotatedMethod(sourceElement, enumName)))
4850
.filter(this::checkAccessibleReturnType);
4951
}
5052

@@ -72,15 +74,10 @@ private boolean isInaccessible(DeclaredType declared) {
7274
.anyMatch(this::isInaccessible);
7375
}
7476

75-
private SourceElement sourceElement() {
76-
return step2.sourceElement;
77-
}
78-
7977
private String enumNameFor(Name sourceMethodName) {
80-
return enumNames.get(sourceMethodName);
81-
}
82-
83-
private List<Executable> methods() {
84-
return step2.methods();
78+
String enumName = "_".contentEquals(sourceMethodName) ?
79+
"_1" : // avoid potential keyword issue
80+
SnakeName.create(sourceMethodName).snake('_').toUpperCase(Locale.ROOT);
81+
return uniqueNameSet.getUniqueName(enumName);
8582
}
8683
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2016 The Dagger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.jbock.annotated;
18+
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
/** A collector for names to be used in the same namespace that should not conflict. */
23+
final class UniqueNameSet {
24+
private final Set<String> uniqueNames = new HashSet<>();
25+
26+
/**
27+
* Generates a unique name using {@code base}. If {@code base} has not yet been added, it will be
28+
* returned as-is. If your {@code base} is healthy, this will always return {@code base}.
29+
*/
30+
String getUniqueName(CharSequence base) {
31+
String name = base.toString();
32+
for (int differentiator = 2; !uniqueNames.add(name); differentiator++) {
33+
name = base.toString() + differentiator;
34+
}
35+
return name;
36+
}
37+
}

0 commit comments

Comments
 (0)