You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "essobedo (via GitHub)" <gi...@apache.org> on 2023/05/03 06:53:33 UTC

[GitHub] [camel] essobedo commented on a diff in pull request #9982: CAMEL-19058: added a new wrapper for the enum array pattern used throughout the code base

essobedo commented on code in PR #9982:
URL: https://github.com/apache/camel/pull/9982#discussion_r1183290909


##########
core/camel-support/src/main/java/org/apache/camel/support/EnumArray.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.support;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * This class provides an array-based structure used to store payloads associated with enums. This is
+ * used in the hot path of the core code to allow access to those payloads with constant time and low
+ * memory overhead.
+ *
+ * This data-structure is meant for internal usage of the Camel Core and is not meant for users.
+ * @param <T>
+ */
+public final class EnumArray<T extends Enum<?>> {
+    private final Object[] internalArray;
+    private final T[] values;
+
+    /**
+     * Creates a new EnumArray with a fixed size determined by the length of values
+     * @param values the Enum values (as in Enum.values())
+     */
+    public EnumArray(T[] values) {

Review Comment:
   ```suggestion
       public EnumArray(Class<T> keyType) {
   ```



##########
core/camel-support/src/main/java/org/apache/camel/support/EnumArray.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.support;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * This class provides an array-based structure used to store payloads associated with enums. This is
+ * used in the hot path of the core code to allow access to those payloads with constant time and low
+ * memory overhead.
+ *
+ * This data-structure is meant for internal usage of the Camel Core and is not meant for users.
+ * @param <T>
+ */
+public final class EnumArray<T extends Enum<?>> {

Review Comment:
   ```suggestion
   public final class EnumArray<T extends Enum<T>> {
   ```



##########
core/camel-support/src/main/java/org/apache/camel/support/EnumArray.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.support;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * This class provides an array-based structure used to store payloads associated with enums. This is
+ * used in the hot path of the core code to allow access to those payloads with constant time and low
+ * memory overhead.
+ *
+ * This data-structure is meant for internal usage of the Camel Core and is not meant for users.
+ * @param <T>
+ */
+public final class EnumArray<T extends Enum<?>> {
+    private final Object[] internalArray;
+    private final T[] values;
+
+    /**
+     * Creates a new EnumArray with a fixed size determined by the length of values
+     * @param values the Enum values (as in Enum.values())
+     */
+    public EnumArray(T[] values) {
+        this.internalArray = new Object[values.length];
+        this.values = values;

Review Comment:
   ```suggestion
           this.values = keyType.getEnumConstants();
           this.internalArray = new Object[values.length];
   ```



##########
core/camel-support/src/main/java/org/apache/camel/support/EnumArray.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.support;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * This class provides an array-based structure used to store payloads associated with enums. This is
+ * used in the hot path of the core code to allow access to those payloads with constant time and low
+ * memory overhead.
+ *
+ * This data-structure is meant for internal usage of the Camel Core and is not meant for users.
+ * @param <T>
+ */
+public final class EnumArray<T extends Enum<?>> {
+    private final Object[] internalArray;
+    private final T[] values;
+
+    /**
+     * Creates a new EnumArray with a fixed size determined by the length of values
+     * @param values the Enum values (as in Enum.values())
+     */
+    public EnumArray(T[] values) {
+        this.internalArray = new Object[values.length];
+        this.values = values;
+    }
+
+    /**
+     * Whether this arrau contains a payload (value) for the given entry
+     * @param entry the entry to check for
+     * @return true if there is a payload or false otherwise
+     */
+    public boolean contains(T entry) {
+        Object payload = internalArray[entry.ordinal()];
+
+        return payload != null;
+    }
+
+    /**
+     * Gets the payload for the given entry
+     * @param entry the entry to get the payload (must not be null)
+     * @return the payload or false otherwise
+     */
+    public Object get(T entry) {
+        Objects.requireNonNull(entry, "The entry for a enum array cannot be null");
+
+        return internalArray[entry.ordinal()];
+    }
+
+    /**
+     * Gets the payload for the given entry
+     * @param entry the entry to get the payload (must not be null)
+     * @param clazz the class to cast the payload to
+     * @return the payload or false otherwise
+     * @param <K> the payload type
+     */
+    public <K> K get(T entry, Class<K> clazz) {
+        Object tmp = get(entry);
+
+        if (tmp != null) {
+            return clazz.cast(tmp);
+        }
+
+        return null;
+    }
+
+    /**
+     * Computes a new value for the entry if it's present on this instance
+     * @param entry the entry to compute the value for
+     * @param mappingFunction the mapping function that will provide the new value. It takes as a parameter the old value.
+     * @return the new value that was computed for this entry
+     */
+    public Object computeIfPresent(T entry, Function<Object, Object> mappingFunction) {
+        Object payload = get(entry);
+
+        if (payload != null) {
+            payload = mappingFunction.apply(payload);
+
+            set(entry, payload);
+        }
+
+        return payload;
+    }
+
+    /**
+     * Sets the payload for the given entry
+     * @param entry the entry to set the payload
+     * @param payload the payload
+     */
+    public void set(T entry, Object payload) {
+        internalArray[entry.ordinal()] = payload;

Review Comment:
   ```suggestion
           internalArray[Objects.requireNonNull(entry, "The entry for a enum array cannot be null").ordinal()] = payload;
   ```
   To be homogenous, we should check if it is null here too



##########
core/camel-support/src/main/java/org/apache/camel/support/EnumArray.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.support;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * This class provides an array-based structure used to store payloads associated with enums. This is
+ * used in the hot path of the core code to allow access to those payloads with constant time and low
+ * memory overhead.
+ *
+ * This data-structure is meant for internal usage of the Camel Core and is not meant for users.
+ * @param <T>
+ */
+public final class EnumArray<T extends Enum<?>> {
+    private final Object[] internalArray;
+    private final T[] values;
+
+    /**
+     * Creates a new EnumArray with a fixed size determined by the length of values
+     * @param values the Enum values (as in Enum.values())
+     */
+    public EnumArray(T[] values) {
+        this.internalArray = new Object[values.length];
+        this.values = values;
+    }
+
+    /**
+     * Whether this arrau contains a payload (value) for the given entry

Review Comment:
   ```suggestion
        * Whether this array contains a payload (value) for the given entry
   ```



##########
core/camel-support/src/main/java/org/apache/camel/support/EnumArray.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.support;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * This class provides an array-based structure used to store payloads associated with enums. This is
+ * used in the hot path of the core code to allow access to those payloads with constant time and low
+ * memory overhead.
+ *
+ * This data-structure is meant for internal usage of the Camel Core and is not meant for users.
+ * @param <T>
+ */
+public final class EnumArray<T extends Enum<?>> {
+    private final Object[] internalArray;
+    private final T[] values;
+
+    /**
+     * Creates a new EnumArray with a fixed size determined by the length of values
+     * @param values the Enum values (as in Enum.values())
+     */
+    public EnumArray(T[] values) {
+        this.internalArray = new Object[values.length];
+        this.values = values;
+    }
+
+    /**
+     * Whether this arrau contains a payload (value) for the given entry
+     * @param entry the entry to check for
+     * @return true if there is a payload or false otherwise
+     */
+    public boolean contains(T entry) {
+        Object payload = internalArray[entry.ordinal()];
+
+        return payload != null;
+    }
+
+    /**
+     * Gets the payload for the given entry
+     * @param entry the entry to get the payload (must not be null)
+     * @return the payload or false otherwise
+     */
+    public Object get(T entry) {
+        Objects.requireNonNull(entry, "The entry for a enum array cannot be null");
+
+        return internalArray[entry.ordinal()];
+    }
+
+    /**
+     * Gets the payload for the given entry
+     * @param entry the entry to get the payload (must not be null)
+     * @param clazz the class to cast the payload to
+     * @return the payload or false otherwise
+     * @param <K> the payload type
+     */
+    public <K> K get(T entry, Class<K> clazz) {
+        Object tmp = get(entry);
+
+        if (tmp != null) {
+            return clazz.cast(tmp);
+        }
+
+        return null;

Review Comment:
   ```suggestion
           return clazz.cast(get(entry));
   ```
   ```



##########
core/camel-support/src/main/java/org/apache/camel/support/EnumArray.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.support;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * This class provides an array-based structure used to store payloads associated with enums. This is
+ * used in the hot path of the core code to allow access to those payloads with constant time and low
+ * memory overhead.
+ *
+ * This data-structure is meant for internal usage of the Camel Core and is not meant for users.
+ * @param <T>
+ */
+public final class EnumArray<T extends Enum<?>> {
+    private final Object[] internalArray;
+    private final T[] values;
+
+    /**
+     * Creates a new EnumArray with a fixed size determined by the length of values
+     * @param values the Enum values (as in Enum.values())
+     */
+    public EnumArray(T[] values) {
+        this.internalArray = new Object[values.length];
+        this.values = values;
+    }
+
+    /**
+     * Whether this arrau contains a payload (value) for the given entry
+     * @param entry the entry to check for
+     * @return true if there is a payload or false otherwise
+     */
+    public boolean contains(T entry) {
+        Object payload = internalArray[entry.ordinal()];
+
+        return payload != null;
+    }
+
+    /**
+     * Gets the payload for the given entry
+     * @param entry the entry to get the payload (must not be null)
+     * @return the payload or false otherwise
+     */
+    public Object get(T entry) {
+        Objects.requireNonNull(entry, "The entry for a enum array cannot be null");
+
+        return internalArray[entry.ordinal()];

Review Comment:
   ```suggestion
           return internalArray[Objects.requireNonNull(entry, "The entry for a enum array cannot be null").ordinal()];
   ```



##########
core/camel-support/src/main/java/org/apache/camel/support/EnumArray.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.support;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * This class provides an array-based structure used to store payloads associated with enums. This is
+ * used in the hot path of the core code to allow access to those payloads with constant time and low
+ * memory overhead.
+ *
+ * This data-structure is meant for internal usage of the Camel Core and is not meant for users.
+ * @param <T>
+ */
+public final class EnumArray<T extends Enum<?>> {
+    private final Object[] internalArray;
+    private final T[] values;
+
+    /**
+     * Creates a new EnumArray with a fixed size determined by the length of values
+     * @param values the Enum values (as in Enum.values())
+     */
+    public EnumArray(T[] values) {
+        this.internalArray = new Object[values.length];
+        this.values = values;
+    }
+
+    /**
+     * Whether this arrau contains a payload (value) for the given entry
+     * @param entry the entry to check for
+     * @return true if there is a payload or false otherwise
+     */
+    public boolean contains(T entry) {
+        Object payload = internalArray[entry.ordinal()];
+
+        return payload != null;

Review Comment:
   ```suggestion
           return internalArray[entry.ordinal()] != null;
   ```
   
   Let's inline to avoid adding a local variable



##########
core/camel-support/src/main/java/org/apache/camel/support/EnumArray.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.support;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * This class provides an array-based structure used to store payloads associated with enums. This is
+ * used in the hot path of the core code to allow access to those payloads with constant time and low
+ * memory overhead.
+ *
+ * This data-structure is meant for internal usage of the Camel Core and is not meant for users.
+ * @param <T>
+ */
+public final class EnumArray<T extends Enum<?>> {

Review Comment:
   Why not use `EnumMap` instead?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org