Simplify example code. (#483)

* Simplify example code.

This was copied code from a different example that used the delegate annotations.

* Make brackets consistent.
This commit is contained in:
Eric Cochran
2018-04-05 11:05:40 -07:00
committed by Jesse Wilson
parent 7cab83a8f2
commit dbdf48777c

View File

@@ -26,8 +26,6 @@ import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@@ -48,25 +46,18 @@ final class FallbackEnum {
@Nullable @Override @SuppressWarnings("unchecked") @Nullable @Override @SuppressWarnings("unchecked")
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) { public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
Class<?> rawType = Types.getRawType(type); Class<?> rawType = Types.getRawType(type);
if (!rawType.isEnum()) return null; if (!rawType.isEnum()) {
if (annotations.isEmpty()) { return null;
}
if (annotations.size() != 1) {
return null;
}
Annotation annotation = annotations.iterator().next();
if (!(annotation instanceof Fallback)) {
return null; return null;
} }
Class<Enum> enumType = (Class<Enum>) rawType; Class<Enum> enumType = (Class<Enum>) rawType;
Set<? extends Annotation> delegateAnnotations = null; Enum<?> fallback = Enum.valueOf(enumType, ((Fallback) annotation).value());
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;
}
return new FallbackEnumJsonAdapter<>(enumType, fallback); return new FallbackEnumJsonAdapter<>(enumType, fallback);
} }
}; };