package org.matrix.androidsdk.util;


import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;

import okhttp3.RequestBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

public final class PolymorphicRequestBodyConverter<T> implements Converter<T, RequestBody> {
    public static final Factory FACTORY = new Factory() {
        @Override public Converter<?, RequestBody> requestBodyConverter(
            Type type,
            Annotation[] parameterAnnotations,
            Annotation[] methodAnnotations,
            Retrofit retrofit
        ) {
            return new PolymorphicRequestBodyConverter<>(
                this, parameterAnnotations, methodAnnotations, retrofit
            );
        }
    };

    private final Factory skipPast;
    private final Annotation[] parameterAnnotations;
    private final Annotation[] methodsAnnotations;
    private final Retrofit retrofit;
    private final Map<Class<?>, Converter<T, RequestBody>> cache = new LinkedHashMap<>();

    PolymorphicRequestBodyConverter(
        Factory skipPast,
        Annotation[] parameterAnnotations,
        Annotation[] methodsAnnotations,
        Retrofit retrofit) {
        this.skipPast = skipPast;
        this.parameterAnnotations = parameterAnnotations;
        this.methodsAnnotations = methodsAnnotations;
        this.retrofit = retrofit;
    }

    @Override public RequestBody convert(T value) throws IOException {
        Class<?> cls = value.getClass();
        Converter<T, RequestBody> requestBodyConverter;
        synchronized (cache) {
            requestBodyConverter = cache.get(cls);
        }
        if (requestBodyConverter == null) {
            requestBodyConverter = retrofit.nextRequestBodyConverter(
                skipPast, cls, parameterAnnotations, methodsAnnotations
            );
            synchronized (cache) {
                cache.put(cls, requestBodyConverter);
            }
        }
        return requestBodyConverter.convert(value);
    }
}

@@ -1,8 +1,607 @@
+/*%0A * Copyright 2018 New Vector Ltd%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 
@@ -629,17 +629,16 @@
 .util;%0A%0A
-%0A
 import j
@@ -1027,32 +1027,40 @@
        @Override
+%0A       
  public Converte
@@ -1111,16 +1111,20 @@
         
+    
 Type typ
@@ -1130,32 +1130,36 @@
 pe,%0A            
+  
+  
 Annotation%5B%5D par
@@ -1181,32 +1181,36 @@
 ns,%0A            
+    
 Annotation%5B%5D met
@@ -1217,32 +1217,36 @@
 hodAnnotations,%0A
+    
             Retr
@@ -1329,16 +1329,20 @@
 rter%3C%3E(%0A
+    
         
@@ -1751,16 +1751,20 @@
         
+    
 Factory 
@@ -1773,16 +1773,20 @@
 ipPast,%0A
+    
         
@@ -1820,32 +1820,36 @@
 ations,%0A        
+    
 Annotation%5B%5D met
@@ -1857,32 +1857,36 @@
 odsAnnotations,%0A
+    
         Retrofit
@@ -2098,16 +2098,20 @@
 Override
+%0A   
  public 
@@ -2455,32 +2455,36 @@
 tBodyConverter(%0A
+    
                 

Add copyright and format