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

com.github.dcsolutions.kalinka.sub.util.KafkaPartitionResolver Maven / Gradle / Ivy

There is a newer version: 0.0.10
Show newest version
package com.github.dcsolutions.kalinka.sub.util;

import static com.github.dcsolutions.kalinka.util.LangUtil.combine;
import static com.github.dcsolutions.kalinka.util.LangUtil.splitCsStrings;

import java.util.List;

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

public class KafkaPartitionResolver {

	public static List partitionsFromString(final String partitionsString) {

		Preconditions.checkNotNull(partitionsString);
		Preconditions.checkState(!partitionsString.isEmpty() && partitionsString.matches("(\\d+[,-]{1})+\\d+"));

		final List splitted = splitCsStrings(partitionsString);
		return splitted.stream().map(s -> {
			final List ranges = Splitter.on('-').splitToList(s);

			final List singleElems = Lists.newArrayList();

			if (ranges.isEmpty()) {
				return singleElems;
			}
			if (ranges.size() == 1) {
				singleElems.add(Integer.valueOf(ranges.get(0)));
				return singleElems;
			}
			for (int k = 0; k < ranges.size() - 1; k++) {
				singleElems.addAll(resolveRange(Integer.valueOf(ranges.get(k)), Integer.valueOf(ranges.get(k + 1))));
			}
			return singleElems;
		}).reduce(Lists.newArrayList(), (l1, l2) -> {
			return combine(l1, l2);
		});
	}

	private static List resolveRange(final Integer first, final Integer second) {

		if (second < first) {
			throw new IllegalArgumentException("In a range the second element must be > first element");
		}
		final List result = Lists.newArrayList();
		for (int i = first; i <= second; i++) {
			result.add(i);
		}
		return result;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy