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-2025 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/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
10   * project is distributed under a three-clause BSD-style license, which can be found at
11   * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
12   * </p>
13   * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
14   * @since 1.5
15   */
16  public class Renderable2dComparator implements Comparator<Renderable2dInterface<?>>
17  {
18      /**
19       * constructs a new Renderable2dComparator.
20       */
21      public Renderable2dComparator()
22      {
23          super();
24      }
25  
26      @Override
27      public int compare(final Renderable2dInterface<?> r1, final Renderable2dInterface<?> r2)
28      {
29          try
30          {
31              double z1 = r1.getSource().getZ();
32              double z2 = r2.getSource().getZ();
33              if (z1 > z2)
34              {
35                  return 1;
36              }
37              if (z1 < z2)
38              {
39                  return -1;
40              }
41          }
42          catch (Exception exception)
43          {
44              // ignore as this can happen when the source is in the process of deletion
45              // and therefore it cannot return a proper location.
46          }
47          return Long.compare(r1.getId(), r2.getId());
48      }
49  }