Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 15246d90ec40e512048c4c8d333ae2cf6be36f52 (plain) (blame)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
 * Copyright (c) 2008 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.tcf.util;

import java.util.HashSet;

import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.protocol.Protocol;

/**
 * Objects of this class are used cache TCF remote data.
 * The cache is asynchronous state machine. The states are:
 *  1. Valid - cache is in sync with remote data, use getError() and getData() to get cached data;
 *  2. Invalid - cache is out out of sync, start data retrieval by calling validate();
 *  3. Pending - cache is waiting result of a command that was sent to remote peer.
 * @param <V> - type of data to be stored in the cache.
 */
public abstract class TCFDataCache<V> implements Runnable {

    private Throwable error;
    private boolean valid;
    private V data;
    
    protected final IChannel channel;
    protected IToken command;

    private final HashSet<Runnable> waiting_list = new HashSet<Runnable>();
    
    public TCFDataCache(IChannel channel) {
        assert channel != null;
        this.channel = channel;
    }
    
    /**
     * @return true if cache contains up-to-date data (or data retrieval error).
     */
    public boolean isValid() {
        return valid;
    }
    
    /**
     * @return true if data retrieval is in progress.
     */
    public boolean isPending() {
        return command != null;
    }
    
    /**
     * @return error object if data retrieval ended with an error, or null if retrieval was successful.
     * Note: It is prohibited to call this method when cache is not valid. 
     */
    public Throwable getError() {
        assert valid;
        return error;
    }
    
    /**
     * @return cached data object.
     * Note: It is prohibited to call this method when cache is not valid. 
     */
    public V getData() {
        assert valid;
        return data;
    }
    
    /**
     * Notify waiting clients about cache state change and remove them from wait list.
     * It is responsibility of clients to check if the state change was one they are waiting for.
     */
    public void run() {
        if (waiting_list.isEmpty()) return;
        Runnable[] arr = waiting_list.toArray(new Runnable[waiting_list.size()]);
        waiting_list.clear();
        for (Runnable r : arr) r.run();
    }
    
    /**
     * Add a client call-back to cache wait list.
     * @param req
     */
    public void wait(Runnable cb) {
        assert !valid;
        if (cb != null) waiting_list.add(cb);
    }
    
    /**
     * Initiate data retrieval if the cache is not valid.
     * @return true if the cache is already valid
     */
    public boolean validate() {
        assert Protocol.isDispatchThread();
        if (channel.getState() != IChannel.STATE_OPEN) {
            error = null;
            command = null;
            valid = true;
            data = null;
        }
        else {
            if (command != null) return false;
            if (!valid && !startDataRetrieval()) return false;
        }
        assert valid;
        assert command == null;
        run();
        return true;
    }
    
    /**
     * End cache pending state.
     * @param token - pending command handle.
     * @param error - data retrieval error or null
     * @param data - up-to-date data object
     */
    public void set(IToken token, Throwable error, V data) {
        assert Protocol.isDispatchThread();
        if (command != token) return;
        command = null;
        if (channel.getState() != IChannel.STATE_OPEN) data = null;
        this.error = error;
        this.data = data;
        valid = true;
        run();
    }

    /**
     * Force cache to become valid, cancel pending data retrieval if any.
     * @param data - up-to-date data object
     */
    public void reset(V data) {
        if (command != null) {
            command.cancel();
            command = null;
        }
        this.data = data;
        error = null;
        valid = true;
        run();
    }
    
    /**
     * Invalidate the cache. If retrieval is in progress - let it continue.
     */
    public void reset() {
        error = null;
        valid = false;
        data = null;
        run();
    }
    
    /**
     * Force cache to invalid state, cancel pending data retrieval if any.
     */
    public void cancel() {
        if (command != null) {
            command.cancel();
            command = null;
        }
        error = null;
        valid = false;
        data = null;
        run();
    }
    
    /**
     * Sub-classes should override this method to implement actual data retrieval logic.
     * @return true is all done, false if retrieval is in progress.
     */
    protected abstract boolean startDataRetrieval();
}

Back to the top