Make OrderComparator.build() use generic arguments
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / common / OrderComparator.java
1 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common;
2
3 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.Ordered;
4 import java.util.Comparator;
5
6 /**
7  * Comparator for comparing objects which extend Ordered.
8  *
9  * @param <T>
10  */
11 public class OrderComparator<T extends Ordered> implements Comparator<T> {
12
13     @SuppressWarnings("rawtypes")
14     private static final OrderComparator INSTANCE = new OrderComparator();
15
16     @SuppressWarnings("unchecked")
17     public static <T extends Ordered> OrderComparator<T> build() {
18         return INSTANCE;
19     }
20
21     @Override
22     public int compare(T obj1, T obj2) {
23         if(obj1 ==null || obj2==null ) {
24             throw new NullPointerException("Cannot compare null Actions");
25         } else if (obj1.getOrder() == null) {
26             throw new NullPointerException(errorMsg(obj1));
27         } else if (obj2.getOrder() == null) {
28             throw new NullPointerException(errorMsg(obj2));
29         }
30         return obj1.getOrder().compareTo(obj2.getOrder());
31     }
32
33     private String errorMsg(T obj) {
34         return "The comparing model " + obj + "has getOrder() == null. An order is mandatory";
35     }
36 }