1
0
Fork 1

use sorted map in annotation processor

(cherry picked from commit 37f51949fda2f04cd64eab76a1cc91343695f52e)
This commit is contained in:
Daniel Gultsch 2024-12-09 17:54:29 +01:00 committed by Arne
parent 11e689c561
commit 75e24ce8e2

View file

@ -4,17 +4,17 @@ import com.google.auto.service.AutoService;
import com.google.common.base.CaseFormat; import com.google.common.base.CaseFormat;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import im.conversations.android.annotation.XmlElement; import im.conversations.android.annotation.XmlElement;
import im.conversations.android.annotation.XmlPackage; import im.conversations.android.annotation.XmlPackage;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor; import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
@ -38,7 +38,7 @@ public class XmlElementProcessor extends AbstractProcessor {
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
final Set<? extends Element> elements = final Set<? extends Element> elements =
roundEnvironment.getElementsAnnotatedWith(XmlElement.class); roundEnvironment.getElementsAnnotatedWith(XmlElement.class);
final ImmutableMap.Builder<Id, String> builder = ImmutableMap.builder(); final ImmutableSortedMap.Builder<Id, String> builder = ImmutableSortedMap.naturalOrder();
for (final Element element : elements) { for (final Element element : elements) {
if (element instanceof final TypeElement typeElement) { if (element instanceof final TypeElement typeElement) {
final Id id = of(typeElement); final Id id = of(typeElement);
@ -160,7 +160,7 @@ public class XmlElementProcessor extends AbstractProcessor {
return false; return false;
} }
public static class Id { public static class Id implements Comparable<Id> {
public final String name; public final String name;
public final String namespace; public final String namespace;
@ -181,5 +181,13 @@ public class XmlElementProcessor extends AbstractProcessor {
public int hashCode() { public int hashCode() {
return Objects.hashCode(name, namespace); return Objects.hashCode(name, namespace);
} }
@Override
public int compareTo(@Nonnull Id id) {
return ComparisonChain.start()
.compare(namespace, id.namespace)
.compare(name, id.name)
.result();
}
} }
} }