View Javadoc
1   package nl.tudelft.simulation.dsol.animation.d2;
2   
3   import java.util.Comparator;
4   
5   /**
6    * A comparator for two Renderable2d objects. 
7    * <p>
8    * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
9    * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
10   * project is distributed under a three-clause BSD-style license, which can be found at
11   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
12   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
13   * </p>
14   * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
15   * @since 1.5
16   */
17  public class Renderable2dComparator implements Comparator<Renderable2dInterface<?>>
18  {
19      /**
20       * constructs a new Renderable2dComparator.
21       */
22      public Renderable2dComparator()
23      {
24          super();
25      }
26  
27      /** {@inheritDoc} */
28      @Override
29      public int compare(final Renderable2dInterface<?> r1, final Renderable2dInterface<?> r2)
30      {
31          try
32          {
33              double z1 = r1.getSource().getZ();
34              double z2 = r2.getSource().getZ();
35              if (z1 > z2)
36              {
37                  return 1;
38              }
39              if (z1 < z2)
40              {
41                  return -1;
42              }
43          }
44          catch (Exception exception)
45          {
46              // ignore as this can happen when the source is in the process of deletion
47              // and therefore it cannot return a proper location.
48          }
49          return Long.compare(r1.getId(), r2.getId());
50      }
51  }