com.softicar.platform.common.container.map.instance.ClassInstanceListMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.container.map.instance;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This class maps classes to a {@link List} of instances of that class.
*
* @author Oliver Richers
*/
public class ClassInstanceListMap {
private final Map, List> map;
public ClassInstanceListMap() {
this.map = new HashMap<>();
}
@SuppressWarnings("unchecked")
public void add(T instance) {
var list = (List) map.computeIfAbsent(instance.getClass(), dummy -> new ArrayList<>());
list.add(instance);
}
@SuppressWarnings("unchecked")
public List getInstances(Class classOfInstances) {
return (List) map.getOrDefault(classOfInstances, Collections.emptyList());
}
}