Skip to content

Commit 21b9940

Browse files
committed
Move description logic from Arguments to Connector
1 parent ac61d3b commit 21b9940

File tree

3 files changed

+62
-48
lines changed

3 files changed

+62
-48
lines changed

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/ConnectorArguments.java

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.google.common.base.Strings;
3131
import com.google.common.collect.ImmutableList;
3232
import com.google.edwmigration.dumper.application.dumper.ZonedParser.DayOffset;
33-
import com.google.edwmigration.dumper.application.dumper.annotations.RespectsInput;
3433
import com.google.edwmigration.dumper.application.dumper.connector.Connector;
3534
import com.google.edwmigration.dumper.application.dumper.connector.ConnectorProperty;
3635
import com.google.edwmigration.dumper.application.dumper.connector.ConnectorPropertyWithDefault;
@@ -42,17 +41,11 @@
4241
import java.time.ZoneOffset;
4342
import java.time.ZonedDateTime;
4443
import java.time.temporal.ChronoUnit;
45-
import java.util.ArrayList;
4644
import java.util.Arrays;
47-
import java.util.Collection;
48-
import java.util.Collections;
4945
import java.util.Date;
50-
import java.util.HashMap;
5146
import java.util.HashSet;
5247
import java.util.List;
53-
import java.util.Map;
5448
import java.util.Optional;
55-
import java.util.Set;
5649
import java.util.function.Predicate;
5750
import java.util.stream.Collectors;
5851
import javax.annotation.CheckForNull;
@@ -62,7 +55,6 @@
6255
import joptsimple.OptionSpec;
6356
import org.apache.commons.lang3.BooleanUtils;
6457
import org.apache.commons.lang3.StringUtils;
65-
import org.springframework.core.annotation.AnnotationUtils;
6658

6759
/** @author shevek */
6860
public class ConnectorArguments extends DefaultArguments {
@@ -604,57 +596,31 @@ public ConnectorArguments(@Nonnull String... args) throws IOException {
604596
super(args);
605597
}
606598

607-
@Nonnull
608-
private static Collection<InputDescriptor> getAcceptsInputs(@Nonnull Connector connector) {
609-
Map<String, InputDescriptor> tmp = new HashMap<>();
610-
Class<?> connectorType = connector.getClass();
611-
while (connectorType != null) {
612-
Set<RespectsInput> respectsInputs =
613-
AnnotationUtils.getDeclaredRepeatableAnnotations(connectorType, RespectsInput.class);
614-
for (RespectsInput respectsInput : respectsInputs) {
615-
InputDescriptor descriptor = new InputDescriptor(respectsInput);
616-
tmp.putIfAbsent(descriptor.getKey(), descriptor);
617-
}
618-
connectorType = connectorType.getSuperclass();
619-
}
620-
621-
List<InputDescriptor> out = new ArrayList<>(tmp.values());
622-
Collections.sort(out);
623-
return out;
624-
}
625-
626599
@Override
627600
protected void printHelpOn(@Nonnull PrintStream out, OptionSet o) throws IOException {
628601
out.append(HELP_INFO);
629602
super.printHelpOn(out, o);
630603

604+
ConnectorRepository repository = ConnectorRepository.getInstance();
631605
// if --connector <valid-connection> provided, print only that
632606
if (o.has(connectorNameOption)) {
633607
String helpOnConnector = o.valueOf(connectorNameOption);
634-
Connector connector = ConnectorRepository.getInstance().getByName(helpOnConnector);
608+
Connector connector = repository.getByName(helpOnConnector);
635609
if (connector != null) {
636610
out.append("\nSelected connector:\n");
637611
printConnectorHelp(out, connector);
638612
return;
639613
}
640614
}
641615
out.append("\nAvailable connectors:\n");
642-
for (Connector connector : ConnectorRepository.getInstance().getAllConnectors()) {
616+
for (Connector connector : repository.getAllConnectors()) {
643617
printConnectorHelp(out, connector);
644618
}
645619
}
646620

647-
private void printConnectorHelp(@Nonnull Appendable out, @Nonnull Connector connector)
621+
private static void printConnectorHelp(@Nonnull Appendable out, @Nonnull Connector connector)
648622
throws IOException {
649-
out.append("* " + connector.getName());
650-
String description = connector.getDescription();
651-
if (!description.isEmpty()) {
652-
out.append(" - " + description);
653-
}
654-
out.append("\n");
655-
for (InputDescriptor descriptor : getAcceptsInputs(connector)) {
656-
out.append(String.format("%8s%s\n", "", descriptor));
657-
}
623+
connector.printHelp(out);
658624
ConnectorProperties.printHelp(out, connector);
659625
}
660626

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/InputDescriptor.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
package com.google.edwmigration.dumper.application.dumper;
1818

1919
import static com.google.common.base.Strings.isNullOrEmpty;
20+
import static java.util.Comparator.comparing;
2021
import static java.util.stream.Collectors.joining;
2122

2223
import com.google.edwmigration.dumper.application.dumper.annotations.RespectsInput;
2324
import java.util.ArrayList;
2425
import java.util.Comparator;
2526
import javax.annotation.Nonnull;
2627

27-
final class InputDescriptor implements Comparable<InputDescriptor> {
28+
public final class InputDescriptor {
2829
private enum Category {
2930
ARGUMENT(1) {
3031
@Override
@@ -56,7 +57,7 @@ String getKey(@Nonnull RespectsInput annotation) {
5657

5758
private final RespectsInput annotation;
5859

59-
InputDescriptor(RespectsInput annotation) {
60+
public InputDescriptor(RespectsInput annotation) {
6061
this.annotation = annotation;
6162
}
6263

@@ -71,11 +72,9 @@ private Category getCategory() {
7172
return Category.OTHER;
7273
}
7374

74-
@Override
75-
public int compareTo(InputDescriptor o) {
76-
return Comparator.comparing(InputDescriptor::order)
77-
.thenComparing(el -> el.annotation.order())
78-
.compare(this, o);
75+
public static Comparator<InputDescriptor> comparator() {
76+
return comparing(InputDescriptor::categoryOrder)
77+
.thenComparing(InputDescriptor::annotationOrder);
7978
}
8079

8180
@Override
@@ -92,7 +91,7 @@ public String toString() {
9291
return buf.stream().filter(el -> !el.isEmpty()).collect(joining(" "));
9392
}
9493

95-
String getKey() {
94+
public String getKey() {
9695
return getCategory().getKey(annotation);
9796
}
9897

@@ -112,7 +111,11 @@ private String requiredHint(String value) {
112111
}
113112
}
114113

115-
private int order() {
114+
private int annotationOrder() {
115+
return annotation.order();
116+
}
117+
118+
private int categoryOrder() {
116119
return getCategory().order;
117120
}
118121
}

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/Connector.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Preconditions.checkState;
21+
import static org.springframework.core.annotation.AnnotationUtils.getDeclaredRepeatableAnnotations;
2122

23+
import autovalue.shaded.com.google.common.collect.ImmutableList;
2224
import com.google.edwmigration.dumper.application.dumper.ConnectorArguments;
25+
import com.google.edwmigration.dumper.application.dumper.InputDescriptor;
26+
import com.google.edwmigration.dumper.application.dumper.annotations.RespectsInput;
2327
import com.google.edwmigration.dumper.application.dumper.handle.Handle;
2428
import com.google.edwmigration.dumper.application.dumper.task.Task;
29+
import java.io.IOException;
2530
import java.time.Clock;
2631
import java.time.ZonedDateTime;
32+
import java.util.ArrayList;
33+
import java.util.Collection;
34+
import java.util.HashMap;
2735
import java.util.List;
36+
import java.util.Map;
37+
import java.util.Set;
2838
import javax.annotation.Nonnull;
2939

3040
/** @author shevek */
@@ -77,4 +87,39 @@ void addTasksTo(@Nonnull List<? super Task<?>> out, @Nonnull ConnectorArguments
7787

7888
@Nonnull
7989
Iterable<ConnectorProperty> getPropertyConstants();
90+
91+
default void printHelp(@Nonnull Appendable out) throws IOException {
92+
out.append("* " + getName());
93+
String description = getDescription();
94+
if (!description.isEmpty()) {
95+
out.append(" - " + description);
96+
}
97+
out.append("\n");
98+
for (InputDescriptor descriptor : getAcceptsInputs(this)) {
99+
out.append(String.format("%8s%s\n", "", descriptor));
100+
}
101+
}
102+
103+
@Nonnull
104+
static Collection<InputDescriptor> getAcceptsInputs(@Nonnull Connector connector) {
105+
106+
ArrayList<Class<?>> classes = new ArrayList<>();
107+
for (Class<?> type = connector.getClass(); type != null; type = type.getSuperclass()) {
108+
classes.add(type);
109+
}
110+
111+
Map<String, InputDescriptor> map = new HashMap<>();
112+
for (Class<?> type : classes) {
113+
Set<RespectsInput> respectsInputs =
114+
getDeclaredRepeatableAnnotations(type, RespectsInput.class);
115+
for (RespectsInput item : respectsInputs) {
116+
InputDescriptor descriptor = new InputDescriptor(item);
117+
map.putIfAbsent(descriptor.getKey(), descriptor);
118+
}
119+
}
120+
121+
return map.values().stream()
122+
.sorted(InputDescriptor.comparator())
123+
.collect(ImmutableList.toImmutableList());
124+
}
80125
}

0 commit comments

Comments
 (0)