summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/same/State.java
blob: 6b7c82447d2b38e625cc796825e1586f17d3a4e1 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.orbekk.same;

import java.util.Map;
import java.util.HashMap;

public class State {
    private Map<String, Component> state = new HashMap<String, Component>(); 
    
    private long stateIteration = 0;
    private Map<String, String> participants = new HashMap<String, String>();
    private String networkName = "";
    private String masterId = "";
    private String data = "";

    public boolean update(String componentName, String data, long revision) {
        Component component = null;
        if (!state.containsKey(componentName)) {
            component = new Component(0, "");
        } else {
            component = state.get(componentName);           
        }
        
        if (revision == component.getRevision()) {
            component.setRevision(revision + 1);
            component.setData(data);
            state.put(componentName, component);
            return true;
        } else {
            return false;
        }
    }
      
    public String getDataOf(String componentName) {
        Component component = state.get(componentName);
        if (component != null) {
            return component.getData();
        } else {
            return null;
        }
    }
    
    public static class Component {
        private long revision;
        private String data;
        
        public Component(long revision, String data) {
            this.revision = revision;
            this.data = data;
        }
        
        public long getRevision() {
            return revision;
        }
        public void setRevision(long revision) {
            this.revision = revision;
        }
        
        public String getData() {
            return data;
        }
        public void setData(String data) {
            this.data = data;
        }       
        
        @Override public String toString() {
            return this.data + " @" + revision;
        }
    }
    
    public long getStateIteration() {
        return stateIteration;
    }

    public void setStateIteration(long stateIteration) {
        this.stateIteration = stateIteration;
    }

    public Map<String, String> getParticipants() {
        return participants;
    }

    public String getNetworkName() {
        return networkName;
    }

    public void setNetworkName(String networkName) {
        this.networkName = networkName;
    }

    public String getMasterId() {
        return masterId;
    }

    public void setMasterId(String masterId) {
        this.masterId = masterId;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    @Override
    public String toString() {
        StringBuilder participantsString = new StringBuilder();
        participantsString.append("[");
        boolean first = true;
        for (Map.Entry<String, String> e : participants.entrySet()) {
            if (!first) {
                participantsString.append(", ");
            }
            first = false;
            participantsString.append(e.getKey())
                    .append("(")
                    .append(e.getValue())
                    .append(")");
            String clientId = e.getKey();
            String url = e.getValue();
        }
        participantsString.append("]");

        return String.format(
            "State( \n" +
            "      stateIteration = %d,\n" +
            "      networkName    = %s,\n" +
            "      masterId       = %s,\n" +
            "      data           = %s,\n" +
            "      participants   = %s\n" +
            ")", stateIteration, networkName, masterId, data,
            participantsString);
    }
}