All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.bnd.build.model.conversions.CapReqFormatter Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package aQute.bnd.build.model.conversions;

import java.util.Collection;
import java.util.Map;

import org.osgi.framework.Version;

import aQute.bnd.header.OSGiHeader;
import aQute.bnd.stream.MapStream;
import aQute.lib.strings.Strings;

abstract class CapReqFormatter {
	String convert(String namespace, Map directives, Map attributes) {
		StringBuilder builder = new StringBuilder(namespace);
		MapStream.of(directives)
			.sortedByKey() // reproducible output
			.forEachOrdered((key, value) -> {
				builder.append(';')
					.append(key)
					.append(':')
					.append('=');
				OSGiHeader.quote(builder, value, '\'');
			});
		MapStream.of(attributes)
			.sortedByKey() // reproducible output
			.forEachOrdered((key, value) -> {
				builder.append(';')
					.append(key)
					.append(valueType(value))
					.append('=');
				OSGiHeader.quote(builder, valueString(value), '\'');
			});
		return builder.toString();
	}

	private static String valueType(Object value) {
		if (value instanceof String) {
			return "";
		}
		if (value instanceof Version) {
			return ":Version";
		}
		if (value instanceof Long) {
			return ":Long";
		}
		if (value instanceof Collection) {
			Collection v = (Collection) value;
			if (v.isEmpty()) {
				return ":List";
			}
			Object first = v.iterator()
				.next();
			if (first instanceof String) {
				return ":List";
			}
			if (first instanceof Version) {
				return ":List";
			}
			if (first instanceof Long) {
				return ":List";
			}
			if (first instanceof Double) {
				return ":List";
			}
		}
		if (value instanceof Double) {
			return ":Double";
		}
		return "";
	}

	private static String valueString(Object value) {
		if (value instanceof Collection) {
			Collection v = (Collection) value;
			return v.stream()
				.map(String::valueOf)
				.collect(Strings.joining());
		}
		return String.valueOf(value);
	}

	public String error(String msg) {
		return msg;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy