summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/param/MapProblemPreference.java
blob: 6e865f0692b73167f415f5c518c6bdd5bd9aa252 (plain)
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
/*******************************************************************************
 * Copyright (c) 2009,2010 QNX Software Systems
 * 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:
 *    QNX Software Systems (Alena Laskavaia)  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.core.param;

import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.cdt.codan.core.model.AbstractCheckerWithProblemPreferences;

/**
 * MapProblemPreference - for checker that needs more than one preferences and
 * they all differently "named".
 * For example checker for parameter names shadowing would have two boolean
 * options:
 * "check contructors" and
 * "check setters". In this case you use this type.
 * {@link AbstractCheckerWithProblemPreferences} class has map as default top
 * level parameter preference.
 * 
 * @noextend This class is not intended to be extended by clients.
 */
public class MapProblemPreference extends AbstractProblemPreference implements
		IProblemPreferenceCompositeValue, IProblemPreferenceCompositeDescriptor {
	protected LinkedHashMap<String, IProblemPreference> hash = new LinkedHashMap<String, IProblemPreference>();

	/**
	 * Default constuctor
	 */
	public MapProblemPreference() {
		super();
	}

	/**
	 * @param key
	 *        - key for itself
	 * @param label
	 *        - label for this group of parameters
	 */
	public MapProblemPreference(String key, String label) {
		setKey(key);
		setLabel(label);
	}

	public PreferenceType getType() {
		return PreferenceType.TYPE_MAP;
	}

	/**
	 * Get parameter preference for element by key
	 * 
	 */
	public IProblemPreference getChildDescriptor(String key) {
		return hash.get(key);
	}

	/**
	 * Adds or replaces child descriptor and value for the element with the key
	 * equals to desc.getKey(). The desc object would be put in the map, some of
	 * its field may be modified.
	 * 
	 * @param desc
	 */
	public IProblemPreference addChildDescriptor(IProblemPreference desc) {
		((AbstractProblemPreference) desc).setParent(this);
		hash.put(desc.getKey(), desc);
		return desc;
	}

	/**
	 * Return list of child descriptors. Client should threat returned value as
	 * read only,
	 * and not assume that modifying its elements would modify actual child
	 * values.
	 */
	public IProblemPreference[] getChildDescriptors() {
		return hash.values().toArray(
				new IProblemPreference[hash.values().size()]);
	}

	/**
	 * Returns value of the child element by its key
	 */
	public Object getChildValue(String key) {
		IProblemPreference childInfo = getChildDescriptor(key);
		return childInfo.getValue();
	}

	/**
	 * Set child value by its key
	 */
	public void setChildValue(String key, Object value) {
		IProblemPreference pref = getChildDescriptor(key);
		if (pref == null)
			throw new IllegalArgumentException("Preference for " + key //$NON-NLS-1$
					+ " must exists before setting its value"); //$NON-NLS-1$
		pref.setValue(value);
		hash.put(key, pref); // cannot assume getChildDescriptor returns shared value
	}

	/**
	 * Removes child value and descriptor by key
	 */
	public void removeChildValue(String key) {
		hash.remove(key);
	}

	@Override
	public Object clone() {
		MapProblemPreference map = (MapProblemPreference) super.clone();
		map.hash = new LinkedHashMap<String, IProblemPreference>();
		for (Iterator<String> iterator = hash.keySet().iterator(); iterator
				.hasNext();) {
			String key = iterator.next();
			map.hash.put(key, (IProblemPreference) hash.get(key).clone());
		}
		return map;
	}

	public String exportValue() {
		StringBuffer buf = new StringBuffer("{"); //$NON-NLS-1$
		for (Iterator<String> iterator = hash.keySet().iterator(); iterator
				.hasNext();) {
			String key = iterator.next();
			IProblemPreference d = hash.get(key);
			buf.append(key + "=>" + d.exportValue()); //$NON-NLS-1$
			if (iterator.hasNext())
				buf.append(","); //$NON-NLS-1$
		}
		return buf.toString() + "}"; //$NON-NLS-1$
	}

	@Override
	public void importValue(String str) {
		StreamTokenizer tokenizer = getImportTokenizer(str);
		try {
			importValue(tokenizer);
		} catch (IllegalArgumentException e) {
			throw new IllegalArgumentException(str + ":" + e.toString(), e); //$NON-NLS-1$
		}
	}

	/**
	 * @param tokenizer
	 */
	@Override
	public void importValue(StreamTokenizer tokenizer) {
		int token;
		try {
			token = tokenizer.nextToken();
			String chara = String.valueOf((char) token);
			if (token != '{') {
				throw new IllegalArgumentException(chara);
			}
			while (true) {
				token = tokenizer.nextToken();
				String key = tokenizer.sval;
				token = tokenizer.nextToken();
				if (token != '=')
					throw new IllegalArgumentException(chara);
				token = tokenizer.nextToken();
				if (token != '>')
					throw new IllegalArgumentException(chara);
				IProblemPreference desc = getChildDescriptor(key);
				if (desc != null && desc instanceof AbstractProblemPreference) {
					((AbstractProblemPreference) desc).importValue(tokenizer);
					setChildValue(key, desc.getValue());
				}
				token = tokenizer.nextToken();
				if (token == '}')
					break;
				if (token != ',')
					throw new IllegalArgumentException(chara);
			}
		} catch (IOException e) {
			throw new IllegalArgumentException(e);
		}
	}

	/**
	 * Removes child descriptor by its key
	 */
	public void removeChildDescriptor(IProblemPreference info) {
		hash.remove(info.getKey());
	}

	/**
	 * @return size of the map
	 */
	public int size() {
		return hash.size();
	}

	/**
	 * Clears the map
	 */
	public void clear() {
		hash.clear();
	}

	@Override
	public String toString() {
		return hash.values().toString();
	}

	/**
	 * Value of this preference is a map key=>value of child preferences.
	 * Modifying this returned map would not change internal state of this
	 * object.
	 */
	@Override
	public Object getValue() {
		LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
		for (Iterator<IProblemPreference> iterator = hash.values().iterator(); iterator
				.hasNext();) {
			IProblemPreference pref = iterator.next();
			map.put(pref.getKey(), pref.getValue());
		}
		return map;
	}

	/**
	 * Set values for this object child elements. Elements are not present in
	 * this map would be removed.
	 * Preference descriptors for the keys must be set before calling this
	 * method, unless value if instanceof {@link IProblemPreference}.
	 * 
	 * @param value - must be Map<String,Object>
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void setValue(Object value) {
		Map<String, Object> map = (Map<String, Object>) value;
		LinkedHashMap<String, IProblemPreference> hash2 = (LinkedHashMap<String, IProblemPreference>) hash
				.clone();
		hash.clear();
		for (Iterator<String> iterator = map.keySet().iterator(); iterator
				.hasNext();) {
			String key = iterator.next();
			Object value2 = map.get(key);
			if (value2 instanceof IProblemPreference) {
				hash.put(key, (IProblemPreference) value2);
			} else {
				setChildValue(key, value2);
				IProblemPreference pref = hash2.get(key);
				pref.setValue(value2);
				hash.put(key, pref);
			}
		}
	}
}