Definitions.java

  1. package org.opentrafficsim.core.definitions;

  2. import java.util.Collections;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;

  5. import org.djutils.exceptions.Throw;
  6. import org.djutils.immutablecollections.Immutable;
  7. import org.djutils.immutablecollections.ImmutableHashMap;
  8. import org.djutils.immutablecollections.ImmutableMap;
  9. import org.opentrafficsim.base.HierarchicalType;

  10. /**
  11.  * The Definitions interface contains access to the core definitions that can be used to interpret the Network and the
  12.  * PerceivableContext. Example interfaces allow the retrieval of GtuTypes and LinkTypes.
  13.  * <p>
  14.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  16.  * </p>
  17.  * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
  18.  */
  19. public class Definitions
  20. {

  21.     /** Map of maps of types per id. */
  22.     private Map<Class<? extends HierarchicalType<?, ?>>, Map<String, HierarchicalType<?, ?>>> typeMap = new LinkedHashMap<>();

  23.     /**
  24.      * Add a type (e.g. a GtuType instance).
  25.      * @param <T> type type (e.g. GtuType).
  26.      * @param typeClass Class&lt;T&gt;; class of type (e.g. GtuType.class).
  27.      * @param t T; type instance (e.g DefaultsNl.CAR).
  28.      */
  29.     public <T extends HierarchicalType<T, ?>> void add(final Class<T> typeClass, final T t)
  30.     {
  31.         Throw.whenNull(typeClass, "Type class may not be null.");
  32.         Throw.whenNull(t, "Type may not be null.");
  33.         this.typeMap.computeIfAbsent(typeClass, (key) -> Collections.synchronizedMap(new LinkedHashMap<>())).put(t.getId(), t);
  34.     }

  35.     /**
  36.      * Obtain a type by its id. Returns {@code null} if it is not present.
  37.      * @param <T> type type (e.g. GtuType).
  38.      * @param typeClass typeClass Class&lt;T&gt;; class of type (e.g. GtuType.class).
  39.      * @param id String; id of the class.
  40.      * @return T; instance with given id, or {@code null} if it is not present.
  41.      */
  42.     @SuppressWarnings("unchecked")
  43.     public <T extends HierarchicalType<T, ?>> T get(final Class<T> typeClass, final String id)
  44.     {
  45.         Throw.whenNull(typeClass, "Type class may not be null.");
  46.         Throw.whenNull(id, "Id may not be null.");
  47.         return (T) this.typeMap.computeIfAbsent(typeClass, (key) -> Collections.synchronizedMap(new LinkedHashMap<>())).get(id);
  48.     }

  49.     /**
  50.      * Obtain all present type of given type type.
  51.      * @param <T> type type (e.g. GtuType).
  52.      * @param typeClass typeClass Class&lt;T&gt;; class of type (e.g. GtuType.class).
  53.      * @return ImmutableMap&lt;String, T&gt;; map of all types of give type type, empty if there are no types of the type type.
  54.      */
  55.     @SuppressWarnings("unchecked")
  56.     public <T extends HierarchicalType<T, ?>> ImmutableMap<String, T> getAll(final Class<T> typeClass)
  57.     {
  58.         Throw.whenNull(typeClass, "Type class may not be null.");
  59.         return new ImmutableHashMap<>((Map<String, T>) this.typeMap.computeIfAbsent(typeClass,
  60.                 (key) -> Collections.synchronizedMap(new LinkedHashMap<>())), Immutable.WRAP);
  61.     }

  62. }