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

org.stagemonitor.configuration.converter.SetValueConverter Maven / Gradle / Ivy

There is a newer version: 1.49.0
Show newest version
package org.stagemonitor.configuration.converter;

import org.stagemonitor.util.StringUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import static java.util.Collections.emptySet;

public class SetValueConverter extends AbstractCollectionValueConverter, T> {

	public static final SetValueConverter STRINGS_VALUE_CONVERTER =
			new SetValueConverter(StringValueConverter.INSTANCE);

	public static final SetValueConverter LOWER_STRINGS_VALUE_CONVERTER =
			new SetValueConverter(StringValueConverter.LOWER_CASE);

	public static final ValueConverter> INTEGERS =
			new SetValueConverter(IntegerValueConverter.INSTANCE);

	public SetValueConverter(ValueConverter valueConverter) {
		super(valueConverter);
	}

	@Override
	public Collection convert(String s) {
		if (s != null && s.length() > 0) {
			final LinkedHashSet result = new LinkedHashSet();
			for (String split : s.split(",")) {
				result.add(valueConverter.convert(split.trim()));
			}
			return Collections.unmodifiableSet(result);
		}
		return emptySet();
	}

	@Override
	public String toString(Collection value) {
		return StringUtils.asCsv(value);
	}

	public static  Set immutableSet(T... values) {
		return Collections.unmodifiableSet(new LinkedHashSet(Arrays.asList(values)));
	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy