Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b9fa395d314d965b75224a0d62dea37863fac684 (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
/*******************************************************************************
 * Copyright (c) 2007, 2016 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.buildproperties;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.eclipse.cdt.core.settings.model.util.CDataUtil;
import org.eclipse.cdt.managedbuilder.buildproperties.IBuildProperties;
import org.eclipse.cdt.managedbuilder.buildproperties.IBuildProperty;
import org.eclipse.core.runtime.CoreException;

public class BuildProperties implements IBuildProperties {
	private HashMap<String, IBuildProperty> fPropertiesMap = new HashMap<>();
	private ArrayList<String> fInexistentProperties;

	public BuildProperties() {

	}

	public BuildProperties(String properties) {
		StringTokenizer t = new StringTokenizer(properties, BuildPropertyManager.PROPERTIES_SEPARATOR);
		while (t.hasMoreTokens()) {
			String property = t.nextToken();
			try {
				BuildProperty prop = new BuildProperty(property);
				addProperty(prop);
			} catch (CoreException e) {
				if (fInexistentProperties == null)
					fInexistentProperties = new ArrayList<>();

				fInexistentProperties.add(property);
			}
		}

		if (fInexistentProperties != null)
			fInexistentProperties.trimToSize();
	}

	@SuppressWarnings("unchecked")
	public BuildProperties(BuildProperties properties) {
		fPropertiesMap.putAll(properties.fPropertiesMap);
		if (properties.fInexistentProperties != null)
			fInexistentProperties = (ArrayList<String>) properties.fInexistentProperties.clone();
	}

	@Override
	public IBuildProperty[] getProperties() {
		return fPropertiesMap.values().toArray(new BuildProperty[fPropertiesMap.size()]);
	}

	@Override
	public IBuildProperty getProperty(String id) {
		return fPropertiesMap.get(id);
	}

	void addProperty(IBuildProperty property) {
		fPropertiesMap.put(property.getPropertyType().getId(), property);
	}

	@Override
	public IBuildProperty setProperty(String propertyId, String propertyValue) throws CoreException {
		return setProperty(propertyId, propertyValue, false);
	}

	public IBuildProperty setProperty(String propertyId, String propertyValue, boolean force) throws CoreException {
		try {
			IBuildProperty property = BuildPropertyManager.getInstance().createProperty(propertyId, propertyValue);

			addProperty(property);

			return property;
		} catch (CoreException e) {
			if (force) {
				if (fInexistentProperties == null)
					fInexistentProperties = new ArrayList<>(1);

				fInexistentProperties.add(BuildProperty.toString(propertyId, propertyValue));
				fInexistentProperties.trimToSize();
			}
			throw e;
		}
	}

	@Override
	public IBuildProperty removeProperty(String id) {
		return fPropertiesMap.remove(id);
	}

	void removeProperty(BuildProperty property) {
		fPropertiesMap.remove(property.getPropertyType().getId());
	}

	@Override
	public String toString() {
		String props = toStringExistingProperties();
		if (fInexistentProperties != null) {
			String inexistentProps = CDataUtil.arrayToString(
					fInexistentProperties.toArray(new String[fInexistentProperties.size()]),
					BuildPropertyManager.PROPERTIES_SEPARATOR);
			if (props.length() != 0) {
				StringBuilder buf = new StringBuilder();
				buf.append(props).append(BuildPropertyManager.PROPERTIES_SEPARATOR).append(inexistentProps);
			} else {
				props = inexistentProps;
			}
		}
		return props;
	}

	public String toStringExistingProperties() {
		int size = fPropertiesMap.size();
		if (size == 0)
			return ""; //$NON-NLS-1$
		else if (size == 1)
			return fPropertiesMap.values().iterator().next().toString();

		StringBuilder buf = new StringBuilder();
		Iterator<IBuildProperty> iter = fPropertiesMap.values().iterator();
		buf.append(iter.next().toString());
		for (; iter.hasNext();) {
			buf.append(BuildPropertyManager.PROPERTIES_SEPARATOR);
			buf.append(iter.next().toString());
		}
		return buf.toString();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object clone() {
		try {
			BuildProperties clone = (BuildProperties) super.clone();

			if (fInexistentProperties != null)
				clone.fInexistentProperties = (ArrayList<String>) fInexistentProperties.clone();

			clone.fPropertiesMap = (HashMap<String, IBuildProperty>) fPropertiesMap.clone();
			/*			for(Iterator iter = clone.fPropertiesMap.entrySet().iterator(); iter.hasNext();){
							Map.Entry entry = (Map.Entry)iter.next();
							BuildProperty prop = (BuildProperty)entry.getValue();
							entry.setValue(prop.clone());
						}
			*/
			return clone;
		} catch (CloneNotSupportedException e) {
		}
		return null;
	}

	@Override
	public void clear() {
		fPropertiesMap.clear();
		fInexistentProperties.clear();
	}

	@Override
	public boolean containsValue(String propertyId, String valueId) {
		IBuildProperty prop = getProperty(propertyId);
		if (prop != null) {
			return valueId.equals(prop.getValue().getId());
		}
		return false;
	}

}

Back to the top