Fix yangtools reference
[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 java.io.Serializable;
12 import java.util.Comparator;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.Ordered;
14
15 /**
16  * Comparator for comparing objects which extend Ordered.
17  *
18  * @param <T> T
19  */
20 public class OrderComparator<T extends Ordered> implements Comparator<T>, Serializable {
21     private static final long serialVersionUID = 1L;
22
23     @SuppressWarnings("rawtypes")
24     private static final OrderComparator INSTANCE = new OrderComparator();
25
26     @SuppressWarnings("unchecked")
27     public static <T extends Ordered> OrderComparator<T> build() {
28         return INSTANCE;
29     }
30
31     @Override
32     public int compare(T obj1, T obj2) {
33         if (obj1 == null || obj2 == null) {
34             throw new NullPointerException("Cannot compare null Actions");
35         } else if (obj1.getOrder() == null) {
36             throw new NullPointerException(errorMsg(obj1));
37         } else if (obj2.getOrder() == null) {
38             throw new NullPointerException(errorMsg(obj2));
39         }
40         return obj1.getOrder().compareTo(obj2.getOrder());
41     }
42
43     private String errorMsg(T obj) {
44         return "The comparing model " + obj + "has getOrder() == null. An order is mandatory";
45     }
46 }