Bug 5540 - Multiple convert types for one convertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / common / OrderComparator.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common;
10
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.Ordered;
12 import java.util.Comparator;
13
14 /**
15  * Comparator for comparing objects which extend Ordered.
16  *
17  * @param <T> T
18  */
19 public class OrderComparator<T extends Ordered> implements Comparator<T> {
20
21     @SuppressWarnings("rawtypes")
22     private static final OrderComparator INSTANCE = new OrderComparator();
23
24     @SuppressWarnings("unchecked")
25     public static <T extends Ordered> OrderComparator<T> build() {
26         return INSTANCE;
27     }
28
29     @Override
30     public int compare(T obj1, T obj2) {
31         if(obj1 ==null || obj2==null ) {
32             throw new NullPointerException("Cannot compare null Actions");
33         } else if (obj1.getOrder() == null) {
34             throw new NullPointerException(errorMsg(obj1));
35         } else if (obj2.getOrder() == null) {
36             throw new NullPointerException(errorMsg(obj2));
37         }
38         return obj1.getOrder().compareTo(obj2.getOrder());
39     }
40
41     private String errorMsg(T obj) {
42         return "The comparing model " + obj + "has getOrder() == null. An order is mandatory";
43     }
44 }