Subversion Repository Public Repository

Nextrek

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
package nextrek.minstrek.core;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class ConditionList {

    private HashMap<String, Boolean> map = new HashMap<String, Boolean>();
    private boolean hasChanged = false;

    public boolean hasChanged() {
        return this.hasChanged;
    }

    private ConditionList() {

    }

    public static ConditionList CreateFromJavascript(String jsonKeyToGrab, String eventJavascript) {

        if (eventJavascript == null) {
            return null;
        }

        ConditionList result = new ConditionList();

        String jsonString = cleanJavascript(eventJavascript);
        JSONTokener jsonTok = new JSONTokener(jsonString);
        try {
            JSONObject json = new JSONObject(jsonTok);
            JSONArray names = json.names();
            for (int scan = 0; scan < names.length(); scan++) {
                String eventName = names.getString(scan);
                JSONObject eventJson = json.getJSONObject(eventName);

                eventName = eventName.toLowerCase();

                result.map.put(eventName, eventJson.optBoolean(jsonKeyToGrab));
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    public static ConditionList CreateFromSharedPrefs(String jsonString) {

        if (jsonString == null) {
            return null;
        }

        ConditionList result = new ConditionList();

        JSONTokener jsonTok = new JSONTokener(jsonString);
        try {
            JSONObject json = new JSONObject(jsonTok);
            JSONArray names = json.names();
            for (int scan = 0; scan < names.length(); scan++) {
                String eventName = names.getString(scan);
                boolean value = json.getBoolean(eventName);

                result.map.put(eventName, value);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    private static String cleanJavascript(String js) {

        int start = js.indexOf("{");
        int end = js.indexOf("};");

        return js.substring(start, end + 1);
    }

    public String saveAsJsonString() throws JSONException {
        JSONObject root = new JSONObject(map);
        return root.toString();
    }

    public Boolean change(String eventName, boolean newValue) {
        Boolean oldValue = this.map.get(eventName);

        if ((oldValue == null) || (newValue != oldValue)) {
            this.map.put(eventName, newValue);
            hasChanged |= true;
        }
        
        return oldValue;
    }

    public boolean isTrue(String eventName) {
        return this.map.get(eventName);
    }

    public void clearChanged() {
        this.hasChanged = false;
    }

}

Commits for Nextrek/Android/Minstrek/MinstrekLib/src/nextrek/minstrek/core/ConditionList.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000