Skip to main content
summaryrefslogtreecommitdiffstats
blob: f57632f957de7fd31773ec7620201408b4ad7dbd (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
/*******************************************************************************
 * 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 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>();

	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);
	}

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

	@Override
	public void setType(PreferenceType type) {
		throw new UnsupportedOperationException();
	}

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

	/**
	 * Put parameter info into the map for element with the key equals to
	 * info.getKey()
	 * 
	 * @param i
	 * @param info
	 */
	public void addChildDescriptor(IProblemPreference info) {
		IProblemPreference desc = (IProblemPreference) info.clone();
		desc.setParent(this);
		hash.put(info.getKey(), desc);
	}

	public IProblemPreference[] getChildDescriptors() {
		return hash.values().toArray(
				new IProblemPreference[hash.values().size()]);
	}

	public Object getChildValue(String key) {
		IProblemPreference childInfo = getChildDescriptor(key);
		return childInfo.getValue();
	}

	public void addChildValue(String key, Object value) {
		getChildDescriptor(key).setValue(value);
	}

	public void removeChildValue(String key) {
		hash.remove(key);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object clone() {
		MapProblemPreference map = (MapProblemPreference) super.clone();
		map.hash = (LinkedHashMap<String, IProblemPreference>) hash.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$
	}

	public void importValue(String str) {
		StreamTokenizer tokenizer = getImportTokenizer(str);
		int token;
		try {
			token = tokenizer.nextToken();
			if (token != '{')
				throw new IllegalArgumentException(str);
			while (true) {
				token = tokenizer.nextToken();
				String key = tokenizer.sval;
				token = tokenizer.nextToken();
				if (token != '=')
					throw new IllegalArgumentException(str);
				token = tokenizer.nextToken();
				if (token != '>')
					throw new IllegalArgumentException(str);
				token = tokenizer.nextToken();
				String val = tokenizer.sval;
				IProblemPreference desc = getChildDescriptor(key);
				if (desc != null) {
					desc.importValue(val);
				} else {
					//putChildValue(key, val);
				}
				token = tokenizer.nextToken();
				if (token == '}')
					break;
				if (token != ',')
					throw new IllegalArgumentException(str);
			}
		} catch (IOException e) {
			throw new IllegalArgumentException(str);
		}
	}

	public void removeChildDescriptor(IProblemPreference info) {
		hash.remove(info);
	}
}

Back to the top