View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   
8   package net.kwfgrid.gwes;
9   
10  import net.kwfgrid.gworkflowdl.structure.Transition;
11  
12  import java.util.Comparator;
13  
14  /**
15   * Compares two transitions by means of its priority. 
16   * @author Andreas Hoheisel
17   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
18   * @version $Id: TransitionPriorityComparator.java 1419 2010-11-01 14:12:17Z hoheisel $
19   */
20  public class TransitionPriorityComparator implements Comparator {
21  
22      /**
23       * Compares its two Transitions for order.  Returns a negative integer,
24       * zero, or a positive integer as the priority of the first transition is less than, equal
25       * to, or greater than the second.<p>
26       * <p/>
27       * Note: this comparator imposes orderings that are inconsistent with equals.
28       *
29       * @param o1 the first object to be compared.
30       * @param o2 the second object to be compared.
31       * @return a negative integer, zero, or a positive integer as the
32       *         first argument is less than, equal to, or greater than the
33       *         second.
34       * @throws ClassCastException if the arguments' types prevent them from
35       *                            being compared by this Comparator.
36       */
37      public int compare(Object o1, Object o2) {
38          int prio1 = getPriority((Transition) o1);
39          int prio2 = getPriority((Transition) o2);
40          return prio1-prio2;
41      }
42  
43      static public int getPriority(Transition transition) {
44          int priority = 0;
45          // <property name="priority">1</property>
46          String propStr = transition.getProperties().get(Constants.PROP_TRANSITION_PRIORITY);
47          if (propStr != null) {
48              try {
49                  priority = Integer.parseInt(propStr);
50              } catch (NumberFormatException e) {
51                  // do nothing.
52              }
53          }
54          return priority;
55      }
56  
57  }