aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/manager/SnippetObjectConverterManager.java
blob: df8af530c957041b061367505a7484ef8bc8a6cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.google.android.mobly.snippet.manager;

import com.google.android.mobly.snippet.SnippetObjectConverter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Manager for classes that implement {@link SnippetObjectConverter}.
 *
 * <p>This class is created to separate how Snippet Lib handles object conversion internally from
 * how the conversion scheme for complex types is defined for users.
 *
 * <p>Snippet Lib can pull in the custom serializers and deserializers through here in various
 * stages of execution, whereas users can have a clean interface for supplying these methods without
 * worrying about internal states of Snippet Lib.
 *
 * <p>This gives us the flexibility of changing Snippet Lib internal structure or expanding support
 * without impacting users. E.g. we can support multiple converter classes in the future.
 */
public class SnippetObjectConverterManager {
    private static SnippetObjectConverter mConverter;
    private static volatile SnippetObjectConverterManager mManager;

    private SnippetObjectConverterManager() {}

    public static synchronized SnippetObjectConverterManager getInstance() {
        if (mManager == null) {
            mManager = new SnippetObjectConverterManager();
        }
        return mManager;
    }

    static void addConverter(Class<? extends SnippetObjectConverter> converterClass) {
        if (mConverter != null) {
            throw new RuntimeException("A converter has been added, cannot add again.");
        }
        try {
            mConverter = converterClass.getConstructor().newInstance();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("No default constructor found for the converter class.");
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getCause());
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
    }

    public Object objectToJson(Object object) throws JSONException {
        if (mConverter == null) {
            return null;
        }
        return mConverter.serialize(object);
    }

    public Object jsonToObject(JSONObject jsonObject, Type type) throws JSONException {
        if (mConverter == null) {
            return null;
        }
        return mConverter.deserialize(jsonObject, type);
    }
}