Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2e78842ed6ad87b9db6faad15aa1d44c39569389 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*******************************************************************************
 * 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.managedbuilder.internal.tcmodification;

import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;

public class ToolListMap implements Cloneable {
	private HashMap<ITool, List<ITool>> fMap;
	private CollectionEntrySet fCollectionEntrySet;

	public ToolListMap() {
		fMap = new HashMap<ITool, List<ITool>>();
	}

	//	public class ValueIter {
	//		private Map fIterMap;
	//
	//		public ValueIter() {
	//			fIterMap = new HashMap(fMap);
	//			for(Iterator iter = fIterMap.entrySet().iterator(); iter.hasNext();){
	//				Map.Entry entry = (Map.Entry)iter.next();
	//				Collection c = (Collection)entry.getValue();
	//				entry.setValue(c.iterator());
	//			}
	//		}
	//
	//		public Iterator get(Object key){
	//			Iterator iter = (Iterator)fIterMap.get(key);
	//			if(iter != null && !iter.hasNext()){
	//				fIterMap.remove(key);
	//				return null;
	//			}
	//			return iter;
	//		}
	//	}

	public class CollectionEntry {
		private Map.Entry<ITool, List<ITool>> fEntry;

		CollectionEntry(Map.Entry<ITool, List<ITool>> entry) {
			fEntry = entry;
		}

		public ITool getKey() {
			return fEntry.getKey();
		}

		public List<ITool> getValue() {
			return fEntry.getValue();
		}

		@Override
		public boolean equals(Object obj) {
			if (obj == this)
				return true;

			if (obj == null)
				return false;

			if (!(obj instanceof CollectionEntry))
				return false;

			return fEntry.equals(((CollectionEntry) obj).fEntry);
		}

		@Override
		public int hashCode() {
			return fEntry.hashCode();
		}
	}

	private class CollectionEntrySet extends AbstractSet<CollectionEntry> {
		private Set<Entry<ITool, List<ITool>>> fMapEntrySet;

		private class Iter implements Iterator<CollectionEntry> {
			private Iterator<Entry<ITool, List<ITool>>> fIter;

			private Iter() {
				fIter = fMapEntrySet.iterator();
			}

			@Override
			public boolean hasNext() {
				return fIter.hasNext();
			}

			@Override
			public CollectionEntry next() {
				return new CollectionEntry(fIter.next());
			}

			@Override
			public void remove() {
				fIter.remove();
			}

		}

		private CollectionEntrySet() {
			fMapEntrySet = fMap.entrySet();
		}

		@Override
		public Iterator<CollectionEntry> iterator() {
			return new Iter();
		}

		@Override
		public int size() {
			return fMapEntrySet.size();
		}
	}

	public void add(ITool key, ITool value) {
		List<ITool> l = get(key, true);
		l.add(value);
	}

	public List<ITool> removeAll(ITool key) {
		return fMap.remove(key);
	}

	public List<ITool> get(ITool key, boolean create) {
		List<ITool> l = fMap.get(key);
		if (l == null && create) {
			l = newList(1);
			fMap.put(key, l);
		}

		return l;
	}

	public List<ITool> valuesToCollection(List<ITool> c) {
		if (c == null)
			c = newList(20);

		for (List<ITool> l : fMap.values()) {
			c.addAll(l);
		}

		return c;
	}

	//	public List<ITool> getValues(){
	//		return valuesToCollection(null);
	//	}

	//	public ITool[] getValuesArray(Class clazz){
	//		List<ITool> list = getValues();
	//		ITool[] result = (ITool[])Array.newInstance(clazz, list.size());
	//		return list.toArray(result);
	//	}

	protected List<ITool> newList(int size) {
		return new ArrayList<ITool>(size);
	}

	@SuppressWarnings("unchecked")
	protected List<ITool> cloneList(List<ITool> l) {
		return (List<ITool>) ((ArrayList<ITool>) l).clone();
	}

	public List<ITool> putValuesToCollection(List<ITool> c) {
		for (CollectionEntry entry : collectionEntrySet()) {
			List<ITool> l = entry.getValue();
			c.addAll(l);
		}
		return c;
	}

	public void remove(ITool key, ITool value) {
		List<ITool> c = get(key, false);
		if (c != null) {
			if (c.remove(value) && c.size() == 0) {
				fMap.remove(key);
			}
		}
	}

	public ITool get(ITool key, int num) {
		List<ITool> l = get(key, false);
		if (l != null) {
			return l.get(num);
		}
		return null;
	}

	public ITool remove(ITool key, int num) {
		List<ITool> l = get(key, false);
		if (l != null) {
			ITool result = null;
			if (l.size() > num) {
				result = l.remove(num);
			}

			return result;
		}
		return null;
	}

	public ITool removeLast(ITool key) {
		List<ITool> l = get(key, false);
		if (l != null) {
			ITool result = null;
			if (l.size() > 0) {
				result = l.remove(l.size() - 1);
			}
			return result;
		}
		return null;
	}

	public void removeAll(ITool key, List<ITool> values) {
		List<ITool> c = get(key, false);
		if (c != null) {
			if (c.removeAll(values) && c.size() == 0) {
				fMap.remove(key);
			}
		}
	}

	public void clearEmptyLists() {
		for (Iterator<Entry<ITool, List<ITool>>> iter = fMap.entrySet().iterator(); iter.hasNext();) {
			Map.Entry<ITool, List<ITool>> entry = iter.next();
			if ((entry.getValue()).size() == 0)
				iter.remove();
		}
	}

	public Set<CollectionEntry> collectionEntrySet() {
		if (fCollectionEntrySet == null)
			fCollectionEntrySet = new CollectionEntrySet();
		return fCollectionEntrySet;
	}

	//	public void difference(ListMap map){
	//		for(Iterator<Entry<ITool, List<ITool>>> iter = map.fMap.entrySet().iterator(); iter.hasNext(); ){
	//			Map.Entry<ITool, List<ITool>> entry = iter.next();
	//			List<ITool> thisC = fMap.get(entry.getKey());
	//			if(thisC != null){
	//				if(thisC.removeAll(entry.getValue()) && thisC == null){
	//					fMap.remove(entry.getKey());
	//				}
	//			}
	//		}
	//	}

	//	public ValueIter valueIter(){
	//		return new ValueIter();
	//	}

	//	protected Collection createCollection(Object key){
	//		return new ArrayList(1);
	//	}

	@Override
	public Object clone() {
		try {
			ToolListMap clone = (ToolListMap) super.clone();
			@SuppressWarnings("unchecked")
			HashMap<ITool, List<ITool>> clone2 = (HashMap<ITool, List<ITool>>) fMap.clone();
			clone.fMap = clone2;
			for (Entry<ITool, List<ITool>> entry : clone.fMap.entrySet()) {
				entry.setValue(cloneList(entry.getValue()));
			}
		} catch (CloneNotSupportedException e) {
			ManagedBuilderCorePlugin.log(e);
		}
		return null;
	}

	//	protected Map getMap(boolean create){
	//		if(fMap == null && create)
	//			fMap = createMap();
	//		return fMap;
	//	}
	//
	//	protected Map createMap(){
	//		return new HashMap();
	//	}
}

Back to the top