Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3f2555ad12e11b03a5a2fd9ea12cf7a56b9d3781 (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
/*******************************************************************************
 * 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.ArrayList;
import java.util.Iterator;

/**
 * List implementation of IProblemPreference.
 * 
 * @noextend This class is not intended to be extended by clients.
 */
public class ListProblemPreference extends AbstractProblemPreference implements
		IProblemPreferenceCompositeValue, IProblemPreferenceCompositeDescriptor {
	protected ArrayList<IProblemPreference> list = new ArrayList<IProblemPreference>(
			1);

	/**
	 * @param key
	 * @param label
	 */
	public ListProblemPreference(String key, String label) {
		setKey(key);
		setLabel(label);
	}

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

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

	/**
	 * Get parameter into for element equal to key's int value.
	 * 
	 * @throws NumberFormatException
	 *             if key is not number
	 * @throws ArrayIndexOutOfBoundsException
	 *             is index is out of bound
	 */
	public IProblemPreference getChildDescriptor(String key)
			throws NumberFormatException {
		if (key == null) {
			// special case if all element are the same return first, if key is
			// null
			return (IProblemPreference) getChildDescriptor(0).clone();
		}
		Integer iv = Integer.valueOf(key);
		if (iv.intValue() >= list.size()) {
			// special case if all element are the same return first clone
			IProblemPreference childInfo = (IProblemPreference) getChildDescriptor(
					0).clone();
			return childInfo;
		}
		return getChildDescriptor(iv.intValue());
	}

	/**
	 * Set i'th element of parameter info, if all are the same i is 0
	 * 
	 * @param i
	 * @param info
	 */
	public void setChildDescriptor(int i, IProblemPreference info) {
		if (info != null) {
			while (i >= list.size()) {
				list.add(null);
			}
			list.set(i, info);
		} else {
			while (i == list.size() - 1) {
				list.remove(i);
			}
		}
	}

	/**
	 * If all list elements have same info it is enough to set only first one
	 * (index 0). When value is set for the other it will be replicated.
	 */
	public void addChildDescriptor(IProblemPreference info) {
		Integer iv = Integer.valueOf(info.getKey());
		IProblemPreference desc = (IProblemPreference) info.clone();
		desc.setParent(this);
		setChildDescriptor(iv, desc);
	}

	public IProblemPreference getChildDescriptor(int i) {
		return list.get(i);
	}

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

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

	public void addChildValue(String key, Object value) {
		IProblemPreference pref = getChildDescriptor(key);
		pref.setValue(value);
		// because descriptor can be phantom we have to set preference forcefully
		setChildDescriptor(Integer.parseInt(key), pref);
	}

	public void removeChildValue(String key) {
		int index = Integer.parseInt(key);
		list.remove(index);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object clone() {
		ListProblemPreference list1 = (ListProblemPreference) super.clone();
		list1.list = (ArrayList<IProblemPreference>) list.clone();
		return list1;
	}

	public String exportValue() {
		StringBuffer buf = new StringBuffer("("); //$NON-NLS-1$
		for (Iterator<IProblemPreference> iterator = list.iterator(); iterator
				.hasNext();) {
			IProblemPreference d = iterator.next();
			buf.append(d.exportValue());
			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;
		int index = 0;
		try {
			token = tokenizer.nextToken();
			if (token != '(')
				throw new IllegalArgumentException(str);
			while (true) {
				token = tokenizer.nextToken();
				String val = tokenizer.sval;
				String ik = String.valueOf(index);
				IProblemPreference desc = getChildDescriptor(ik);
				if (desc != null) {
					desc.importValue(val);
					addChildValue(ik, desc.getValue());
				}
				token = tokenizer.nextToken();
				if (token == ')')
					break;
				if (token != ',')
					throw new IllegalArgumentException(str);
				index++;
			}
		} catch (IOException e) {
			throw new IllegalArgumentException(str);
		}
	}

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

Back to the top