Skip to content

Commit

Permalink
Reduce Optional allocs with attribute methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Oct 14, 2024
1 parent 85b42fa commit 9599b1c
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,19 @@ public Optional<ValuedAttribute> getAttribute(Attribute attribute) {
}

public void setAttribute(Attribute attribute, double value) {
ValuedAttribute property = getAttribute(attribute).orElse(null);
if (property == null) throw new IllegalArgumentException("Cannot set attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!");
ValuedAttribute property = attributeMap.get(attribute);
if (property == null) {
throw new IllegalArgumentException("Cannot set attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!");
}
property.override(value);
}

public double getAttributeValue(Attribute attribute) {
return getAttribute(attribute).map(ValuedAttribute::get)
.orElseThrow(() -> new IllegalArgumentException("Cannot get attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!"));
final ValuedAttribute property = attributeMap.get(attribute);
if (property == null) {
throw new IllegalArgumentException("Cannot get attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!");
}
return property.get();
}

public void resetAttributes() {
Expand Down

0 comments on commit 9599b1c

Please sign in to comment.