Category.java

  1. package org.opentrafficsim.road.gtu.strategical.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-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  11.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  12.  * <p>
  13.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 15, 2016 <br>
  14.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  15.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  16.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  17.  */
  18. public class Category implements Serializable
  19. {

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

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

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

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

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

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

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

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

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

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

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

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

  162. }