Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eec53c6c02d812c76c9d9b67bb0601fbba769e1c (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
/*******************************************************************************
 * Copyright (c) 2007, 2011 Intel Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.settings.model;

import java.util.Map;

import org.eclipse.cdt.core.settings.model.extension.CDataObject;


public class ProxyProvider implements IProxyProvider {
	private IProxyCache fCache;
	private ICDataScope fScope;
	private IProxyFactory fFactory;
	private boolean fProxiesCached;

	public ProxyProvider(ICDataScope scope, IProxyCache cache, IProxyFactory factory){
		fScope = scope;
		fCache = cache;
		fFactory = factory;
	}

	@Override
	public CDataProxy[] getProxies() {
		if(!fProxiesCached || !fScope.isStatic()){
			fillCache();
			fProxiesCached = true;
		}
		return fCache.getCachedProxies();
	}
/*
	private void clearInvalidCachedProxies(){
		CDataProxy proxies[] = fCache.getCachedProxies();
		for(int i = 0; i < proxies.length; i++){
			if(!proxies[i].isValid()){
				fCache.removeCachedProxy(proxies[i]);
			}
		}
	}
*/
	@Override
	public CDataProxy getProxy(String id) {
		if(!fProxiesCached || !fScope.isStatic()){
			fillCache();
			fProxiesCached = true;
		}
		return fCache.getCachedProxy(id);
	}

	protected void fillCache(){
		Map<String, CDataProxy> map = fCache.getCachedProxiesMap();

		CDataObject datas[] =fScope.getChildren();
		for (CDataObject data : datas) {
			CDataProxy proxy = fCache.getCachedProxy(data.getId());
			if(proxy == null || proxy.getType() != data.getType()){
				proxy = fFactory.createProxy(data);
				if(proxy != null){
					fCache.addCachedProxy(proxy);
				}
			} else {
				proxy.setData(data);
				map.remove(data.getId());
			}
		}

		if(!map.isEmpty()){
			for (CDataProxy proxy : map.values()) {
				fCache.removeCachedProxy(proxy);
			}
		}
	}

	@Override
	public CDataProxy getProxy(CDataObject data) {
		if(!fProxiesCached || !fScope.isStatic()){
			fillCache();
			fProxiesCached = true;
		}
		return fCache.getCachedProxy(data);
	}

	@Override
	public void removeCachedProxy(String id) {
		fCache.removeCachedProxy(id);
		fProxiesCached = true;
	}

	@Override
	public void removeCachedProxy(CDataProxy proxy) {
		fCache.removeCachedProxy(proxy);
		fProxiesCached = true;
	}

	@Override
	public CDataProxy[] getCachedProxies() {
		return fCache.getCachedProxies();
	}

	@Override
	public CDataProxy[] getProxiesOfKind(int kind) {
		CDataProxy[] proxies = getProxies();
		if(proxies.length > 0){
			CDataProxy[] tmp = new CDataProxy[proxies.length];
			int num = 0;
			for (CDataProxy proxy : proxies) {
				if((proxy.getType() & kind) == proxy.getType())
					tmp[num++] = proxy;
			}

			if(num != proxies.length){
				proxies = new CDataProxy[num];
				System.arraycopy(tmp, 0, proxies, 0, num);
			}
		}
		return proxies;
	}

	@Override
	public void cacheValues() {
		if(!fProxiesCached || !fScope.isStatic()){
			fillCache();
			fProxiesCached = true;
		}
	}

	@Override
	public void invalidateCache() {
		fProxiesCached = false;
		CDataProxy[] proxies = fCache.getCachedProxies();
		for (CDataProxy proxy : proxies) {
			proxy.doClearData();
		}
	}


}

Back to the top