ParseUtil.java

  1. package org.opentrafficsim.road.network.factory.xml.utils;

  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.function.Predicate;

  5. /**
  6.  * Parser - Utility class for parsing using JAXB generated classes. <br>
  7.  * <br>
  8.  * Copyright (c) 2003-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
  9.  * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
  10.  * source code and binary code of this software is proprietary information of Delft University of Technology.
  11.  * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
  12.  */
  13. public final class ParseUtil
  14. {
  15.     /** */
  16.     private ParseUtil()
  17.     {
  18.         // utility class
  19.     }

  20.     /**
  21.      * Returns all objects of given type from the list of all objects.
  22.      * @param objectList List&lt;?&gt;; list of objects
  23.      * @param clazz Class&lt;T&gt;; class of type of objects to return
  24.      * @param <T> type
  25.      * @return list of all objects of given type from the list of all objects
  26.      */
  27.     @SuppressWarnings("unchecked")
  28.     public static <T> List<T> getObjectsOfType(final List<?> objectList, final Class<T> clazz)
  29.     {
  30.         List<T> list = new ArrayList<>();
  31.         for (Object object : objectList)
  32.         {
  33.             if (clazz.isAssignableFrom(object.getClass()))
  34.             {
  35.                 list.add((T) object);
  36.             }
  37.         }
  38.         return list;
  39.     }

  40.     /**
  41.      * Select object of given type by predicate.
  42.      * @param objectList List&lt;?&gt;; list of objects
  43.      * @param clazz Class&lt;T&gt;; class of type of objects to return
  44.      * @param predicate Predicate&lt;T&gt;; predicate
  45.      * @param <T> type
  46.      * @return (first) object of given type that matches the predicate
  47.      */
  48.     public static <T> T findObject(final List<?> objectList, final Class<T> clazz, final Predicate<T> predicate)
  49.     {
  50.         for (Object object : objectList)
  51.         {
  52.             if (clazz.isAssignableFrom(object.getClass()))
  53.             {
  54.                 @SuppressWarnings("unchecked")
  55.                 T t = (T) object;
  56.                 if (predicate.test(t))
  57.                 {
  58.                     return t;
  59.                 }
  60.             }
  61.         }
  62.         throw new RuntimeException(String.format("Object of type %s could not be found.", clazz));
  63.     }

  64.     /**
  65.      * Select object of given type by predicate.
  66.      * @param objectList List&lt;T&gt;; list of objects
  67.      * @param predicate Predicate&lt;T&gt;; predicate
  68.      * @param <T> type
  69.      * @return (first) object of given type that matches the predicate
  70.      */
  71.     public static <T> T findObject(final List<T> objectList, final Predicate<T> predicate)
  72.     {
  73.         for (T object : objectList)
  74.         {
  75.             if (predicate.test(object))
  76.             {
  77.                 return object;
  78.             }
  79.         }
  80.         throw new RuntimeException("Object with predicate not be found.");
  81.     }
  82. }