Categorization.java

  1. package org.opentrafficsim.road.od;

  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.List;

  5. import org.djutils.base.Identifiable;
  6. import org.djutils.exceptions.Throw;

  7. /**
  8.  * A categorization determines for what part of traffic certain demand data is applicable. By default, this is always for a
  9.  * given origin-destination pair and time period. A categorization adds to this additional segregation. For example, per lane,
  10.  * per vehicle class, etc.
  11.  * <p>
  12.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  13.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  14.  * </p>
  15.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  16.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  17.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  18.  */
  19. public class Categorization implements Serializable, Identifiable
  20. {

  21.     /** Empty categorization. */
  22.     public static final Categorization UNCATEGORIZED = new Categorization("Uncategorized");

  23.     /** */
  24.     private static final long serialVersionUID = 20160921L;

  25.     /** Id. */
  26.     private final String id;

  27.     /** Set of categorization classes. */
  28.     private final List<Class<?>> classes = new ArrayList<>();

  29.     /**
  30.      * @param id String; id
  31.      */
  32.     private Categorization(final String id)
  33.     {
  34.         Throw.whenNull(id, "Id may not be null.");
  35.         this.id = id;
  36.     }

  37.     /**
  38.      * @param id String; Id
  39.      * @param class1 Class&lt;?&gt;; 1st class
  40.      * @param classes Class&lt;?&gt;...; other classes
  41.      * @throws IllegalArgumentException if any class is given multiple times
  42.      * @throws NullPointerException if any input is null
  43.      */
  44.     public Categorization(final String id, final Class<?> class1, final Class<?>... classes)
  45.     {
  46.         this(id);
  47.         Throw.whenNull(class1, "Classes may not be null.");
  48.         this.classes.add(class1);
  49.         for (Class<?> clazz : classes)
  50.         {
  51.             Throw.whenNull(clazz, "Classes may not be null.");
  52.             Throw.when(this.classes.contains(clazz), IllegalArgumentException.class, "Class %s is given multiple times.",
  53.                     clazz);
  54.             this.classes.add(clazz);
  55.         }
  56.     }

  57.     /**
  58.      * Returns the number of category classes defined.
  59.      * @return number of category classes defined
  60.      */
  61.     public final int size()
  62.     {
  63.         return this.classes.size();
  64.     }

  65.     /**
  66.      * Returns the i'th class.
  67.      * @param i int; index of the class
  68.      * @return the i'th class
  69.      * @throws IndexOutOfBoundsException if index i is out of bounds
  70.      */
  71.     public final Class<?> get(final int i)
  72.     {
  73.         Throw.when(i < 0 || i >= size(), IndexOutOfBoundsException.class,
  74.                 "Index %d is out of range for categorization of size %d.", i, size());
  75.         return this.classes.get(i);
  76.     }

  77.     /**
  78.      * @return id.
  79.      */
  80.     @Override
  81.     public final String getId()
  82.     {
  83.         return this.id;
  84.     }

  85.     /**
  86.      * Returns whether the categorization contains a class that is, or is a sub type of, the given class.
  87.      * @param clazz Class&lt;?&gt;; class to check
  88.      * @return whether the categorization contains a class that is, or is a sub type of, the given class
  89.      */
  90.     public final boolean entails(final Class<?> clazz)
  91.     {
  92.         for (Class<?> clazz2 : this.classes)
  93.         {
  94.             if (clazz.isAssignableFrom(clazz2))
  95.             {
  96.                 return true;
  97.             }
  98.         }
  99.         return false;
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     public final int hashCode()
  104.     {
  105.         final int prime = 31;
  106.         int result = 1;
  107.         result = prime * result + ((this.classes == null) ? 0 : this.classes.hashCode());
  108.         return result;
  109.     }

  110.     /** {@inheritDoc} */
  111.     @Override
  112.     public final boolean equals(final Object obj)
  113.     {
  114.         if (this == obj)
  115.         {
  116.             return true;
  117.         }
  118.         if (obj == null)
  119.         {
  120.             return false;
  121.         }
  122.         if (getClass() != obj.getClass())
  123.         {
  124.             return false;
  125.         }
  126.         Categorization other = (Categorization) obj;
  127.         if (this.classes == null)
  128.         {
  129.             if (other.classes != null)
  130.             {
  131.                 return false;
  132.             }
  133.         }
  134.         else if (!this.classes.equals(other.classes))
  135.         {
  136.             return false;
  137.         }
  138.         return true;
  139.     }

  140.     /** {@inheritDoc} */
  141.     @Override
  142.     public final String toString()
  143.     {
  144.         return "Categorization [id=" + this.id + ", classes=" + this.classes + "]";
  145.     }

  146. }