aboutsummaryrefslogtreecommitdiffstats
path: root/buildSrc/src/main/java/tools/refinery/gradle/utils/SonarPropertiesUtils.java
blob: 3810fccfd3f2e12dbaf23795c531b229a1da4d08 (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
package tools.refinery.gradle.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

public final class SonarPropertiesUtils {
	private SonarPropertiesUtils() {
		throw new IllegalStateException("This is a static utility class and should not be instantiated directly");
	}

	/**
	 * Adds the entries to a Sonar property of list type.
	 * <p>
	 * According to the Sonar Gradle documentation for {@link org.sonarqube.gradle.SonarProperties}, property values
	 * are converted to Strings as follows:
	 * <ul>
	 * <li>{@code Iterable}s are recursively converted and joined into a comma-separated String.</li>
	 * <li>All other values are converted to Strings by calling their {@code toString()} method.</li>
	 * </ul>
	 * Therefore, we use {@link ArrayList} to retain lists entries, which will be recursively converted later.
	 *
	 * @param properties   The sonar properties map returned by
	 *                     {@link org.sonarqube.gradle.SonarProperties#getProperties()}.
	 * @param propertyName The name of the property to append to.
	 * @param entries      The entries to append.
	 */
	public static void addToList(Map<String, Object> properties, String propertyName, String... entries) {
		ArrayList<Object> newValue;
		var currentValue = properties.get(propertyName);
		if (currentValue instanceof ArrayList<?> currentList) {
			@SuppressWarnings("unchecked")
			var objectList = (ArrayList<Object>) currentList;
			newValue = objectList;
		} else if (currentValue == null) {
			newValue = new ArrayList<>(entries.length);
		} else {
			newValue = new ArrayList<>(entries.length + 1);
			newValue.add(currentValue);
		}
		Collections.addAll(newValue, entries);
		properties.put(propertyName, newValue);
	}
}