[New] The GroupedParameterizedElement.get*() helpers handle missing bags

This commit is contained in:
Robert von Burg 2021-01-20 18:20:45 +01:00
parent 6f8274f631
commit fac7d659d9
1 changed files with 48 additions and 65 deletions

View File

@ -15,6 +15,7 @@
*/ */
package li.strolch.model; package li.strolch.model;
import static java.util.Collections.*;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static li.strolch.model.StrolchModelConstants.*; import static li.strolch.model.StrolchModelConstants.*;
import static li.strolch.utils.helper.StringHelper.isEmpty; import static li.strolch.utils.helper.StringHelper.isEmpty;
@ -89,183 +90,157 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
@Override @Override
public String getString(String paramKey) throws StrolchModelException { public String getString(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? "" : bag.getString(paramKey);
return "";
return bag.getString(paramKey);
} }
@Override @Override
public String getString(String bagKey, String paramKey) throws StrolchModelException { public String getString(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getString(paramKey); return bag == null ? "" : bag.getString(paramKey);
} }
@Override @Override
public boolean getBoolean(String paramKey) throws StrolchModelException { public boolean getBoolean(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag != null && bag.getBoolean(paramKey);
return false;
return bag.getBoolean(paramKey);
} }
@Override @Override
public boolean getBoolean(String bagKey, String paramKey) throws StrolchModelException { public boolean getBoolean(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getBoolean(paramKey); return bag != null && bag.getBoolean(paramKey);
} }
@Override @Override
public int getInteger(String paramKey) throws StrolchModelException { public int getInteger(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? 0 : bag.getInteger(paramKey);
return 0;
return bag.getInteger(paramKey);
} }
@Override @Override
public int getInteger(String bagKey, String paramKey) throws StrolchModelException { public int getInteger(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getInteger(paramKey); return bag == null ? 0 : bag.getInteger(paramKey);
} }
@Override @Override
public double getDouble(String paramKey) throws StrolchModelException { public double getDouble(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? 0 : bag.getDouble(paramKey);
return 0.0D;
return bag.getDouble(paramKey);
} }
@Override @Override
public double getDouble(String bagKey, String paramKey) throws StrolchModelException { public double getDouble(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getDouble(paramKey); return bag.getDouble(paramKey);
} }
@Override @Override
public long getLong(String paramKey) throws StrolchModelException { public long getLong(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? 0L : bag.getLong(paramKey);
return 0L;
return bag.getLong(paramKey);
} }
@Override @Override
public long getLong(String bagKey, String paramKey) throws StrolchModelException { public long getLong(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getLong(paramKey); return bag == null ? 0L : bag.getLong(paramKey);
} }
@Override @Override
public ZonedDateTime getDate(String paramKey) throws StrolchModelException { public ZonedDateTime getDate(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? ISO8601.EMPTY_VALUE_ZONED_DATE : bag.getDate(paramKey);
return ISO8601.EMPTY_VALUE_ZONED_DATE;
return bag.getDate(paramKey);
} }
@Override @Override
public ZonedDateTime getDate(String bagKey, String paramKey) throws StrolchModelException { public ZonedDateTime getDate(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getDate(paramKey); return bag == null ? ISO8601.EMPTY_VALUE_ZONED_DATE : bag.getDate(paramKey);
} }
@Override @Override
public LocalDateTime getLocalDate(String paramKey) throws StrolchModelException { public LocalDateTime getLocalDate(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? ISO8601.EMPTY_VALUE_LOCAL_DATE : bag.getLocalDate(paramKey);
return ISO8601.EMPTY_VALUE_LOCAL_DATE;
return bag.getLocalDate(paramKey);
} }
@Override @Override
public LocalDateTime getLocalDate(String bagKey, String paramKey) throws StrolchModelException { public LocalDateTime getLocalDate(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getLocalDate(paramKey); return bag == null ? ISO8601.EMPTY_VALUE_LOCAL_DATE : bag.getLocalDate(paramKey);
} }
@Override @Override
public String getText(String paramKey) throws StrolchModelException { public String getText(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? "" : bag.getText(paramKey);
return "";
return bag.getText(paramKey);
} }
@Override @Override
public String getText(String bagKey, String paramKey) throws StrolchModelException { public String getText(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getText(paramKey); return bag == null ? "" : bag.getText(paramKey);
} }
@Override @Override
public PeriodDuration getDuration(String paramKey) throws StrolchModelException { public PeriodDuration getDuration(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? PeriodDuration.ZERO : bag.getDuration(paramKey);
return PeriodDuration.ZERO;
return bag.getDuration(paramKey);
} }
@Override @Override
public PeriodDuration getDuration(String bagKey, String paramKey) throws StrolchModelException { public PeriodDuration getDuration(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getDuration(paramKey); return bag == null ? PeriodDuration.ZERO : bag.getDuration(paramKey);
} }
@Override @Override
public List<String> getStringList(String paramKey) throws StrolchModelException { public List<String> getStringList(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? emptyList() : bag.getStringList(paramKey);
return Collections.emptyList();
return bag.getStringList(paramKey);
} }
@Override @Override
public List<String> getStringList(String bagKey, String paramKey) throws StrolchModelException { public List<String> getStringList(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getStringList(paramKey); return bag == null ? emptyList() : bag.getStringList(paramKey);
} }
@Override @Override
public List<Integer> getIntegerList(String paramKey) throws StrolchModelException { public List<Integer> getIntegerList(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? emptyList() : bag.getIntegerList(paramKey);
return Collections.emptyList();
return bag.getIntegerList(paramKey);
} }
@Override @Override
public List<Integer> getIntegerList(String bagKey, String paramKey) throws StrolchModelException { public List<Integer> getIntegerList(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getIntegerList(paramKey); return bag == null ? emptyList() : bag.getIntegerList(paramKey);
} }
@Override @Override
public List<Double> getDoubleList(String paramKey) throws StrolchModelException { public List<Double> getDoubleList(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? emptyList() : bag.getDoubleList(paramKey);
return Collections.emptyList();
return bag.getDoubleList(paramKey);
} }
@Override @Override
public List<Double> getDoubleList(String bagKey, String paramKey) throws StrolchModelException { public List<Double> getDoubleList(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getDoubleList(paramKey); return bag == null ? emptyList() : bag.getDoubleList(paramKey);
} }
@Override @Override
public List<Long> getLongList(String paramKey) throws StrolchModelException { public List<Long> getLongList(String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(BAG_PARAMETERS, false); ParameterBag bag = getParameterBag(BAG_PARAMETERS, false);
if (bag == null) return bag == null ? emptyList() : bag.getLongList(paramKey);
return Collections.emptyList();
return bag.getLongList(paramKey);
} }
@Override @Override
public List<Long> getLongList(String bagKey, String paramKey) throws StrolchModelException { public List<Long> getLongList(String bagKey, String paramKey) throws StrolchModelException {
ParameterBag bag = getParameterBag(bagKey, true); ParameterBag bag = getParameterBag(bagKey, false);
return bag.getLongList(paramKey); return bag == null ? emptyList() : bag.getLongList(paramKey);
} }
// //
@ -723,6 +698,14 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
return relationsBag.getStringList(paramKey); return relationsBag.getStringList(paramKey);
} }
@Override
public Stream<String> streamRelationIds(String paramKey) throws StrolchModelException {
ParameterBag relationsBag = getParameterBag(BAG_RELATIONS, false);
if (relationsBag == null)
return Stream.empty();
return relationsBag.streamStringList(paramKey);
}
@Override @Override
public void setRelationId(String paramKey, String id) throws StrolchModelException { public void setRelationId(String paramKey, String id) throws StrolchModelException {
getRelationParam(paramKey, true).setValue(id); getRelationParam(paramKey, true).setValue(id);
@ -980,7 +963,7 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
@Override @Override
public Set<String> getParameterBagKeySet() { public Set<String> getParameterBagKeySet() {
if (this.parameterBagMap == null) { if (this.parameterBagMap == null) {
return Collections.emptySet(); return emptySet();
} }
return new HashSet<>(this.parameterBagMap.keySet()); return new HashSet<>(this.parameterBagMap.keySet());
} }