Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8749453313fe37d84570302ff4dbfeb8ed1f5326 (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
/*******************************************************************************
 * 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 update(Callback rm) {
        fCache.update(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.update(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(Iterable caches) throws InvalidCacheException, ExecutionException {
        for (Object cacheObj : caches) {
            ICache<?> cache = (ICache<?>)cacheObj;
            if (cache.isValid()) {
                addDependsOn(cache);
            }
        }
        super.validate(caches);
    }
}

Back to the top