[New] added many querying methods to the ElementMap interface
Implemented it in the InMemoryMap abstraction
This commit is contained in:
parent
66e076ea49
commit
643f846f32
|
@ -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<T extends StrolchElement> {
|
||||
|
||||
public boolean hasType(String type);
|
||||
|
||||
public boolean hasElement(String type, String id);
|
||||
|
||||
public T getBy(String type, String id);
|
||||
|
||||
public List<T> getAllElements();
|
||||
|
||||
public List<T> getElementsBy(String type);
|
||||
|
||||
public Set<String> getTypes();
|
||||
|
||||
public Set<String> getAllKeys();
|
||||
|
||||
public Set<String> getKeysBy(String type);
|
||||
|
||||
public void add(T element);
|
||||
|
||||
public void update(T element);
|
||||
|
|
|
@ -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<T extends StrolchElement> extends StrolchComponent implements ElementMap<T> {
|
||||
|
||||
private Map<String, Map<String, T>> elementMap;
|
||||
|
||||
/**
|
||||
* @param container
|
||||
* @param componentName
|
||||
|
@ -45,8 +51,6 @@ public abstract class InMemoryElementMap<T extends StrolchElement> extends Strol
|
|||
super(container, componentName);
|
||||
}
|
||||
|
||||
private Map<String, Map<String, T>> elementMap;
|
||||
|
||||
@Override
|
||||
public void initialize(ComponentConfiguration configuration) {
|
||||
this.elementMap = new HashMap<>();
|
||||
|
@ -57,7 +61,7 @@ public abstract class InMemoryElementMap<T extends StrolchElement> extends Strol
|
|||
public void start() {
|
||||
super.start();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
this.elementMap.clear();
|
||||
|
@ -70,16 +74,84 @@ public abstract class InMemoryElementMap<T extends StrolchElement> 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<String, T> 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<T> getAllElements() {
|
||||
|
||||
List<T> allElements = new ArrayList<>();
|
||||
for (Map<String, T> elementsByType : this.elementMap.values()) {
|
||||
allElements.addAll(elementsByType.values());
|
||||
}
|
||||
|
||||
return allElements;
|
||||
}
|
||||
|
||||
public List<T> getElementsBy(String type) {
|
||||
if (StringHelper.isEmpty(type))
|
||||
throw new IllegalArgumentException("type may not be null!"); //$NON-NLS-1$
|
||||
|
||||
Map<String, T> 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<String> getTypes() {
|
||||
return new HashSet<>(this.elementMap.keySet());
|
||||
}
|
||||
|
||||
public Set<String> getAllKeys() {
|
||||
|
||||
Set<String> allKeys = new HashSet<>();
|
||||
for (Map<String, T> elementsByType : this.elementMap.values()) {
|
||||
allKeys.addAll(elementsByType.keySet());
|
||||
}
|
||||
|
||||
return allKeys;
|
||||
}
|
||||
|
||||
public Set<String> getKeysBy(String type) {
|
||||
if (StringHelper.isEmpty(type))
|
||||
throw new IllegalArgumentException("type may not be null!"); //$NON-NLS-1$
|
||||
|
||||
Map<String, T> 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
|
||||
|
|
Loading…
Reference in New Issue