diff --git a/src/main/java/li/strolch/runtime/agent/ElementMap.java b/src/main/java/li/strolch/runtime/agent/ElementMap.java index 3baab9aae..c3d7e3571 100644 --- a/src/main/java/li/strolch/runtime/agent/ElementMap.java +++ b/src/main/java/li/strolch/runtime/agent/ElementMap.java @@ -21,6 +21,9 @@ */ package li.strolch.runtime.agent; +import java.util.List; +import java.util.Set; + import li.strolch.model.StrolchElement; /** @@ -29,8 +32,22 @@ import li.strolch.model.StrolchElement; */ public interface ElementMap { + public boolean hasType(String type); + + public boolean hasElement(String type, String id); + public T getBy(String type, String id); + public List getAllElements(); + + public List getElementsBy(String type); + + public Set getTypes(); + + public Set getAllKeys(); + + public Set getKeysBy(String type); + public void add(T element); public void update(T element); diff --git a/src/main/java/li/strolch/runtime/agent/InMemoryElementMap.java b/src/main/java/li/strolch/runtime/agent/InMemoryElementMap.java index 560eb24d6..5c6a21c15 100644 --- a/src/main/java/li/strolch/runtime/agent/InMemoryElementMap.java +++ b/src/main/java/li/strolch/runtime/agent/InMemoryElementMap.java @@ -22,8 +22,12 @@ package li.strolch.runtime.agent; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Set; import li.strolch.model.StrolchElement; import li.strolch.runtime.component.ComponentContainer; @@ -37,6 +41,8 @@ import ch.eitchnet.utils.helper.StringHelper; */ public abstract class InMemoryElementMap extends StrolchComponent implements ElementMap { + private Map> elementMap; + /** * @param container * @param componentName @@ -45,8 +51,6 @@ public abstract class InMemoryElementMap extends Strol super(container, componentName); } - private Map> elementMap; - @Override public void initialize(ComponentConfiguration configuration) { this.elementMap = new HashMap<>(); @@ -57,7 +61,7 @@ public abstract class InMemoryElementMap extends Strol public void start() { super.start(); } - + @Override public void stop() { this.elementMap.clear(); @@ -70,16 +74,84 @@ public abstract class InMemoryElementMap extends Strol super.destroy(); } + @Override + public boolean hasElement(String type, String id) { + return this.elementMap.containsKey(type) && this.elementMap.get(type).containsKey(id); + } + + @Override + public boolean hasType(String type) { + return this.elementMap.containsKey(type); + } + @Override public T getBy(String type, String id) { if (StringHelper.isEmpty(type) || StringHelper.isEmpty(id)) throw new IllegalArgumentException("type and id may not be null!"); //$NON-NLS-1$ Map byTypeMap = this.elementMap.get(type); - if (byTypeMap == null) - return null; + if (byTypeMap == null || byTypeMap.isEmpty()) { + String msg = MessageFormat.format("There is no element with the type {0} and id {1}", type, id); //$NON-NLS-1$ + throw new IllegalArgumentException(msg); + } - return byTypeMap.get(id); + T element = byTypeMap.get(id); + if (element == null) { + String msg = MessageFormat.format("There is no element with the type {0} and id {1}", type, id); //$NON-NLS-1$ + throw new IllegalArgumentException(msg); + } + + return element; + } + + public List getAllElements() { + + List allElements = new ArrayList<>(); + for (Map elementsByType : this.elementMap.values()) { + allElements.addAll(elementsByType.values()); + } + + return allElements; + } + + public List getElementsBy(String type) { + if (StringHelper.isEmpty(type)) + throw new IllegalArgumentException("type may not be null!"); //$NON-NLS-1$ + + Map elementsByType = this.elementMap.get(type); + if (elementsByType == null || elementsByType.isEmpty()) { + String msg = MessageFormat.format("There are no elements with the type {0}", type); //$NON-NLS-1$ + throw new IllegalArgumentException(msg); + } + + return new ArrayList<>(elementsByType.values()); + } + + public Set getTypes() { + return new HashSet<>(this.elementMap.keySet()); + } + + public Set getAllKeys() { + + Set allKeys = new HashSet<>(); + for (Map elementsByType : this.elementMap.values()) { + allKeys.addAll(elementsByType.keySet()); + } + + return allKeys; + } + + public Set getKeysBy(String type) { + if (StringHelper.isEmpty(type)) + throw new IllegalArgumentException("type may not be null!"); //$NON-NLS-1$ + + Map elementsByType = this.elementMap.get(type); + if (elementsByType == null || elementsByType.isEmpty()) { + String msg = MessageFormat.format("There are no elements with the type {0}", type); //$NON-NLS-1$ + throw new IllegalArgumentException(msg); + } + + return new HashSet<>(elementsByType.keySet()); } @Override