summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/same/VariableFactory.java
blob: 81b76155cf36f69b1e888eccee4e22993aa5870f (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
package com.orbekk.same;

import org.codehaus.jackson.type.TypeReference;

/**
 * TODO: Use WeakReference in order to make variables GC-able.
 */
public class VariableFactory {
    Client.ClientInterface client;
    
    private class VariableImpl<T> implements Variable<T> {
        String identifier;
        TypeReference<T> type;
        T value;
    
        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) {
        }

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

        @Override
        public void setOnChangeListener(Variable.OnChangeListener<T> listener) {
            throw new RuntimeException("Not implemented.");
        }
    }
    
    public static VariableFactory create(Client.ClientInterface client) {
        return new VariableFactory(client);
    }
    
    VariableFactory(Client.ClientInterface client) {
        this.client = client;
    }
    
    public <T> Variable<T> create(String identifier, TypeReference<T> type) {
        Variable<T> variable = new VariableImpl<T>(identifier, type);
        variable.update();
        return variable;
    }
}