Skip to content

Commit

Permalink
NO-JIRA Save double property lookup on common case
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 authored and clebertsuconic committed Oct 11, 2021
1 parent 377cda6 commit 95d2de5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Expand Up @@ -23,10 +23,12 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

import io.netty.buffer.ByteBuf;
import org.apache.activemq.artemis.api.core.ActiveMQPropertyConversionException;
Expand Down Expand Up @@ -169,10 +171,12 @@ public Boolean getBooleanProperty(final SimpleString key) throws ActiveMQPropert
throw new ActiveMQPropertyConversionException("Invalid conversion: " + key);
}

public Byte getByteProperty(final SimpleString key) throws ActiveMQPropertyConversionException {
public Byte getByteProperty(final SimpleString key,
final Supplier<Byte> defaultValue) throws ActiveMQPropertyConversionException {
Objects.requireNonNull(defaultValue);
Object value = doGetProperty(key);
if (value == null) {
return Byte.valueOf(null);
return defaultValue.get();
} else if (value instanceof Byte) {
return (Byte) value;
} else if (value instanceof SimpleString) {
Expand All @@ -181,6 +185,10 @@ public Byte getByteProperty(final SimpleString key) throws ActiveMQPropertyConve
throw new ActiveMQPropertyConversionException("Invalid conversion: " + key);
}

public Byte getByteProperty(final SimpleString key) throws ActiveMQPropertyConversionException {
return getByteProperty(key, () -> Byte.valueOf(null));
}

public Character getCharProperty(final SimpleString key) throws ActiveMQPropertyConversionException {
Object value = doGetProperty(key);
if (value == null) {
Expand Down
Expand Up @@ -115,6 +115,18 @@ public void testByteProperty() throws Exception {
}
}

@Test
public void testNoByteProperty() {
Assert.assertEquals(0, props.size());
Assert.assertNull(props.getByteProperty(key, () -> null));
props.putByteProperty(key.concat('0'), RandomUtil.randomByte());
Assert.assertEquals(1, props.size());
Assert.assertNull(props.getByteProperty(key, () -> null));
props.putNullValue(key);
Assert.assertTrue(props.containsProperty(key));
Assert.assertNull(props.getByteProperty(key, () -> null));
}

@Test
public void testIntProperty() throws Exception {
Integer val = RandomUtil.randomInt();
Expand Down
Expand Up @@ -154,10 +154,11 @@ public SimpleString getReplyTo() {

@Override
public RoutingType getRoutingType() {
if (containsProperty(Message.HDR_ROUTING_TYPE)) {
return RoutingType.getType(getByteProperty(Message.HDR_ROUTING_TYPE));
final Byte maybeByte = getProperties().getByteProperty(Message.HDR_ROUTING_TYPE, () -> null);
if (maybeByte == null) {
return null;
}
return null;
return RoutingType.getType(maybeByte);
}

@Override
Expand Down

0 comments on commit 95d2de5

Please sign in to comment.