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

org.requirementsascode.ModelElementContainer Maven / Gradle / Ivy

There is a newer version: 2.0
Show newest version
package org.requirementsascode;

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

import org.requirementsascode.exception.ElementAlreadyInModel;
import org.requirementsascode.exception.NoSuchElementInModel;

/**
 * Contains static helper methods to ease the implementation of model creation
 * and finding elements in it, using maps.
 *
 * @author b_muth
 */
class ModelElementContainer {
	static  T findModelElement(String modelElementName,
			Map modelElementNameToElementMap) {
		Objects.requireNonNull(modelElementName);
		Objects.requireNonNull(modelElementNameToElementMap);

		if (!hasModelElement(modelElementName, modelElementNameToElementMap)) {
			throw new NoSuchElementInModel(modelElementName);
		}
		T modelElement = modelElementNameToElementMap.get(modelElementName);
		return modelElement;
	}

	static  boolean hasModelElement(String modelElementName,
			Map modelElementNameToElementMap) {
		Objects.requireNonNull(modelElementName);
		Objects.requireNonNull(modelElementNameToElementMap);

		boolean hasModelElement = modelElementNameToElementMap.containsKey(modelElementName);
		return hasModelElement;
	}

	static  void saveModelElement(T modelElement, Map modelElementNameToElementMap) {
		Objects.requireNonNull(modelElement);
		Objects.requireNonNull(modelElementNameToElementMap);

		String modelElementName = modelElement.getName();
		if (hasModelElement(modelElementName, modelElementNameToElementMap)) {
			throw new ElementAlreadyInModel(modelElementName);
		}
		modelElementNameToElementMap.put(modelElementName, modelElement);
	}

	static  Collection getModelElements(Map modelElementNameToElementMap) {
		Objects.requireNonNull(modelElementNameToElementMap);

		return modelElementNameToElementMap.values();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy