This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new b858655bc1 Utility class modernization
b858655bc1 is described below

commit b858655bc14247858f333a5193410dda1a131c9c
Author: James Bognar <[email protected]>
AuthorDate: Fri Nov 7 13:59:57 2025 -0500

    Utility class modernization
---
 .../juneau/common/reflect/ReflectionMap2.java      | 240 +++++++++++++++++++++
 .../apache/juneau/rest/debug/DebugEnablement.java  |   6 +-
 .../juneau/rest/debug/DebugEnablementMap.java      |   4 +-
 3 files changed, 245 insertions(+), 5 deletions(-)

diff --git 
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ReflectionMap2.java
 
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ReflectionMap2.java
index 42bc351f17..25cc709bed 100644
--- 
a/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ReflectionMap2.java
+++ 
b/juneau-core/juneau-common/src/main/java/org/apache/juneau/common/reflect/ReflectionMap2.java
@@ -509,6 +509,246 @@ public class ReflectionMap2<V> {
                return constructorEntries.stream().filter(x -> 
x.matches(c)).map(x -> x.value);
        }
 
+       /**
+        * Finds first value in this map that matches the specified class.
+        *
+        * @param c The class to test for.
+        * @return The matching object.  Never <jk>null</jk>.
+        */
+       public Optional<V> find(Class<?> c) {
+               return find(c, null);
+       }
+
+       /**
+        * Finds first value in this map that matches the specified class.
+        *
+        * @param c The class to test for.
+        * @param ofType Only return objects of the specified type.
+        * @return The matching object.  Never <jk>null</jk>.
+        */
+       public Optional<V> find(Class<?> c, Class<? extends V> ofType) {
+               return opt(findMatching(c).filter(x -> x != null && (ofType == 
null || ofType.isInstance(x))).findFirst().orElse(null));
+       }
+
+       /**
+        * Finds first value in this map that matches the specified constructor.
+        *
+        * @param c The constructor to test for.
+        * @return The matching object.  Never <jk>null</jk>.
+        */
+       public Optional<V> find(Constructor<?> c) {
+               return find(c, null);
+       }
+
+       /**
+        * Finds first value in this map that matches the specified constructor.
+        *
+        * @param c The constructor to test for.
+        * @param ofType Only return objects of the specified type.
+        * @return The matching object.  Never <jk>null</jk>.
+        */
+       public Optional<V> find(Constructor<?> c, Class<? extends V> ofType) {
+               return opt(findMatching(c).filter(x -> x != null && (ofType == 
null || ofType.isInstance(x))).findFirst().orElse(null));
+       }
+
+       /**
+        * Finds first value in this map that matches the specified field.
+        *
+        * @param f The field to test for.
+        * @return The matching object.  Never <jk>null</jk>.
+        */
+       public Optional<V> find(Field f) {
+               return find(f, null);
+       }
+
+       /**
+        * Finds first value in this map that matches the specified field.
+        *
+        * @param f The field to test for.
+        * @param ofType Only return objects of the specified type.
+        * @return The matching object.  Never <jk>null</jk>.
+        */
+       public Optional<V> find(Field f, Class<? extends V> ofType) {
+               return opt(findMatching(f).filter(x -> x != null && (ofType == 
null || ofType.isInstance(x))).findFirst().orElse(null));
+       }
+
+       /**
+        * Finds first value in this map that matches the specified method.
+        *
+        * @param m The method to test for.
+        * @return The matching object.  Never <jk>null</jk>.
+        */
+       public Optional<V> find(Method m) {
+               return find(m, null);
+       }
+
+       /**
+        * Finds first value in this map that matches the specified method.
+        *
+        * @param m The method to test for.
+        * @param ofType Only return objects of the specified type.
+        * @return The matching object.  Never <jk>null</jk>.
+        */
+       public Optional<V> find(Method m, Class<? extends V> ofType) {
+               return opt(findMatching(m).filter(x -> x != null && (ofType == 
null || ofType.isInstance(x))).findFirst().orElse(null));
+       }
+
+       /**
+        * Finds all values in this map that match the specified class.
+        *
+        * @param c The class to test for.
+        * @return The matching objects.  Never <jk>null</jk>.
+        */
+       public List<V> findAll(Class<?> c) {
+               return findAll(c, null);
+       }
+
+       /**
+        * Finds all values in this map that match the specified class.
+        *
+        * @param c The class to test for.
+        * @param ofType Only return objects of the specified type.
+        * @return The matching objects.  Never <jk>null</jk>.
+        */
+       public List<V> findAll(Class<?> c, Class<? extends V> ofType) {
+               return findMatching(c).filter(x -> ofType == null || 
ofType.isInstance(x)).toList();
+       }
+
+       /**
+        * Finds all values in this map that match the specified constructor.
+        *
+        * @param c The constructor to test for.
+        * @return The matching objects.  Never <jk>null</jk>.
+        */
+       public List<V> findAll(Constructor<?> c) {
+               return findAll(c, null);
+       }
+
+       /**
+        * Finds all values in this map that match the specified constructor.
+        *
+        * @param c The constructor to test for.
+        * @param ofType Only return objects of the specified type.
+        * @return The matching objects.  Never <jk>null</jk>.
+        */
+       public List<V> findAll(Constructor<?> c, Class<? extends V> ofType) {
+               return findMatching(c).filter(x -> ofType == null || 
ofType.isInstance(x)).toList();
+       }
+
+       /**
+        * Finds all values in this map that match the specified field.
+        *
+        * @param f The field to test for.
+        * @return The matching objects.  Never <jk>null</jk>.
+        */
+       public List<V> findAll(Field f) {
+               return findAll(f, null);
+       }
+
+       /**
+        * Finds all values in this map that match the specified field.
+        *
+        * @param f The field to test for.
+        * @param ofType Only return objects of the specified type.
+        * @return The matching objects.  Never <jk>null</jk>.
+        */
+       public List<V> findAll(Field f, Class<? extends V> ofType) {
+               return findMatching(f).filter(x -> ofType == null || 
ofType.isInstance(x)).toList();
+       }
+
+       /**
+        * Finds all values in this map that match the specified method.
+        *
+        * @param m The method to test for.
+        * @return The matching objects.  Never <jk>null</jk>.
+        */
+       public List<V> findAll(Method m) {
+               return findAll(m, null);
+       }
+
+       /**
+        * Finds all values in this map that match the specified method.
+        *
+        * @param m The method to test for.
+        * @param ofType Only return objects of the specified type.
+        * @return The matching objects.  Never <jk>null</jk>.
+        */
+       public List<V> findAll(Method m, Class<? extends V> ofType) {
+               return findMatching(m).filter(x -> ofType == null || 
ofType.isInstance(x)).toList();
+       }
+
+       /**
+        * Finds all values in this map that match the specified class and 
appends them to the specified array.
+        *
+        * @param c The class to test for.
+        * @param ofType Only return objects of the specified type.
+        * @param array The array to append to.
+        * @return The array with matching objects appended.
+        */
+       public V[] appendAll(Class<?> c, Class<? extends V> ofType, V[] array) {
+               var list = findAll(c, ofType);
+               if (list.isEmpty())
+                       return array;
+               var newArray = Arrays.copyOf(array, array.length + list.size());
+               for (int i = 0; i < list.size(); i++)
+                       newArray[array.length + i] = list.get(i);
+               return newArray;
+       }
+
+       /**
+        * Finds all values in this map that match the specified constructor 
and appends them to the specified array.
+        *
+        * @param c The constructor to test for.
+        * @param ofType Only return objects of the specified type.
+        * @param array The array to append to.
+        * @return The array with matching objects appended.
+        */
+       public V[] appendAll(Constructor<?> c, Class<? extends V> ofType, V[] 
array) {
+               var list = findAll(c, ofType);
+               if (list.isEmpty())
+                       return array;
+               var newArray = Arrays.copyOf(array, array.length + list.size());
+               for (int i = 0; i < list.size(); i++)
+                       newArray[array.length + i] = list.get(i);
+               return newArray;
+       }
+
+       /**
+        * Finds all values in this map that match the specified field and 
appends them to the specified array.
+        *
+        * @param f The field to test for.
+        * @param ofType Only return objects of the specified type.
+        * @param array The array to append to.
+        * @return The array with matching objects appended.
+        */
+       public V[] appendAll(Field f, Class<? extends V> ofType, V[] array) {
+               var list = findAll(f, ofType);
+               if (list.isEmpty())
+                       return array;
+               var newArray = Arrays.copyOf(array, array.length + list.size());
+               for (int i = 0; i < list.size(); i++)
+                       newArray[array.length + i] = list.get(i);
+               return newArray;
+       }
+
+       /**
+        * Finds all values in this map that match the specified method and 
appends them to the specified array.
+        *
+        * @param m The method to test for.
+        * @param ofType Only return objects of the specified type.
+        * @param array The array to append to.
+        * @return The array with matching objects appended.
+        */
+       public V[] appendAll(Method m, Class<? extends V> ofType, V[] array) {
+               var list = findAll(m, ofType);
+               if (list.isEmpty())
+                       return array;
+               var newArray = Arrays.copyOf(array, array.length + list.size());
+               for (int i = 0; i < list.size(); i++)
+                       newArray[array.length + i] = list.get(i);
+               return newArray;
+       }
+
        @Override /* Overridden from Object */
        public String toString() {
                // @formatter:off
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/debug/DebugEnablement.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/debug/DebugEnablement.java
index 99c33132c7..ddaa303113 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/debug/DebugEnablement.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/debug/DebugEnablement.java
@@ -45,7 +45,7 @@ public abstract class DebugEnablement {
         */
        public static class Builder {
 
-               ReflectionMap.Builder<Enablement> mapBuilder;
+               ReflectionMap2.Builder<Enablement> mapBuilder;
                Enablement defaultEnablement = NEVER;
                Predicate<HttpServletRequest> conditional;
                BeanCreator<DebugEnablement> creator;
@@ -56,7 +56,7 @@ public abstract class DebugEnablement {
                 * @param beanStore The bean store to use for creating beans.
                 */
                protected Builder(BeanStore beanStore) {
-                       mapBuilder = ReflectionMap.create(Enablement.class);
+                       mapBuilder = ReflectionMap2.create(Enablement.class);
                        defaultEnablement = NEVER;
                        conditional = x -> 
"true".equalsIgnoreCase(x.getHeader("Debug"));
                        creator = 
beanStore.createBean(DebugEnablement.class).type(BasicDebugEnablement.class).builder(Builder.class,
 this);
@@ -211,7 +211,7 @@ public abstract class DebugEnablement {
        }
 
        private final Enablement defaultEnablement;
-       private final ReflectionMap<Enablement> enablementMap;
+       private final ReflectionMap2<Enablement> enablementMap;
        private final Predicate<HttpServletRequest> conditionalPredicate;
 
        /**
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/debug/DebugEnablementMap.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/debug/DebugEnablementMap.java
index 2eb384c6ce..2672a5c4ef 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/debug/DebugEnablementMap.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/debug/DebugEnablementMap.java
@@ -26,11 +26,11 @@ import org.apache.juneau.common.reflect.*;
  *     <li class='link'><a class="doclink" 
href="https://juneau.apache.org/docs/topics/RestServerLoggingAndDebugging";>Logging
 / Debugging</a>
  * </ul>
  */
-public class DebugEnablementMap extends ReflectionMap<Enablement> {
+public class DebugEnablementMap extends ReflectionMap2<Enablement> {
        /**
         * Builder class.
         */
-       public static class Builder extends ReflectionMap.Builder<Enablement> {
+       public static class Builder extends ReflectionMap2.Builder<Enablement> {
 
                /**
                 * Constructor.

Reply via email to