Category.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.exceptions.Throw;

  6. /**
  7.  * A category is a set of objects who's class belongs to a certain categorization. One {@code Category} object can specify to
  8.  * which subset of traffic between on origin and destination certain demand data belongs.
  9.  * <p>
  10.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  11.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  12.  * </p>
  13.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  14.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  15.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  16.  */
  17. public class Category implements Serializable
  18. {

  19.     /** Empty category. */
  20.     public static final Category UNCATEGORIZED = new Category(Categorization.UNCATEGORIZED);

  21.     /** */
  22.     private static final long serialVersionUID = 20160921L;

  23.     /** Categorization. */
  24.     private final Categorization categorization;

  25.     /** List of objects defining the category. */
  26.     private final List<Object> objects = new ArrayList<>();

  27.     /**
  28.      * @param categorization Categorization; categorization
  29.      */
  30.     private Category(final Categorization categorization)
  31.     {
  32.         Throw.whenNull(categorization, "Categorization may not be null.");
  33.         this.categorization = categorization;
  34.     }

  35.     /**
  36.      * @param categorization Categorization; categorization
  37.      * @param object1 Object; 1st object
  38.      * @param objects Object...; other objects
  39.      * @throws IllegalArgumentException if the objects do not comply with the categorization
  40.      * @throws NullPointerException if any input is null
  41.      */
  42.     public Category(final Categorization categorization, final Object object1, final Object... objects)
  43.     {
  44.         this(categorization);
  45.         Throw.when(categorization.size() != objects.length + 1, IllegalArgumentException.class,
  46.                 "Objects do not comply with the categorization; bad number of objects.");
  47.         Throw.whenNull(object1, "Objects may not be null.");
  48.         Throw.when(!categorization.get(0).isAssignableFrom(object1.getClass()), IllegalArgumentException.class,
  49.                 "Objects do not comply with the categorization; object 1 is of type %s, should be %s.", object1.getClass(),
  50.                 categorization.get(0));
  51.         for (int i = 1; i < categorization.size(); i++)
  52.         {
  53.             Throw.whenNull(objects[i - 1], "Objects may not be null.");
  54.             Throw.when(!categorization.get(i).isAssignableFrom(objects[i - 1].getClass()), IllegalArgumentException.class,
  55.                     "Objects do not comply with the categorization; object %d is of type %s, should be %s.", i + 1,
  56.                     objects[i - 1].getClass(), categorization.get(i));
  57.         }
  58.         this.objects.add(object1);
  59.         for (Object object : objects)
  60.         {
  61.             this.objects.add(object);
  62.         }
  63.     }

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

  76.     /**
  77.      * @return categorization.
  78.      */
  79.     public final Categorization getCategorization()
  80.     {
  81.         return this.categorization;
  82.     }

  83.     /**
  84.      * Returns the object of this category pertaining to the specified class from the category.
  85.      * @param clazz Class&lt;T&gt;; class from categorization to get the category object for
  86.      * @param <T> type of the object
  87.      * @return Object; object of this category pertaining to the specified class from the category
  88.      */
  89.     @SuppressWarnings("unchecked")
  90.     public final <T> T get(final Class<T> clazz)
  91.     {
  92.         Throw.when(!this.categorization.entails(clazz), RuntimeException.class,
  93.                 "Trying to get a categorization object by class, with the class not belonging to the categorization.");
  94.         for (Object obj : this.objects)
  95.         {
  96.             if (clazz.isAssignableFrom(obj.getClass()))
  97.             {
  98.                 return (T) obj; // assignable so safe
  99.             }
  100.         }
  101.         throw new RuntimeException(
  102.                 "Categorization contains no object assignable to given class, even though the categorization entails the class.");
  103.     }

  104.     /** {@inheritDoc} */
  105.     @Override
  106.     public final int hashCode()
  107.     {
  108.         final int prime = 31;
  109.         int result = 1;
  110.         result = prime * result + ((this.categorization == null) ? 0 : this.categorization.hashCode());
  111.         result = prime * result + ((this.objects == null) ? 0 : this.objects.hashCode());
  112.         return result;
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public final boolean equals(final Object obj)
  117.     {
  118.         if (this == obj)
  119.         {
  120.             return true;
  121.         }
  122.         if (obj == null)
  123.         {
  124.             return false;
  125.         }
  126.         if (getClass() != obj.getClass())
  127.         {
  128.             return false;
  129.         }
  130.         Category other = (Category) obj;
  131.         if (this.categorization == null)
  132.         {
  133.             if (other.categorization != null)
  134.             {
  135.                 return false;
  136.             }
  137.         }
  138.         else if (!this.categorization.equals(other.categorization))
  139.         {
  140.             return false;
  141.         }
  142.         if (this.objects == null)
  143.         {
  144.             if (other.objects != null)
  145.             {
  146.                 return false;
  147.             }
  148.         }
  149.         else if (!this.objects.equals(other.objects))
  150.         {
  151.             return false;
  152.         }
  153.         return true;
  154.     }

  155.     /** {@inheritDoc} */
  156.     @Override
  157.     public final String toString()
  158.     {
  159.         return "Category [" + this.objects + "]";
  160.     }

  161. }