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

com.softicar.platform.common.container.map.set.SetMaps Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.map.set;

import com.softicar.platform.common.container.map.list.ListTreeMap;
import com.softicar.platform.common.core.exceptions.SofticarDeveloperException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.TreeSet;

public class SetMaps {

	public static , T extends Comparable> Map toInvertedMap(SetMap input, boolean allowCollisions) {

		Map output = new TreeMap<>();

		for (Entry> entry: input.entrySet()) {
			S inputKey = entry.getKey();

			for (T inputValue: entry.getValue()) {
				if (allowCollisions || !output.containsKey(inputValue)) {
					output.put(inputValue, inputKey);
				} else {
					throw new SofticarDeveloperException("Collision detected while inverting a %s (in collision-free mode).", SetMap.class.getSimpleName());
				}
			}
		}

		return output;
	}

	public static , T extends Comparable> SetMap toInvertedSetMap(SetMap input) {

		SetMap output = new SetMap<>();

		for (Entry> entry: input.entrySet()) {
			S inputKey = entry.getKey();

			for (T inputValue: entry.getValue()) {
				output.addToSet(inputValue, inputKey);
			}
		}

		return output;
	}

	public static , T extends Comparable> ListTreeMap toListMap(SetMap input) {

		ListTreeMap output = new ListTreeMap<>();

		for (Entry> entry: input.entrySet()) {
			S inputKey = entry.getKey();

			for (T inputValue: entry.getValue()) {
				output.addToList(inputValue, inputKey);
			}
		}

		return output;
	}
}