This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 552c46d50f0 (chores) camel-support: fix code duplications in the
AbstractExchange
552c46d50f0 is described below
commit 552c46d50f0c4532d0ef9c829fbb81d5fbd55c9d
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Wed Jun 7 12:42:38 2023 +0200
(chores) camel-support: fix code duplications in the AbstractExchange
---
.../org/apache/camel/support/AbstractExchange.java | 60 ++++++++--------------
1 file changed, 20 insertions(+), 40 deletions(-)
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java
b/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java
index 52c6ea7a534..0bf4d95f735 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java
@@ -200,8 +200,13 @@ class AbstractExchange implements Exchange {
@Override
public <T> T getProperty(ExchangePropertyKey key, Class<T> type) {
Object value = getProperty(key);
+ return evalPropertyValue(type, value);
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> T evalPropertyValue(final Class<T> type, final Object value) {
if (value == null) {
- // lets avoid NullPointerException when converting to boolean for
null values
+ // let's avoid NullPointerException when converting to boolean for
null values
if (boolean.class == type) {
return (T) Boolean.FALSE;
}
@@ -209,7 +214,7 @@ class AbstractExchange implements Exchange {
}
// eager same instance type test to avoid the overhead of invoking the
type converter
- // if already same type
+ // if already is the same type
if (type.isInstance(value)) {
return (T) value;
}
@@ -217,14 +222,14 @@ class AbstractExchange implements Exchange {
return ExchangeHelper.convertToType(this, type, value);
}
- @Override
- public <T> T getProperty(ExchangePropertyKey key, Object defaultValue,
Class<T> type) {
- Object value = getProperty(key);
+ // TODO: fix re-assignment of the value instance here.
+ @SuppressWarnings("unchecked")
+ private <T> T evalPropertyValue(final Object defaultValue, final Class<T>
type, Object value) {
if (value == null) {
value = defaultValue;
}
if (value == null) {
- // lets avoid NullPointerException when converting to boolean for
null values
+ // let's avoid NullPointerException when converting to boolean for
null values
if (boolean.class == type) {
return (T) Boolean.FALSE;
}
@@ -232,7 +237,7 @@ class AbstractExchange implements Exchange {
}
// eager same instance type test to avoid the overhead of invoking the
type converter
- // if already same type
+ // if already is the same type
if (type.isInstance(value)) {
return (T) value;
}
@@ -240,6 +245,12 @@ class AbstractExchange implements Exchange {
return ExchangeHelper.convertToType(this, type, value);
}
+ @Override
+ public <T> T getProperty(ExchangePropertyKey key, Object defaultValue,
Class<T> type) {
+ Object value = getProperty(key);
+ return evalPropertyValue(defaultValue, type, value);
+ }
+
@Override
public void setProperty(ExchangePropertyKey key, Object value) {
internalProperties.put(key, value);
@@ -268,45 +279,14 @@ class AbstractExchange implements Exchange {
@SuppressWarnings("unchecked")
public <T> T getProperty(String name, Class<T> type) {
Object value = getProperty(name);
- if (value == null) {
- // lets avoid NullPointerException when converting to boolean for
null values
- if (boolean.class == type) {
- return (T) Boolean.FALSE;
- }
- return null;
- }
-
- // eager same instance type test to avoid the overhead of invoking the
type converter
- // if already same type
- if (type.isInstance(value)) {
- return (T) value;
- }
-
- return ExchangeHelper.convertToType(this, type, value);
+ return evalPropertyValue(type, value);
}
@Override
@SuppressWarnings("unchecked")
public <T> T getProperty(String name, Object defaultValue, Class<T> type) {
Object value = getProperty(name);
- if (value == null) {
- value = defaultValue;
- }
- if (value == null) {
- // lets avoid NullPointerException when converting to boolean for
null values
- if (boolean.class == type) {
- return (T) Boolean.FALSE;
- }
- return null;
- }
-
- // eager same instance type test to avoid the overhead of invoking the
type converter
- // if already same type
- if (type.isInstance(value)) {
- return (T) value;
- }
-
- return ExchangeHelper.convertToType(this, type, value);
+ return evalPropertyValue(defaultValue, type, value);
}
@Override