From dbdf48777c392b5635b74a0e946afdc9b2b54a38 Mon Sep 17 00:00:00 2001 From: Eric Cochran Date: Thu, 5 Apr 2018 11:05:40 -0700 Subject: [PATCH] Simplify example code. (#483) * Simplify example code. This was copied code from a different example that used the delegate annotations. * Make brackets consistent. --- .../squareup/moshi/recipes/FallbackEnum.java | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/examples/src/main/java/com/squareup/moshi/recipes/FallbackEnum.java b/examples/src/main/java/com/squareup/moshi/recipes/FallbackEnum.java index c418421..3ff435e 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/FallbackEnum.java +++ b/examples/src/main/java/com/squareup/moshi/recipes/FallbackEnum.java @@ -26,8 +26,6 @@ import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.reflect.Type; -import java.util.Collections; -import java.util.LinkedHashSet; import java.util.Set; import javax.annotation.Nullable; @@ -48,25 +46,18 @@ final class FallbackEnum { @Nullable @Override @SuppressWarnings("unchecked") public JsonAdapter create(Type type, Set annotations, Moshi moshi) { Class rawType = Types.getRawType(type); - if (!rawType.isEnum()) return null; - if (annotations.isEmpty()) { + if (!rawType.isEnum()) { + return null; + } + if (annotations.size() != 1) { + return null; + } + Annotation annotation = annotations.iterator().next(); + if (!(annotation instanceof Fallback)) { return null; } Class enumType = (Class) rawType; - Set delegateAnnotations = null; - Enum fallback = null; - for (Annotation annotation : annotations) { - if (annotation instanceof Fallback) { - delegateAnnotations = new LinkedHashSet<>(annotations); - delegateAnnotations.remove(annotation); - delegateAnnotations = Collections.unmodifiableSet(delegateAnnotations); - fallback = Enum.valueOf(enumType, ((Fallback) annotation).value()); - break; - } - } - if (delegateAnnotations == null) { - return null; - } + Enum fallback = Enum.valueOf(enumType, ((Fallback) annotation).value()); return new FallbackEnumJsonAdapter<>(enumType, fallback); } };