Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c910d6321544e204251704bb3f414d37cb519440 (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
/*******************************************************************************
 * Copyright (c) 2012 Wind River Systems 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.tcf.debug.test.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;


/**
 * 
 */
public abstract class TransactionCache<V> extends Transaction<V> implements ICache<V> {

    private List<ICache<?>> fDependsOn;
    private List<Callback> fDependsOnCallbacks;
    
    private CallbackCache<V> fCache = new CallbackCache<V>() {
        @Override
        protected void retrieve(DataCallback<V> rm) {
            request(rm);
        }
    };

    public V getData() {
        return fCache.getData();
    }

    public Throwable getError() {
        return fCache.getError();
    }

    public void wait(Callback rm) {
        fCache.wait(rm);
    }

    public boolean isValid() {
        return fCache.isValid();
    }
    
    @Override
    protected void preProcess() {
        super.preProcess();
        
        if (fDependsOnCallbacks != null) {
            for (Callback cb : fDependsOnCallbacks) {
                cb.cancel();
            }
            fDependsOnCallbacks = null;
        }
        fDependsOn = new ArrayList<ICache<?>>(4);
    }
    
    protected void postProcess(boolean done, V data, Throwable error) {
        if (done) {
            fDependsOnCallbacks = new ArrayList<Callback>(fDependsOn.size());
            for (ICache<?> cache : fDependsOn) {
                assert cache.isValid();
                cache.wait(new Callback() {
                    @Override
                    protected void handleCompleted() {
                        if (!isCancelled()) {
                            fCache.reset();
                            for (Callback cb : fDependsOnCallbacks) {
                                if (cb == this) continue;
                                cb.cancel();
                            }
                        }
                    }
                });
            }
        } else {
            fDependsOn = null;
        }
        super.postProcess(done, data, error);
    }
    
    /**
     * Can be called while in {@link #process()}
     * @param cache
     */
    public void addDependsOn(ICache<?> cache) {
        fDependsOn.add(cache);
    }
    
    public <T> T validate(ICache<T> cache) throws InvalidCacheException, ExecutionException {
        if (cache.isValid()) {
            addDependsOn(cache);
        }
        return super.validate(cache);
    }
    
    @Override
    public void validate(@SuppressWarnings("rawtypes") Iterable caches) throws InvalidCacheException, ExecutionException {
        for (Object cacheObj : caches) {
            ICache<?> cache = (ICache<?>)cacheObj;
            if (cache.isValid()) {
                addDependsOn(cache);
            }
        }
        super.validate(caches);
    }
    
    @Override
    public boolean validateUnchecked(ICache<?> cache) {
        if (cache.isValid()) {
            addDependsOn(cache);
        }
        return super.validateUnchecked(cache);
    }
    
    @Override
    public boolean validateUnchecked(@SuppressWarnings("rawtypes") Iterable caches) {
        for (Object cacheObj : caches) {
            ICache<?> cache = (ICache<?>)cacheObj;
            if (cache.isValid()) {
                addDependsOn(cache);
            }
        }
        return super.validateUnchecked(caches);
    }
}

Back to the top