From ee01275c7609ae4b889e2b6b971d3ba8977d0cea Mon Sep 17 00:00:00 2001 From: Serj Lotutovici Date: Wed, 21 Oct 2015 12:01:43 +0200 Subject: [PATCH] Create new builder from Moshi instance --- .../main/java/com/squareup/moshi/Moshi.java | 36 ++++++++++++++----- .../java/com/squareup/moshi/MoshiTest.java | 10 ++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/moshi/src/main/java/com/squareup/moshi/Moshi.java b/moshi/src/main/java/com/squareup/moshi/Moshi.java index 4aa279f..abbd8ff 100644 --- a/moshi/src/main/java/com/squareup/moshi/Moshi.java +++ b/moshi/src/main/java/com/squareup/moshi/Moshi.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -30,6 +31,16 @@ import java.util.Set; * Coordinates binding between JSON values and Java objects. */ public final class Moshi { + static final List BUILT_IN_FACTORIES = new ArrayList<>(); + + static { + BUILT_IN_FACTORIES.add(StandardJsonAdapters.FACTORY); + BUILT_IN_FACTORIES.add(CollectionJsonAdapter.FACTORY); + BUILT_IN_FACTORIES.add(MapJsonAdapter.FACTORY); + BUILT_IN_FACTORIES.add(ArrayJsonAdapter.FACTORY); + BUILT_IN_FACTORIES.add(ClassJsonAdapter.FACTORY); + } + private final List factories; private final ThreadLocal>> reentrantCalls = new ThreadLocal<>(); private final Map> adapterCache = new LinkedHashMap<>(); @@ -37,11 +48,7 @@ public final class Moshi { private Moshi(Builder builder) { List factories = new ArrayList<>(); factories.addAll(builder.factories); - factories.add(StandardJsonAdapters.FACTORY); - factories.add(CollectionJsonAdapter.FACTORY); - factories.add(MapJsonAdapter.FACTORY); - factories.add(ArrayJsonAdapter.FACTORY); - factories.add(ClassJsonAdapter.FACTORY); + factories.addAll(BUILT_IN_FACTORIES); this.factories = Collections.unmodifiableList(factories); } @@ -116,6 +123,14 @@ public final class Moshi { + type + " annotated " + annotations); } + /** Returns a new builder containing all custom factories used by the current instance. */ + public Moshi.Builder newBuilder() { + int fullSize = factories.size(); + int tailSize = BUILT_IN_FACTORIES.size(); + List customFactories = factories.subList(0, fullSize - tailSize); + return new Builder().addAll(customFactories); + } + /** Returns an opaque object that's equal if the type and annotations are equal. */ private Object cacheKey(Type type, Set annotations) { if (annotations.isEmpty()) return type; @@ -123,7 +138,7 @@ public final class Moshi { } public static final class Builder { - private final List factories = new ArrayList<>(); + final List factories = new ArrayList<>(); public Builder add(final Type type, final JsonAdapter jsonAdapter) { if (type == null) throw new IllegalArgumentException("type == null"); @@ -162,8 +177,8 @@ public final class Moshi { }); } - public Builder add(JsonAdapter.Factory jsonAdapter) { - factories.add(jsonAdapter); + public Builder add(JsonAdapter.Factory factory) { + factories.add(factory); return this; } @@ -171,6 +186,11 @@ public final class Moshi { return add(AdapterMethodsFactory.get(adapter)); } + private Builder addAll(List factories) { + this.factories.addAll(factories); + return this; + } + public Moshi build() { return new Moshi(this); } diff --git a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java index 391d883..92d21d5 100644 --- a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java +++ b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java @@ -799,6 +799,16 @@ public final class MoshiTest { assertThat(adapter1).isSameAs(adapter2); } + @Test public void newBuilder() throws Exception { + Moshi moshi = new Moshi.Builder() + .add(Pizza.class, new PizzaAdapter()) + .build(); + Moshi.Builder newBuilder = moshi.newBuilder(); + for (JsonAdapter.Factory factory : Moshi.BUILT_IN_FACTORIES) { + assertThat(factory).isNotIn(newBuilder.factories); + } + } + static class Pizza { final int diameter; final boolean extraCheese;