summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/same/VariableFactory.java
blob: 8c786c0fd9dab7656491cab42f959bda6fd3208e (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.orbekk.same;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.orbekk.same.State.Component;

/**
 * TODO: Use WeakReference in order to make variables GC-able.
 */
public class VariableFactory {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private ClientInterface client;
    private ObjectMapper mapper = new ObjectMapper();
    
    private class VariableImpl<T> implements Variable<T>, StateChangedListener {
        String identifier;
        TypeReference<T> type;
        T value;
        long revision = 0;
        OnChangeListener<T> listener = null;
    
        public VariableImpl(String identifier, TypeReference<T> type) {
            this.identifier = identifier;
            this.type = type;
        }

        @Override
        public T get() {
            return value;
        }

        @Override
        public void set(T value) throws UpdateConflict {
            try {
                String serializedValue = mapper.writeValueAsString(value);
                client.set(identifier, serializedValue, revision);
            } catch (JsonGenerationException e) {
                logger.warn("Failed to convert to JSON: {}", value);
                logger.warn("Parse exception.", e);
                throw new RuntimeException(e);
            } catch (JsonMappingException e) {
                logger.warn("Failed to convert to JSON: {}", value);
                logger.warn("Parse exception.", e);
                throw new RuntimeException(e);
            } catch (IOException e) {
                logger.warn("Failed to cornvert to JSON: {}", value);
                logger.warn("Parse exception.", e);
                throw new RuntimeException(e);
            }
        }

        @Override
        public void update() {
            value = client.getState().getParsedData(identifier, type);
            revision = client.getState().getRevision(identifier);
        }

        @Override
        public void setOnChangeListener(OnChangeListener<T> listener) {
            this.listener = listener;
        }

        @Override
        public void stateChanged(Component component) {
            if (component.getName().equals(identifier)) {
                if (listener != null) {
                    listener.valueChanged(this);
                }
            }
        }
    }
    
    public static VariableFactory create(ClientInterface client) {
        return new VariableFactory(client);
    }
    
    VariableFactory(ClientInterface client) {
        this.client = client;
    }
    
    public <T> Variable<T> create(String identifier, TypeReference<T> type) {
        VariableImpl<T> variable = new VariableImpl<T>(identifier, type);
        variable.update();
        client.addStateListener(variable);
        return variable;
    }
    
    public Variable<String> createString(String identifier) {
        return create(identifier, new TypeReference<String>() {});
    }
}