Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0066722a2458dc72a246385f083f8e1bfdb742a3 (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation and others.
 * 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:
 * IBM - Initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.make.internal.core.scannerconfig.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * Represents a symbol definition with possible multiple values
 * example:
 * 		LOG_LEVEL
 * 		LOG_LEVEL = 2
 * 		LOG_LEVEL = LOG_BASE + 1
 * @author vhirsl
 */
public class SymbolEntry {
	private static final String UNSPECIFIED_VALUE = "1";	//$NON-NLS-1$
	private String name;
	private Map values;	// Values can be either in the active (selected) group or in the removed group 
	
//	public SymbolEntry(String name) {
//		this.name = name;
//	}

	public SymbolEntry(String name, String value) {
		this(name, value, true);
	}
	public SymbolEntry(String name, String value, boolean active) {
		this.name = name;
		if (values == null) {
			values = new LinkedHashMap(1);
		}
		values.put(value, Boolean.valueOf(active));
	}
	public SymbolEntry(SymbolEntry se) {
		name = se.name;
		// deep copy
		values = new LinkedHashMap(se.values.size());
		for (Iterator i = se.values.keySet().iterator(); i.hasNext(); ) {
			String key = (String) i.next();
			Boolean value = (Boolean) se.values.get(key);
			values.put(key, Boolean.valueOf(value.booleanValue()));
		}
	}

	public boolean add(String value) {
		return add(value, true);
	}
	public boolean add(String value, boolean active) {
		boolean rc = false;
		if (!values.containsKey(value)) {
			values.put(value, Boolean.valueOf(active));
			rc = true;
		}
		return rc;
	}
	public void replace(String value, boolean active) {
		values.put(value, Boolean.valueOf(active));
	}

//	private void addAll(SymbolEntry se) {
//		values.putAll(se.values);
//	}
	
	public void remove(String value) {
		values.remove(value);
	}
	public void removeAll() {
		values = null;
	}
	
	public List getActive() {
		return get(true, true, true);
	}
	public List getActiveRaw() {
		return get(false, true, true);
	}
	
	public List getRemoved() {
		return get(true, true, false);
	}
	public List getRemovedRaw() {
		return get(false, true, false);
	}
	
	public List getAll() {
		return get(true, false, true /*don't care*/);
	}
	public List getAllRaw() {
		return get(false, false, true /*don't care*/);
	}

	/**
	 * Utility function to retrieve values as a set.
	 * 
	 * @param format - false = raw
	 * @param subset - false = all
	 * @param active - false = removed
	 * @return List
	 */
	private List get(boolean format, boolean subset, boolean active) {
		List rv = new ArrayList(values.size());
		for (Iterator i = values.keySet().iterator(); i.hasNext(); ) {
			String val = (String) i.next();
			if (subset && ((Boolean) values.get(val)).booleanValue() != active)
				continue;
			if (format) {
				rv.add(name + "=" + (val == null ? UNSPECIFIED_VALUE : val));//$NON-NLS-1$
			}
			else {
				rv.add(name + (val == null ? "" : "=" + val));//$NON-NLS-1$ //$NON-NLS-2$
			}				
		}
		return rv;
	}
	/**
	 * Returns only value part of all active entries
	 * @return List
	 */
	public List getValuesOnly(boolean active) {
		List rv = new ArrayList(values.size());
		for (Iterator i = values.keySet().iterator(); i.hasNext(); ) {
			String val = (String) i.next();
			if (((Boolean) values.get(val)).booleanValue() == active) {
				rv.add(val == null ? UNSPECIFIED_VALUE : val);
			}
		}
		return rv;
	}
	
	public int numberOfValues() {
		return values.size();
	}
	
	public String toString() {
		StringBuffer buffer = new StringBuffer(name);
		buffer.append(':');
		for (Iterator i = values.keySet().iterator(); i.hasNext(); ) {
			String val = (String) i.next();
			buffer.append('\t');
			buffer.append((val == null) ? "null" : val);//$NON-NLS-1$
			if (((Boolean) values.get(val)).booleanValue() == true) {
				buffer.append("(active)");//$NON-NLS-1$
			}
			buffer.append('\n');
		}
		return buffer.toString();
	}
}

Back to the top